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