Skip to content
Snippets Groups Projects
Commit 4ebb274c authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

rel #17017 : Inspector gadget plugged into MappedSoapClient

parent f5569841
Branches
Tags
5 merge requests!529Hotline 6.56,!512Master,!484Master,!483Hotline 6.54,!478Hotline #17017 missing informations in user account with soap client
...@@ -24,8 +24,27 @@ ...@@ -24,8 +24,27 @@
* PHP classes to WSDL types with the same name * PHP classes to WSDL types with the same name
*/ */
class Class_WebService_MappedSoapClient extends SoapClient { class Class_WebService_MappedSoapClient extends SoapClient {
protected static $_logger;
protected $_soap_server; 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() { protected function __getWSDLStructTypes() {
$types = []; $types = [];
...@@ -66,14 +85,27 @@ class Class_WebService_MappedSoapClient extends SoapClient { ...@@ -66,14 +85,27 @@ class Class_WebService_MappedSoapClient extends SoapClient {
public function setServer($server) { public function setServer($server) {
$this->_soap_server = $server; $this->_soap_server = $server;
return $this;
} }
public function __doRequest($request, $location, $action, $version, $one_way = null) { public function __doRequest($request, $location, $action, $version, $one_way = null) {
if(!$this->_soap_server) try {
return parent::__doRequest($request, $location, $action, $version, $one_way); 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
...@@ -30,6 +30,7 @@ class ZendAfi_Controller_Plugin_InspectorGadget extends Zend_Controller_Plugin_A ...@@ -30,6 +30,7 @@ class ZendAfi_Controller_Plugin_InspectorGadget extends Zend_Controller_Plugin_A
$this->beEnabled(); $this->beEnabled();
Class_WebService_SIGB_AbstractService::setLogger($this); 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 ...@@ -71,7 +72,7 @@ class ZendAfi_Controller_Plugin_InspectorGadget extends Zend_Controller_Plugin_A
. '<div data-type="' . self::PARAM_NAME . '" style="display:none;"><ol>'; . '<div data-type="' . self::PARAM_NAME . '" style="display:none;"><ol>';
foreach($this->_calls as $call) foreach($this->_calls as $call)
$html .= '<li>' . $this->renderCall($call) . '</li>'; $html .= '<li>' . $call->render() . '</li>';
return $html . '</ol></div>'; return $html . '</ol></div>';
} }
...@@ -83,34 +84,72 @@ onclick="$(\'[data-type=' . self::PARAM_NAME . ']\').dialog({title:\'Inspector\' ...@@ -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() { public function log() {
$httpClient = Zend_Registry::get('httpClient'); $httpClient = Zend_Registry::get('httpClient');
$call = new StdClass(); $response_code = $response_body = null;
$call->request = $httpClient->getLastRequest();
if ($response = $httpClient->getLastResponse()) { if ($response = $httpClient->getLastResponse()) {
$call->response_code = $response->getStatus(); $response_code = $response->getStatus();
$call->response_body = $response->getBody(); $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) { public function logError($url, $message) {
$call = new StdClass(); $this->_calls[] = new ZendAfi_Controller_Plugin_InspectorGadget_HttpCall($url,
$call->request = $url; 'n/a',
$call->response_code = 'n/a'; $message);
$call->response_body = $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>';
} }
} }
......
...@@ -20,30 +20,111 @@ ...@@ -20,30 +20,111 @@
*/ */
class MappedSoapClientTest extends Storm_Test_ModelTestCase { abstract class MappedSoapClientTestCase extends Storm_Test_ModelTestCase {
protected $_soap_client, protected $_soap_client;
$_expected_xml;
public function setUp() { public function setUp() {
parent::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> <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', protected function getServer() {
'location' => 'http://afi-sa.fr/wsdl', return $this->mock()
'trace' => true]); ->whenCalled('handle')
$this->_soap_client->setServer($this->mock() ->answers($this->_response);
->whenCalled('handle') }
->answers($this->_expected_xml));
$this->_soap_client->testing();
protected function getLogger() {
return $this->_logger = $this->mock()
->whenCalled('logSoap')
->answers(true);
} }
/** @test */ /** @test */
public function soapClientShouldReturnExpectedError() { public function soapClientShouldCallLogger() {
$this->assertEquals($this->_expected_xml, $this->_soap_client->__getLastResponse()); $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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment