BaseTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_StrikeIron
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @ see Zend_Service_StrikeIron_BaseTest
  24. */
  25. require_once 'Zend/Service/StrikeIron/BaseTest.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_StrikeIron
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Service
  33. * @group Zend_Service_StrikeIron
  34. */
  35. class Zend_Service_StrikeIron_BaseTest extends PHPUnit_Framework_TestCase
  36. {
  37. public function setUp()
  38. {
  39. $this->soapClient = new Zend_Service_StrikeIron_BaseTest_MockSoapClient;
  40. $this->base = new Zend_Service_StrikeIron_Base(array('client' => $this->soapClient,
  41. 'username' => 'user',
  42. 'password' => 'pass'));
  43. }
  44. public function testHasNoPredefinedWsdl()
  45. {
  46. $this->assertSame(null, $this->base->getWsdl());
  47. }
  48. public function testSettingWsdl()
  49. {
  50. $wsdl = 'http://example.com/foo';
  51. $base = new Zend_Service_StrikeIron_Base(array('client' => $this->soapClient,
  52. 'wsdl' => $wsdl));
  53. $this->assertEquals($wsdl, $base->getWsdl());
  54. }
  55. public function testSoapClientDependencyInjection()
  56. {
  57. $this->assertSame($this->soapClient, $this->base->getSoapClient());
  58. }
  59. public function testSoapClientInitializesDefaultSOAPClient()
  60. {
  61. // set soapclient options to non-wsdl mode just to get a
  62. // soapclient instance without hitting the network
  63. $base = new Zend_Service_StrikeIron_Base(array('options' => array('location' => '',
  64. 'uri' => '')));
  65. $this->assertTrue($base->getSoapClient() instanceof SOAPClient);
  66. }
  67. public function testDefaultSoapHeadersHasTheLicenseInfoHeader()
  68. {
  69. $this->base->foo();
  70. $headers = $this->soapClient->calls[0]['headers'];
  71. $this->assertTrue(is_array($headers));
  72. $this->assertEquals(1, count($headers));
  73. $header = $headers[0];
  74. $this->assertTrue($header instanceof SoapHeader);
  75. $this->assertEquals('LicenseInfo', $header->name);
  76. $this->assertEquals('user', $header->data['RegisteredUser']['UserID']);
  77. $this->assertEquals('pass', $header->data['RegisteredUser']['Password']);
  78. }
  79. public function testAddingInvalidSoapHeaderThrows()
  80. {
  81. $invalidHeaders = 'foo';
  82. try {
  83. $base = new Zend_Service_StrikeIron_Base(array('client' => $this->soapClient,
  84. 'headers' => $invalidHeaders));
  85. $this->fail();
  86. } catch (Zend_Service_StrikeIron_Exception $e) {
  87. $this->assertRegExp('/instance of soapheader/i', $e->getMessage());
  88. }
  89. }
  90. public function testAddingInvalidSoapHeaderArrayThrows()
  91. {
  92. $invalidHeaders = array('foo');
  93. try {
  94. $base = new Zend_Service_StrikeIron_Base(array('client' => $this->soapClient,
  95. 'headers' => $invalidHeaders));
  96. $this->fail();
  97. } catch (Zend_Service_StrikeIron_Exception $e) {
  98. $this->assertRegExp('/instance of soapheader/i', $e->getMessage());
  99. }
  100. }
  101. public function testAddingScalarSoapHeaderNotLicenseInfo()
  102. {
  103. $header = new SoapHeader('foo', 'bar');
  104. $base = new Zend_Service_StrikeIron_Base(array('client' => $this->soapClient,
  105. 'headers' => $header));
  106. $base->foo();
  107. $headers = $this->soapClient->calls[0]['headers'];
  108. $this->assertEquals(2, count($headers));
  109. $this->assertEquals($header->name, $headers[0]->name);
  110. $this->assertEquals('LicenseInfo', $headers[1]->name);
  111. }
  112. public function testAddingScalarSoapHeaderThatOverridesLicenseInfo()
  113. {
  114. $soapHeaders = new SoapHeader('http://ws.strikeiron.com',
  115. 'LicenseInfo',
  116. array('RegisteredUser' => array('UserID' => 'foo',
  117. 'Password' => 'bar')));
  118. $base = new Zend_Service_StrikeIron_Base(array('client' => $this->soapClient,
  119. 'headers' => $soapHeaders));
  120. $base->foo();
  121. $headers = $this->soapClient->calls[0]['headers'];
  122. $this->assertTrue(is_array($headers));
  123. $this->assertEquals(1, count($headers));
  124. $header = $headers[0];
  125. $this->assertTrue($header instanceof SoapHeader);
  126. $this->assertEquals('LicenseInfo', $header->name);
  127. $this->assertEquals('foo', $header->data['RegisteredUser']['UserID']);
  128. $this->assertEquals('bar', $header->data['RegisteredUser']['Password']);
  129. }
  130. public function testAddingArrayOfSoapHeaders()
  131. {
  132. $headers = array(new SoapHeader('foo', 'bar'),
  133. new SoapHeader('baz', 'qux'));
  134. $base = new Zend_Service_StrikeIron_Base(array('client' => $this->soapClient,
  135. 'headers' => $headers));
  136. $base->foo();
  137. $headers = $this->soapClient->calls[0]['headers'];
  138. $this->assertTrue(is_array($headers));
  139. $this->assertEquals(3, count($headers)); // these 2 + default LicenseInfo
  140. }
  141. public function testMethodInflection()
  142. {
  143. $this->base->foo();
  144. $this->assertEquals('Foo', $this->soapClient->calls[0]['method']);
  145. }
  146. public function testMethodResultNotWrappingNonObject()
  147. {
  148. $this->assertEquals(42, $this->base->returnThe42());
  149. }
  150. public function testMethodResultWrappingAnyObject()
  151. {
  152. $this->assertTrue(
  153. $this->base->returnTheObject() instanceof Zend_Service_StrikeIron_Decorator
  154. );
  155. }
  156. public function testMethodResultWrappingAnObjectAndSelectingDefaultResultProperty()
  157. {
  158. $this->assertEquals('unwraped', $this->base->wrapThis());
  159. }
  160. public function testMethodExceptionsAreWrapped()
  161. {
  162. try {
  163. $this->base->throwTheException();
  164. $this->fail();
  165. } catch (Exception $e) {
  166. $this->assertTrue($e instanceof Zend_Service_StrikeIron_Exception);
  167. $this->assertEquals('Exception: foo', $e->getMessage());
  168. $this->assertEquals(43, $e->getCode());
  169. }
  170. }
  171. public function testGettingOutputHeaders()
  172. {
  173. $this->assertSame(array(), $this->base->getLastOutputHeaders());
  174. $info = $this->base->foo();
  175. $this->assertEquals(Zend_Service_StrikeIron_BaseTest_MockSoapClient::$outputHeaders,
  176. $this->base->getLastOutputHeaders());
  177. }
  178. public function testGettingSubscriptionInfo()
  179. {
  180. $this->assertEquals(0, count($this->soapClient->calls));
  181. $info = $this->base->getSubscriptionInfo();
  182. $this->assertEquals(1, count($this->soapClient->calls));
  183. $this->assertEquals(3, $info->remainingHits);
  184. }
  185. public function testGettingSubscriptionInfoWithCaching()
  186. {
  187. $this->assertEquals(0, count($this->soapClient->calls));
  188. $this->base->foo();
  189. $this->base->getSubscriptionInfo();
  190. $this->assertEquals(1, count($this->soapClient->calls));
  191. }
  192. public function testGettingSubscriptionOverridingCache()
  193. {
  194. $this->assertEquals(0, count($this->soapClient->calls));
  195. $this->base->getSubscriptionInfo();
  196. $this->assertEquals(1, count($this->soapClient->calls));
  197. $this->base->getSubscriptionInfo(true);
  198. $this->assertEquals(2, count($this->soapClient->calls));
  199. }
  200. public function testGettingSubscriptionInfoWithDefaultQueryMethod()
  201. {
  202. $this->base->getSubscriptionInfo();
  203. $this->assertEquals('GetRemainingHits', $this->soapClient->calls[0]['method']);
  204. }
  205. public function testGettingSubscriptionInfoWithCustomQueryMethod()
  206. {
  207. $method = 'SendSubscriptionInfoHeaderPlease';
  208. $this->base->getSubscriptionInfo(true, $method);
  209. $this->assertEquals($method, $this->soapClient->calls[0]['method']);
  210. }
  211. public function testGettingSubscriptionInfoThrowsWhenHeaderNotFound()
  212. {
  213. try {
  214. $this->base->getSubscriptionInfo(true, 'ReturnNoOutputHeaders');
  215. $this->fail();
  216. } catch (Zend_Service_StrikeIron_Exception $e) {
  217. $this->assertRegExp('/no subscriptioninfo header/i', $e->getMessage());
  218. }
  219. }
  220. }
  221. /**
  222. * @category Zend
  223. * @package Zend_Service_StrikeIron
  224. * @subpackage UnitTests
  225. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  226. * @license http://framework.zend.com/license/new-bsd New BSD License
  227. */
  228. class Zend_Service_StrikeIron_BaseTest_MockSoapClient
  229. {
  230. public static $outputHeaders = array('SubscriptionInfo' => array('RemainingHits' => 3));
  231. public $calls = array();
  232. public function __soapCall($method, $params, $options, $headers, &$outputHeaders)
  233. {
  234. $outputHeaders = self::$outputHeaders;
  235. $this->calls[] = array('method' => $method,
  236. 'params' => $params,
  237. 'options' => $options,
  238. 'headers' => $headers);
  239. if ($method == 'ReturnTheObject') {
  240. // testMethodResultWrappingAnyObject
  241. return new stdclass();
  242. } else if ($method == 'WrapThis') {
  243. // testMethodResultWrappingAnObjectAndSelectingDefaultResultProperty
  244. return (object)array('WrapThisResult' => 'unwraped');
  245. } else if ($method == 'ThrowTheException') {
  246. // testMethodExceptionsAreWrapped
  247. throw new Exception('foo', 43);
  248. } else if ($method == 'ReturnNoOutputHeaders') {
  249. // testGettingSubscriptionInfoThrowsWhenHeaderNotFound
  250. $outputHeaders = array();
  251. } else {
  252. return 42;
  253. }
  254. }
  255. }