From 4ebb274c4ff0fbe33e69fad26e3306b104993a6a Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Wed, 22 Oct 2014 18:05:44 +0200
Subject: [PATCH] rel #17017 : Inspector gadget plugged into MappedSoapClient

---
 library/Class/WebService/MappedSoapClient.php |  38 +++++-
 .../Controller/Plugin/InspectorGadget.php     |  77 ++++++++++---
 .../Class/WebService/MappedSoapClientTest.php | 109 +++++++++++++++---
 3 files changed, 188 insertions(+), 36 deletions(-)

diff --git a/library/Class/WebService/MappedSoapClient.php b/library/Class/WebService/MappedSoapClient.php
index 22a62abc5fd..85f1e6c21f3 100644
--- a/library/Class/WebService/MappedSoapClient.php
+++ b/library/Class/WebService/MappedSoapClient.php
@@ -24,8 +24,27 @@
  * PHP classes to WSDL types with the same name
  */
 class Class_WebService_MappedSoapClient extends SoapClient {
+	protected static $_logger;
 	protected $_soap_server;
 
+	public static function setLogger($logger) {
+		static::$_logger = $logger;
+	}
+
+
+	public static function log($instance) {
+		if (!static::$_logger)
+			return;
+		static::$_logger->logSoap($instance);
+	}
+
+
+	public static function logError($url, $message) {
+		if (!static::$_logger)
+			return;
+		static::$_logger->logError($url, $message);
+	}
+
 
 	protected function __getWSDLStructTypes() {
 		$types = [];
@@ -66,14 +85,27 @@ class Class_WebService_MappedSoapClient extends SoapClient {
 
 	public function setServer($server) {
 		$this->_soap_server = $server;
+		return $this;
 	}
 
 
 	public function __doRequest($request, $location, $action, $version, $one_way = null) {
-		if(!$this->_soap_server)
-			return parent::__doRequest($request, $location, $action, $version, $one_way);
+		try {
+			if (!$this->_soap_server) {
+				$response = parent::__doRequest($request, $location, $action, $version, $one_way);
+				static::log($this);
+				return $response;
+			}
 
-		return $this->_soap_server->handle($request, $location, $action, $version, $one_way);
+			$response = $this->_soap_server
+				->handle($request, $location, $action, $version, $one_way);
+			static::log($this);
+			return $response;
+		} catch (Exception $e) {
+			static::logError($location, $request . $e->getMessage());
+			throw $e;
+		}
 	}
 }
+
 ?>
\ No newline at end of file
diff --git a/library/ZendAfi/Controller/Plugin/InspectorGadget.php b/library/ZendAfi/Controller/Plugin/InspectorGadget.php
index ad9f6ea4a7c..1e22596495e 100644
--- a/library/ZendAfi/Controller/Plugin/InspectorGadget.php
+++ b/library/ZendAfi/Controller/Plugin/InspectorGadget.php
@@ -30,6 +30,7 @@ class ZendAfi_Controller_Plugin_InspectorGadget extends Zend_Controller_Plugin_A
 
 		$this->beEnabled();
 		Class_WebService_SIGB_AbstractService::setLogger($this);
+		Class_WebService_MappedSoapClient::setLogger($this);
 	}
 
 
@@ -71,7 +72,7 @@ class ZendAfi_Controller_Plugin_InspectorGadget extends Zend_Controller_Plugin_A
 			. '<div data-type="' . self::PARAM_NAME . '" style="display:none;"><ol>';
 
 		foreach($this->_calls as $call)
-			$html .= '<li>' . $this->renderCall($call) . '</li>';
+			$html .= '<li>' . $call->render() . '</li>';
 
 		return $html . '</ol></div>';
 	}
@@ -83,34 +84,72 @@ onclick="$(\'[data-type=' . self::PARAM_NAME . ']\').dialog({title:\'Inspector\'
 	}
 
 
-	protected function renderCall($call) {
-		return
-			'<h3>Requête</h3>' . $call->request . ' (' . $call->response_code . ')'
-			. '<h3>Réponse</h3><textarea style="width:600px">' . $call->response_body . '</textarea>';
-	}
-
-
 	public function log() {
 		$httpClient = Zend_Registry::get('httpClient');
-		$call = new StdClass();
-		$call->request = $httpClient->getLastRequest();
-
+		$response_code = $response_body = null;
 		if ($response = $httpClient->getLastResponse()) {
-			$call->response_code = $response->getStatus();
-			$call->response_body = $response->getBody();
+			$response_code = $response->getStatus();
+			$response_body = $response->getBody();
 		}
 
-		$this->_calls[] = $call;
+		$this->_calls[] = new ZendAfi_Controller_Plugin_InspectorGadget_HttpCall($httpClient->getLastRequest(), $response_code, $response_body);
 	}
 
 
 	public function logError($url, $message) {
-		$call = new StdClass();
-		$call->request = $url;
-		$call->response_code = 'n/a';
-		$call->response_body = $message;
+		$this->_calls[] = new ZendAfi_Controller_Plugin_InspectorGadget_HttpCall($url,
+																																						 'n/a',
+																																						 $message);
+	}
+
 
