OnlineTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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-2009 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-2009 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. if(!defined('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID') || !defined('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')) {
  73. $this->markTestSkipped('Constants AccessKeyId and SecretKey have to be set.');
  74. }
  75. $this->_amazon = new Zend_Service_Amazon(
  76. TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID,
  77. 'US',
  78. TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY
  79. );
  80. $this->_query = new Zend_Service_Amazon_Query(
  81. TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID,
  82. 'US',
  83. TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY
  84. );
  85. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  86. $this->_amazon->getRestClient()
  87. ->getHttpClient()
  88. ->setAdapter($this->_httpClientAdapterSocket);
  89. // terms of use compliance: no more than one query per second
  90. sleep(1);
  91. }
  92. /**
  93. * Ensures that itemSearch() works as expected when searching for PHP books
  94. * @group ItemSearchPhp
  95. * @return void
  96. */
  97. public function testItemSearchBooksPhp()
  98. {
  99. $resultSet = $this->_amazon->itemSearch(array(
  100. 'SearchIndex' => 'Books',
  101. 'Keywords' => 'php',
  102. 'ResponseGroup' => 'Small,ItemAttributes,Images,SalesRank,Reviews,EditorialReview,Similarities,'
  103. . 'ListmaniaLists'
  104. ));
  105. $this->assertTrue(10 < $resultSet->totalResults());
  106. $this->assertTrue(1 < $resultSet->totalPages());
  107. $this->assertEquals(0, $resultSet->key());
  108. try {
  109. $resultSet->seek(-1);
  110. $this->fail('Expected OutOfBoundsException not thrown');
  111. } catch (OutOfBoundsException $e) {
  112. $this->assertContains('Illegal index', $e->getMessage());
  113. }
  114. $resultSet->seek(9);
  115. try {
  116. $resultSet->seek(10);
  117. $this->fail('Expected OutOfBoundsException not thrown');
  118. } catch (OutOfBoundsException $e) {
  119. $this->assertContains('Illegal index', $e->getMessage());
  120. }
  121. foreach ($resultSet as $item) {
  122. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  123. }
  124. $this->assertTrue(simplexml_load_string($item->asXml()) instanceof SimpleXMLElement);
  125. }
  126. /**
  127. * Ensures that itemSearch() works as expected when searching for music with keyword of Mozart
  128. *
  129. * @return void
  130. */
  131. public function testItemSearchMusicMozart()
  132. {
  133. $resultSet = $this->_amazon->itemSearch(array(
  134. 'SearchIndex' => 'Music',
  135. 'Keywords' => 'Mozart',
  136. 'ResponseGroup' => 'Small,Tracks,Offers'
  137. ));
  138. foreach ($resultSet as $item) {
  139. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  140. }
  141. }
  142. /**
  143. * Ensures that itemSearch() works as expected when searching for digital cameras
  144. *
  145. * @return void
  146. */
  147. public function testItemSearchElectronicsDigitalCamera()
  148. {
  149. $resultSet = $this->_amazon->itemSearch(array(
  150. 'SearchIndex' => 'Electronics',
  151. 'Keywords' => 'digital camera',
  152. 'ResponseGroup' => 'Accessories'
  153. ));
  154. foreach ($resultSet as $item) {
  155. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  156. }
  157. }
  158. /**
  159. * Ensures that itemSearch() works as expected when sorting
  160. *
  161. * @return void
  162. */
  163. public function testItemSearchBooksPHPSort()
  164. {
  165. $resultSet = $this->_amazon->itemSearch(array(
  166. 'SearchIndex' => 'Books',
  167. 'Keywords' => 'php',
  168. 'Sort' => '-titlerank'
  169. ));
  170. foreach ($resultSet as $item) {
  171. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  172. }
  173. }
  174. /**
  175. * Ensures that itemSearch() throws an exception when provided an invalid city
  176. *
  177. * @return void
  178. */
  179. public function testItemSearchExceptionCityInvalid()
  180. {
  181. try {
  182. $this->_amazon->itemSearch(array(
  183. 'SearchIndex' => 'Restaurants',
  184. 'Keywords' => 'seafood',
  185. 'City' => 'Des Moines'
  186. ));
  187. $this->fail('Expected Zend_Service_Exception not thrown');
  188. } catch (Zend_Service_Exception $e) {
  189. }
  190. }
  191. /**
  192. * Ensures that itemLookup() works as expected
  193. *
  194. * @return void
  195. */
  196. public function testItemLookup()
  197. {
  198. $item = $this->_amazon->itemLookup('B0000A432X');
  199. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  200. }
  201. /**
  202. * Ensures that itemLookup() throws an exception when provided an invalid ASIN
  203. *
  204. * @return void
  205. */
  206. public function testItemLookupExceptionAsinInvalid()
  207. {
  208. try {
  209. $this->_amazon->itemLookup('oops');
  210. $this->fail('Expected Zend_Service_Exception not thrown');
  211. } catch (Zend_Service_Exception $e) {
  212. $this->assertContains('not a valid value for ItemId', $e->getMessage());
  213. }
  214. }
  215. /**
  216. * Ensures that itemLookup() works as expected when provided multiple ASINs
  217. *
  218. * @return void
  219. */
  220. public function testItemLookupMultiple()
  221. {
  222. $resultSet = $this->_amazon->itemLookup('0596006810,1590593804');
  223. $count = 0;
  224. foreach ($resultSet as $item) {
  225. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  226. $count++;
  227. }
  228. $this->assertEquals(2, $count);
  229. }
  230. /**
  231. * Ensures that itemLookup() throws an exception when given a SearchIndex
  232. *
  233. * @return void
  234. */
  235. public function testItemLookupExceptionSearchIndex()
  236. {
  237. try {
  238. $this->_amazon->itemLookup('oops', array('SearchIndex' => 'Books'));
  239. $this->fail('Expected Zend_Service_Exception not thrown');
  240. } catch (Zend_Service_Exception $e) {
  241. $this->assertContains('restricted parameter combination', $e->getMessage());
  242. }
  243. }
  244. /**
  245. * Ensures that the query API works as expected when searching for PHP books
  246. *
  247. * @return void
  248. */
  249. public function testQueryBooksPhp()
  250. {
  251. $resultSet = $this->_query->category('Books')->Keywords('php')->search();
  252. foreach ($resultSet as $item) {
  253. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  254. }
  255. }
  256. /**
  257. * Ensures that the query API throws an exception when a category is not first provided
  258. *
  259. * @return void
  260. */
  261. public function testQueryExceptionCategoryMissing()
  262. {
  263. try {
  264. $this->_query->Keywords('php');
  265. $this->fail('Expected Zend_Service_Exception not thrown');
  266. } catch (Zend_Service_Exception $e) {
  267. $this->assertContains('set a category', $e->getMessage());
  268. }
  269. }
  270. /**
  271. * Ensures that the query API throws an exception when the category is invalid
  272. *
  273. * @return void
  274. */
  275. public function testQueryExceptionCategoryInvalid()
  276. {
  277. try {
  278. $this->_query->category('oops')->search();
  279. $this->fail('Expected Zend_Service_Exception not thrown');
  280. } catch (Zend_Service_Exception $e) {
  281. $this->assertContains('SearchIndex is invalid', $e->getMessage());
  282. }
  283. }
  284. /**
  285. * Ensures that the query API works as expected when searching by ASIN
  286. *
  287. * @return void
  288. */
  289. public function testQueryAsin()
  290. {
  291. $item = $this->_query->asin('B0000A432X')->search();
  292. $this->assertTrue($item instanceof Zend_Service_Amazon_Item);
  293. }
  294. }
  295. class Zend_Service_Amazon_OnlineTest_Skip extends PHPUnit_Framework_TestCase
  296. {
  297. public function setUp()
  298. {
  299. $this->markTestSkipped('Zend_Service_Amazon online tests not enabled with an access key ID in '
  300. . 'TestConfiguration.php');
  301. }
  302. public function testNothing()
  303. {
  304. }
  305. }