mirror of
https://github.com/filp/whoops.git
synced 2026-08-30 20:17:11 +00:00
added a SoapResponseHandler.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user