added a SoapResponseHandler.

This commit is contained in:
Markus Staab
2013-12-27 09:02:39 +01:00
parent 764bbc3c40
commit fbf0e1d2e2
2 changed files with 123 additions and 0 deletions
@@ -0,0 +1,49 @@
<?php
/**
* Whoops - php errors for cool kids
* @author Filipe Dobreira <http://github.com/filp>
*/
namespace Whoops\Handler;
use Whoops\Handler\Handler;
/**
* Catches an exception and converts it to an Soap XML
* response.
*
* @author Markus Staab <http://github.com/staabm>
*/
class SoapResponseHandler extends Handler
{
/**
* @return int
*/
public function handle()
{
$exception = $this->getException();
echo $this->toXml($exception);
return Handler::QUIT;
}
/**
* Converts a Exception into a SoapFault XML
*/
private function toXml(\Exception $exception) {
$xml = '';
$xml .= '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">';
$xml .= ' <SOAP-ENV:Body>';
$xml .= ' <SOAP-ENV:Fault>';
$xml .= ' <faultcode>'. htmlspecialchars($exception->getCode()) .'</faultcode>';
$xml .= ' <faultstring>'. htmlspecialchars($exception->getMessage()) .'</faultstring>';
$xml .= ' <detail>'. htmlspecialchars($exception->getTraceAsString()) .'</detail>';
$xml .= ' </SOAP-ENV:Fault>';
$xml .= ' </SOAP-ENV:Body>';
$xml .= '</SOAP-ENV:Envelope>';
return $xml;
}
}
@@ -0,0 +1,74 @@
<?php
/**
* Whoops - php errors for cool kids
*/
namespace Whoops\Handler;
use Whoops\TestCase;
use Whoops\Handler\SoapResponseHandler;
use RuntimeException;
class SoapResponseHandlerTest extends TestCase
{
public function testSimpleValid()
{
$handler = new SoapResponseHandler;
$run = $this->getRunInstance();
$run->pushHandler($handler);
$run->register();
ob_start();
$run->handleException($this->getException());
$data = ob_get_clean();
$this->assertTrue($this->isValidXml($data));
return simplexml_load_string($data);
}
/**
* @depends testSimpleValid
*/
public function testSimpleValidCode(\SimpleXMLElement $xml)
{
$this->checkField($xml, 'faultcode', (string) $this->getException()->getCode());
}
/**
* @depends testSimpleValid
*/
public function testSimpleValidMessage(\SimpleXMLElement $xml)
{
$this->checkField($xml, 'faultstring', $this->getException()->getMessage());
}
/**
* Helper for testSimpleValid*
*/
private function checkField(\SimpleXMLElement $xml, $field, $value)
{
$list = $xml->xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault/'.$field);
$this->assertArrayHasKey(0, $list);
$this->assertSame($value, (string) $list[0]);
}
private function getException()
{
return new RuntimeException('boom', 678);
}
/**
* See if passed string is a valid XML document
* @param string $data
* @return boolean
*/
private function isValidXml($data)
{
$prev = libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);
libxml_use_internal_errors($prev);
return $xml !== false;
}
}