BaseTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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-2012 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-2012 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->assertType('SOAPClient', $base->getSoapClient());
  66. }
  67. public function testDefaultSoapHeadersHasTheLicenseInfoHeader()
  68. {
  69. $this->base->foo();
  70. $headers = $this->soapClient->calls[0]['headers'];
  71. $this->assertType('array', $headers);
  72. $this->assertEquals(1, count($headers));
  73. $header = $headers[0];
  74. $this->assertType('SoapHeader', $header);
  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->assertType('array', $headers);
  123. $this->assertEquals(1, count($headers));
  124. $header = $headers[0];
  125. $this->assertType('SoapHeader', $header);
  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->assertType('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->assertType('Zend_Service_StrikeIron_Decorator',
  153. $this->base->returnTheObject());
  154. }
  155. public function testMethodResultWrappingAnObjectAndSelectingDefaultResultProperty()
  156. {
  157. $this->assertEquals('unwraped', $this->base->wrapThis());
  158. }
  159. public function testMethodExceptionsAreWrapped()
  160. {
  161. try {
  162. $this->base->throwTheException();
  163. $this->fail();
  164. } catch (Exception $e) {
  165. $this->assertType('Zend_Service_StrikeIron_Exception', $e);
  166. $this->assertEquals('Exception: foo', $e->getMessage());
  167. $this->assertEquals(43, $e->getCode());
  168. }
  169. }
  170. public function testGettingOutputHeaders()
  171. {
  172. $this->assertSame(array(), $this->base->getLastOutputHeaders());
  173. $info = $this->base->foo();
  174. $this->assertEquals(Zend_Service_StrikeIron_BaseTest_MockSoapClient::$outputHeaders,
  175. $this->base->getLastOutputHeaders());
  176. }
  177. public function testGettingSubscriptionInfo()
  178. {
  179. $this->assertEquals(0, count($this->soapClient->calls));
  180. $info = $this->base->getSubscriptionInfo();
  181. $this->assertEquals(1, count($this->soapClient->calls));
  182. $this->assertEquals(3, $info->remainingHits);
  183. }
  184. public function testGettingSubscriptionInfoWithCaching()
  185. {
  186. $this->assertEquals(0, count($this->soapClient->calls));
  187. $this->base->foo();
  188. $this->base->getSubscriptionInfo();
  189. $this->assertEquals(1, count($this->soapClient->calls));
  190. }
  191. public function testGettingSubscriptionOverridingCache()
  192. {
  193. $this->assertEquals(0, count($this->soapClient->calls));
  194. $this->base->getSubscriptionInfo();
  195. $this->assertEquals(1, count($this->soapClient->calls));
  196. $this->base->getSubscriptionInfo(true);
  197. $this->assertEquals(2, count($this->soapClient->calls));
  198. }
  199. public function testGettingSubscriptionInfoWithDefaultQueryMethod()
  200. {
  201. $this->base->getSubscriptionInfo();
  202. $this->assertEquals('GetRemainingHits', $this->soapClient->calls[0]['method']);
  203. }
  204. public function testGettingSubscriptionInfoWithCustomQueryMethod()
  205. {
  206. $method = 'SendSubscriptionInfoHeaderPlease';
  207. $this->base->getSubscriptionInfo(true, $method);
  208. $this->assertEquals($method, $this->soapClient->calls[0]['method']);
  209. }
  210. public function testGettingSubscriptionInfoThrowsWhenHeaderNotFound()
  211. {
  212. try {
  213. $this->base->getSubscriptionInfo(true, 'ReturnNoOutputHeaders');
  214. $this->fail();
  215. } catch (Zend_Service_StrikeIron_Exception $e) {
  216. $this->assertRegExp('/no subscriptioninfo header/i', $e->getMessage());
  217. }
  218. }
  219. }
  220. /**
  221. * @category Zend
  222. * @package Zend_Service_StrikeIron
  223. * @subpackage UnitTests
  224. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  225. * @license http://framework.zend.com/license/new-bsd New BSD License
  226. */
  227. class Zend_Service_StrikeIron_BaseTest_MockSoapClient
  228. {
  229. public static $outputHeaders = array('SubscriptionInfo' => array('RemainingHits' => 3));
  230. public $calls = array();
  231. public function __soapCall($method, $params, $options, $headers, &$outputHeaders)
  232. {
  233. $outputHeaders = self::$outputHeaders;
  234. $this->calls[] = array('method' => $method,
  235. 'params' => $params,
  236. 'options' => $options,
  237. 'headers' => $headers);
  238. if ($method == 'ReturnTheObject') {
  239. // testMethodResultWrappingAnyObject
  240. return new stdclass();
  241. } else if ($method == 'WrapThis') {
  242. // testMethodResultWrappingAnObjectAndSelectingDefaultResultProperty
  243. return (object)array('WrapThisResult' => 'unwraped');
  244. } else if ($method == 'ThrowTheException') {
  245. // testMethodExceptionsAreWrapped
  246. throw new Exception('foo', 43);
  247. } else if ($method == 'ReturnNoOutputHeaders') {
  248. // testGettingSubscriptionInfoThrowsWhenHeaderNotFound
  249. $outputHeaders = array();
  250. } else {
  251. return 42;
  252. }
  253. }
  254. }