BaseTest.php 11 KB

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