From f5569841232e8b492ee06c2ddb5230ad7555cf7f Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Wed, 22 Oct 2014 15:40:45 +0200
Subject: [PATCH] hotline #17017 wrap soap server for mocking handle

---
 library/Class/WebService/MappedSoapClient.php | 28 ++++++++---
 .../Class/WebService/MappedSoapClientTest.php | 49 +++++++++++++++++++
 2 files changed, 71 insertions(+), 6 deletions(-)
 create mode 100644 tests/library/Class/WebService/MappedSoapClientTest.php

diff --git a/library/Class/WebService/MappedSoapClient.php b/library/Class/WebService/MappedSoapClient.php
index b9fafb58f3b..22a62abc5fd 100644
--- a/library/Class/WebService/MappedSoapClient.php
+++ b/library/Class/WebService/MappedSoapClient.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with AFI-OPAC 2.0; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 /**
@@ -24,14 +24,17 @@
  * PHP classes to WSDL types with the same name
  */
 class Class_WebService_MappedSoapClient extends SoapClient {
+	protected $_soap_server;
+
+
 	protected function __getWSDLStructTypes() {
 		$types = [];
 		foreach($this->__getTypes() as $type) {
 			preg_match('/([a-z0-9_]+)\s+([a-z0-9_]+(\[\])?)(.*)?/si', $type, $matches);
-			if ($matches[1] == 'struct') 
+			if ($matches[1] == 'struct')
 				$types[] = $matches[2];
 		}
-		return $types;		
+		return $types;
 	}
 
 
@@ -42,13 +45,13 @@ class Class_WebService_MappedSoapClient extends SoapClient {
 				$this->_classmap[$classname] = $classname;
 	}
 
-	
+
 	/**
 	 * @param $name string
-	 * @return boolean 
+	 * @return boolean
 	 */
 	public function hasFunction($name) {
-		foreach ($this->__getFunctions() as $function) 
+		foreach ($this->__getFunctions() as $function)
 			if (false !== strpos($function, ' ' . $name . '('))
 				return true;
 		return false;
@@ -59,5 +62,18 @@ class Class_WebService_MappedSoapClient extends SoapClient {
 		parent::__construct($wsdl, $options);
 		$this->__generateClassmap();
 	}
+
+
+	public function setServer($server) {
+		$this->_soap_server = $server;
+	}
+
+
+	public function __doRequest($request, $location, $action, $version, $one_way = null) {
+		if(!$this->_soap_server)
+			return parent::__doRequest($request, $location, $action, $version, $one_way);
+
+		return $this->_soap_server->handle($request, $location, $action, $version, $one_way);
+	}
 }
 ?>
\ No newline at end of file
diff --git a/tests/library/Class/WebService/MappedSoapClientTest.php b/tests/library/Class/WebService/MappedSoapClientTest.php
new file mode 100644
index 00000000000..bb45c2451b7
--- /dev/null
+++ b/tests/library/Class/WebService/MappedSoapClientTest.php
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * AFI-OPAC 2.0 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class MappedSoapClientTest extends Storm_Test_ModelTestCase {
+	protected $_soap_client,
+		$_expected_xml;
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->_expected_xml = '<?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();
+	}
+
+
+	/** @test */
+	public function soapClientShouldReturnExpectedError() {
+		$this->assertEquals($this->_expected_xml, $this->_soap_client->__getLastResponse());
+	}
+}
+?>
\ No newline at end of file
-- 
GitLab