PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Service/Amazon/OnlineTest.php

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