-		$this->_calls[] = $call;
+	public function logSoap($client) {
+		$this->_calls[] = new ZendAfi_Controller_Plugin_InspectorGadget_SoapCall
+			($client->__getLastRequestHeaders(), $client->__getLastRequest(),
+			 $client->__getLastResponseHeaders(), $client->__getLastResponse());
+	}
+}
+
+
+
+class ZendAfi_Controller_Plugin_InspectorGadget_HttpCall {
+	protected $_request, $_response_code, $_response_body;
+
+	public function __construct($request, $response_code, $response_body) {
+		$this->_request = $request;
+		$this->_response_code = $response_code;
+		$this->_response_body = $response_body;
+	}
+
+
+	public function render() {
+		return
+			'<h3>Requête</h3>' . $this->_request . ' (' . $this->_response_code . ')'
+			. '<h3>Réponse</h3><textarea style="width:600px">' . $this->_response_body . '</textarea>';
+	}
+}
+
+
+
+class ZendAfi_Controller_Plugin_InspectorGadget_SoapCall {
+	protected $_request_headers, $_request_body,
+		$_response_headers, $_response_body;
+
+public function __construct($request_headers, $request_body,
+														$response_headers, $response_body) {
+		$this->_request_headers = $request_headers;
+		$this->_request_body = $request_body;
+		$this->_response_headers = $response_headers;
+		$this->_response_body = $response_body;
+	}
+
+
+	public function render() {
+		return
+			'<h3>Requête</h3>' . $this->_request_headers
+			. '<hr><textarea style="width:600px">' . $this->_request_body . '</textarea>'
+			. '<h3>Réponse</h3>' . $this->_response_headers
+			. '<hr><textarea style="width:600px">' . $this->_response_body . '</textarea>';
 	}
 }
 
diff --git a/tests/library/Class/WebService/MappedSoapClientTest.php b/tests/library/Class/WebService/MappedSoapClientTest.php
index bb45c2451b7..a87e6324521 100644
--- a/tests/library/Class/WebService/MappedSoapClientTest.php
+++ b/tests/library/Class/WebService/MappedSoapClientTest.php
@@ -20,30 +20,111 @@
  */
 
 
-class MappedSoapClientTest extends Storm_Test_ModelTestCase {
-	protected $_soap_client,
-		$_expected_xml;
+abstract class MappedSoapClientTestCase extends Storm_Test_ModelTestCase {
+	protected $_soap_client;
 
 	public function setUp() {
 		parent::setUp();
+		$this->_soap_client = new Class_WebService_MappedSoapClient(null,
+																																['uri' => 'http://afi-sa.fr/wsdl',
+																																 'location' => 'http://afi-sa.fr/wsdl',
+																																 'trace' => true,
+																																 'exceptions' => true]);
 
-		$this->_expected_xml = '<?xml version="1.0" encoding="UTF-8"?>
+		Class_WebService_MappedSoapClient::setLogger($this->getLogger());
+		$this->_soap_client
+			->setServer($this->getServer());
+	}
+
+
+	public function tearDown() {
+		Class_WebService_MappedSoapClient::setLogger(null);
+		parent::tearDown();
+	}
+
+
+	protected function getServer() {
+		return $this->mock()->beStrict();
+	}
+
+
+	protected function getLogger() {
+		return $this->mock()->beStrict();
+	}
+}
+
+
+
+
+class MappedSoapClientWithoutErrorTest extends MappedSoapClientTestCase {
+	protected $_response = '<?xml version="1.0" encoding="UTF-8"?>
 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://afi-sa.fr/wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:testing/></SOAP-ENV:Body></SOAP-ENV:Envelope>
 ';
 
-		$this->_soap_client = new Class_WebService_MappedSoapClient(null, ['uri' => 'http://afi-sa.fr/wsdl',
-																																			 'location' => 'http://afi-sa.fr/wsdl',
-																																			 'trace' => true]);
-		$this->_soap_client->setServer($this->mock()
-																	 ->whenCalled('handle')
-																	 ->answers($this->_expected_xml));
-		$this->_soap_client->testing();
+	protected function getServer() {
+		return $this->mock()
+								->whenCalled('handle')
+								->answers($this->_response);
+	}
+
+
+	protected function getLogger() {
+		return $this->_logger = $this->mock()
+																 ->whenCalled('logSoap')
+																 ->answers(true);
 	}
 
 
 	/** @test */
-	public function soapClientShouldReturnExpectedError() {
-		$this->assertEquals($this->_expected_xml, $this->_soap_client->__getLastResponse());
+	public function soapClientShouldCallLogger() {
+		$this->_soap_client->testing();
+		$this->assertTrue($this->_logger->methodHasBeenCalled('logSoap'));
 	}
 }
-?>
\ No newline at end of file
+
+
+
+class MappedSoapClientWithErrorTest extends MappedSoapClientTestCase {
+	protected function getServer() {
+		return $this->mock()
+								->whenCalled('handle')
+								->willDo(function() { throw new SoapFault('ERROR', 'Error message'); });
+	}
+
+
+	protected function getLogger() {
+		return $this->_logger = $this->mock()
+																 ->whenCalled('logError')
+																 ->answers(true);
+	}
+
+
+	/** @test */
+	public function soapClientShouldLogError() {
+		$closure = function() {
+			$this->assertTrue($this->_logger->methodHasBeenCalled('logError'));
+		};
+		$this->withTryDo($closure);
+	}
+
+
+	/** @test */
+	public function soapClientShouldLogErrorMessage() {
+		$closure = function() {
+			$this->assertContains('<SOAP-ENV:Body><ns1:testing/></SOAP-ENV:Body>',
+														$this->_logger->getAttributesForLastCallOn('logError')[1]);
+		};
+		$this->withTryDo($closure);
+	}
+
+
+	protected function withTryDo($closure) {
+		try {
+			$this->_soap_client->testing();
+		} catch(SoapFault $e) {
+			$closure();
+			return;
+		}
+		$this->fail('No Soap Fault');
+	}
+}
\ No newline at end of file
-- 
GitLab