2
0

OnlineTest.php 10 KB

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