From fbf0e1d2e2f87e07da0c2bf98ae459994926ded8 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Dec 2013 09:02:39 +0100 Subject: [PATCH] added a SoapResponseHandler. --- src/Whoops/Handler/SoapResponseHandler.php | 49 ++++++++++++ .../Handler/SoapResponseHandlerTest.php | 74 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/Whoops/Handler/SoapResponseHandler.php create mode 100644 tests/Whoops/Handler/SoapResponseHandlerTest.php diff --git a/src/Whoops/Handler/SoapResponseHandler.php b/src/Whoops/Handler/SoapResponseHandler.php new file mode 100644 index 0000000..d89125e --- /dev/null +++ b/src/Whoops/Handler/SoapResponseHandler.php @@ -0,0 +1,49 @@ + + */ + +namespace Whoops\Handler; + +use Whoops\Handler\Handler; + +/** + * Catches an exception and converts it to an Soap XML + * response. + * + * @author Markus Staab + */ +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 .= ''; + $xml .= ' '; + $xml .= ' '; + $xml .= ' '. htmlspecialchars($exception->getCode()) .''; + $xml .= ' '. htmlspecialchars($exception->getMessage()) .''; + $xml .= ' '. htmlspecialchars($exception->getTraceAsString()) .''; + $xml .= ' '; + $xml .= ' '; + $xml .= ''; + + return $xml; + } +} diff --git a/tests/Whoops/Handler/SoapResponseHandlerTest.php b/tests/Whoops/Handler/SoapResponseHandlerTest.php new file mode 100644 index 0000000..4a972d9 --- /dev/null +++ b/tests/Whoops/Handler/SoapResponseHandlerTest.php @@ -0,0 +1,74 @@ +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; + } +}