OnlineTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_Amazon
  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_Amazon
  28. */
  29. require_once 'Zend/Service/Amazon.php';
  30. /**
  31. * @see Zend_Service_Amazon_Query
  32. */
  33. require_once 'Zend/Service/Amazon/Query.php';
  34. /**
  35. * @see Zend_Http_Client_Adapter_Socket
  36. */
  37. require_once 'Zend/Http/Client/Adapter/Socket.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Service_Amazon
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_Service_Amazon_OnlineTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Reference to Amazon service consumer object
  49. *
  50. * @var Zend_Service_Amazon
  51. */
  52. protected $_amazon;
  53. /**
  54. * Reference to Amazon query API object
  55. *
  56. * @var Zend_Service_Amazon_Query
  57. */
  58. protected $_query;
  59. /**
  60. * Socket based HTTP client adapter
  61. *
  62. * @var Zend_Http_Client_Adapter_Socket
  63. */
  64. protected $_httpClientAdapterSocket;
  65. /**
  66. * Sets up this test case
  67. *
  68. * @return void
  69. */
  70. public function setUp()
  71. {
  72. $this->_amazon = new Zend_Service_Amazon(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'));
  73. $this->_query = new Zend_Service_Amazon_Query(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'));
  74. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  75. $this->_amazon->getRestClient()
  76. ->getHttpClient()
  77. ->setAdapter($this->_httpClientAdapterSocket);
  78. // terms of use compliance: no more than one query per second
  79. sleep(1);
  80. }
  81. /**
  82. * Ensures that itemSearch() works as expected when searching for PHP books
  83. *
  84. * @return void
  85. */
  86. public function testItemSearchBooksPhp()
  87. {
  88. $resultSet = $this->_amazon->itemSearch(array(
  89. 'SearchIndex' => 'Books',
  90. 'Keywords' => 'php',
  91. 'ResponseGroup' => 'Small,ItemAttributes,Images,SalesRank,Reviews,EditorialReview,Similarities,'
  92. . 'ListmaniaLists'
  93. ));
  94. $this->assertTrue(10 < $resultSet->totalResults());
  95. $this->assertTrue(1 < $resultSet->totalPages());
  96. $this->assertEquals(0, $resultSet->key());
  97. try {
  98. $resultSet->seek(-1);
  99. $this->fail('Expected OutOfBoundsException not thrown');
  100. } catch (OutOfBoundsException $e) {
  101. $this->assertContains('Illegal index', $e->getMessage());
  102. }
  103. $resultSet->seek(9);
  104. try {
  105. $resultSet->seek(10);
  106. $this->fail('Expected OutOfBoundsException not thrown');
  107. } catch (OutOfBoundsException $e) {
  108. $this->assertContains('Illegal index', $e->getMessage());
  109. }
  110. foreach ($resultSet as $item) {
  111. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  112. }
  113. $this->assertTrue(simplexml_load_string($item->asXml()) instanceof SimpleXMLElement);
  114. }
  115. /**
  116. * Ensures that itemSearch() works as expected when searching for music with keyword of Mozart
  117. *
  118. * @return void
  119. */
  120. public function testItemSearchMusicMozart()
  121. {
  122. $resultSet = $this->_amazon->itemSearch(array(
  123. 'SearchIndex' => 'Music',
  124. 'Keywords' => 'Mozart',
  125. 'ResponseGroup' => 'Small,Tracks,Offers'
  126. ));
  127. foreach ($resultSet as $item) {
  128. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  129. }
  130. }
  131. /**
  132. * Ensures that itemSearch() works as expected when searching for digital cameras
  133. *
  134. * @return void
  135. */
  136. public function testItemSearchElectronicsDigitalCamera()
  137. {
  138. $resultSet = $this->_amazon->itemSearch(array(
  139. 'SearchIndex' => 'Electronics',
  140. 'Keywords' => 'digital camera',
  141. 'ResponseGroup' => 'Accessories'
  142. ));
  143. foreach ($resultSet as $item) {
  144. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  145. }
  146. }
  147. /**
  148. * Ensures that itemSearch() works as expected when sorting
  149. *
  150. * @return void
  151. */
  152. public function testItemSearchBooksPHPSort()
  153. {
  154. $resultSet = $this->_amazon->itemSearch(array(
  155. 'SearchIndex' => 'Books',
  156. 'Keywords' => 'php',
  157. 'Sort' => '-titlerank'
  158. ));
  159. foreach ($resultSet as $item) {
  160. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  161. }
  162. }
  163. /**
  164. * Ensures that itemSearch() throws an exception when provided an invalid city
  165. *
  166. * @return void
  167. */
  168. public function testItemSearchExceptionCityInvalid()
  169. {
  170. try {
  171. $this->_amazon->itemSearch(array(
  172. 'SearchIndex' => 'Restaurants',
  173. 'Keywords' => 'seafood',
  174. 'City' => 'Des Moines'
  175. ));
  176. $this->fail('Expected Zend_Service_Exception not thrown');
  177. } catch (Zend_Service_Exception $e) {
  178. }
  179. }
  180. /**
  181. * Ensures that itemLookup() works as expected
  182. *
  183. * @return void
  184. */
  185. public function testItemLookup()
  186. {
  187. $item = $this->_amazon->itemLookup('B0000A432X');
  188. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  189. }
  190. /**
  191. * Ensures that itemLookup() throws an exception when provided an invalid ASIN
  192. *
  193. * @return void
  194. */
  195. public function testItemLookupExceptionAsinInvalid()
  196. {
  197. try {
  198. $this->_amazon->itemLookup('oops');
  199. $this->fail('Expected Zend_Service_Exception not thrown');
  200. } catch (Zend_Service_Exception $e) {
  201. $this->assertContains('not a valid value for ItemId', $e->getMessage());
  202. }
  203. }
  204. /**
  205. * Ensures that itemLookup() works as expected when provided multiple ASINs
  206. *
  207. * @return void
  208. */
  209. public function testItemLookupMultiple()
  210. {
  211. $resultSet = $this->_amazon->itemLookup('0596006810,1590593804');
  212. $count = 0;
  213. foreach ($resultSet as $item) {
  214. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  215. $count++;
  216. }
  217. $this->assertEquals(2, $count);
  218. }
  219. /**
  220. * Ensures that itemLookup() throws an exception when given a SearchIndex
  221. *
  222. * @return void
  223. */
  224. public function testItemLookupExceptionSearchIndex()
  225. {
  226. try {
  227. $this->_amazon->itemLookup('oops', array('SearchIndex' => 'Books'));
  228. $this->fail('Expected Zend_Service_Exception not thrown');
  229. } catch (Zend_Service_Exception $e) {
  230. $this->assertContains('restricted parameter combination', $e->getMessage());
  231. }
  232. }
  233. /**
  234. * Ensures that the query API works as expected when searching for PHP books
  235. *
  236. * @return void
  237. */
  238. public function testQueryBooksPhp()
  239. {
  240. $resultSet = $this->_query->category('Books')->Keywords('php')->search();
  241. foreach ($resultSet as $item) {
  242. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  243. }
  244. }
  245. /**
  246. * Ensures that the query API throws an exception when a category is not first provided
  247. *
  248. * @return void
  249. */
  250. public function testQueryExceptionCategoryMissing()
  251. {
  252. try {
  253. $this->_query->Keywords('php');
  254. $this->fail('Expected Zend_Service_Exception not thrown');
  255. } catch (Zend_Service_Exception $e) {
  256. $this->assertContains('set a category', $e->getMessage());
  257. }
  258. }
  259. /**
  260. * Ensures that the query API throws an exception when the category is invalid
  261. *
  262. * @return void
  263. */
  264. public function testQueryExceptionCategoryInvalid()
  265. {
  266. try {
  267. $this->_query->category('oops')->search();
  268. $this->fail('Expected Zend_Service_Exception not thrown');
  269. } catch (Zend_Service_Exception $e) {
  270. $this->assertContains('SearchIndex is invalid', $e->getMessage());
  271. }
  272. }
  273. /**
  274. * Ensures that the query API works as expected when searching by ASIN
  275. *
  276. * @return void
  277. */
  278. public function testQueryAsin()
  279. {
  280. $item = $this->_query->asin('B0000A432X')->search();
  281. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  282. }
  283. }
  284. class Zend_Service_Amazon_OnlineTest_Skip extends PHPUnit_Framework_TestCase
  285. {
  286. public function setUp()
  287. {
  288. $this->markTestSkipped('Zend_Service_Amazon online tests not enabled with an access key ID in '
  289. . 'TestConfiguration.php');
  290. }
  291. public function testNothing()
  292. {
  293. }
  294. }