OfflineTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_ResultSet
  28. */
  29. require_once 'Zend/Service/Amazon/ResultSet.php';
  30. /**
  31. * @see Zend_Service_Amazon_ResultSet
  32. */
  33. require_once 'Zend/Service/Amazon/SimilarProduct.php';
  34. /**
  35. * @see Zend_Http_Client_Adapter_Socket
  36. */
  37. require_once 'Zend/Http/Client/Adapter/Socket.php';
  38. /**
  39. * @see Zend_Http_Client_Adapter_Test
  40. */
  41. require_once 'Zend/Http/Client/Adapter/Test.php';
  42. /**
  43. * @category Zend
  44. * @package Zend_Service_Amazon
  45. * @subpackage UnitTests
  46. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. * @group Zend_Service
  49. * @group Zend_Service_Amazon
  50. */
  51. class Zend_Service_Amazon_OfflineTest extends PHPUnit_Framework_TestCase
  52. {
  53. /**
  54. * Reference to Amazon service consumer object
  55. *
  56. * @var Zend_Service_Amazon
  57. */
  58. protected $_amazon;
  59. /**
  60. * HTTP client adapter for testing
  61. *
  62. * @var Zend_Http_Client_Adapter_Test
  63. */
  64. protected $_httpClientAdapterTest;
  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->_httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  74. }
  75. /**
  76. * Ensures that __construct() throws an exception when given an invalid country code
  77. *
  78. * @return void
  79. */
  80. public function testConstructExceptionCountryCodeInvalid()
  81. {
  82. try {
  83. $amazon = new Zend_Service_Amazon(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'), 'oops');
  84. $this->fail('Expected Zend_Service_Exception not thrown');
  85. } catch (Zend_Service_Exception $e) {
  86. $this->assertContains('Unknown country code', $e->getMessage());
  87. }
  88. }
  89. /**
  90. * @group ZF-2056
  91. */
  92. public function testMozardSearchFromFile()
  93. {
  94. $xml = file_get_contents(dirname(__FILE__)."/_files/mozart_result.xml");
  95. $dom = new DOMDocument();
  96. $dom->loadXML($xml);
  97. $mozartTracks = array(
  98. 'B00005A8JZ' => '29',
  99. 'B0000058HV' => '25',
  100. 'B000BLI3K2' => '500',
  101. 'B00004X0QF' => '9',
  102. 'B000004194' => '19',
  103. 'B00000I9M0' => '9',
  104. 'B000004166' => '20',
  105. 'B00002DEH1' => '58',
  106. 'B0000041EV' => '12',
  107. 'B00004SA87' => '42',
  108. );
  109. $result = new Zend_Service_Amazon_ResultSet($dom);
  110. foreach($result AS $item) {
  111. $trackCount = $mozartTracks[$item->ASIN];
  112. $this->assertEquals($trackCount, count($item->Tracks));
  113. }
  114. }
  115. /**
  116. * @group ZF-2749
  117. */
  118. public function testSimilarProductConstructorMissingAttributeDoesNotThrowNotice()
  119. {
  120. $dom = new DOMDocument();
  121. $asin = $dom->createElement("ASIN", "TEST");
  122. $product = $dom->createElement("product");
  123. $product->appendChild($asin);
  124. $similarproduct = new Zend_Service_Amazon_SimilarProduct($product);
  125. }
  126. /**
  127. * @group ZF-7251
  128. */
  129. public function testFullOffersFromFile()
  130. {
  131. $xml = file_get_contents(dirname(__FILE__)."/_files/offers_with_names.xml");
  132. $dom = new DOMDocument();
  133. $dom->loadXML($xml);
  134. $dataExpected = array(
  135. '0439774098' => array(
  136. 'offers' => array(
  137. 'A79CLRHOQ3NF4' => array(
  138. 'name' => 'PLEXSUPPLY',
  139. 'price' => '5153'
  140. ),
  141. 'A2K9NS8DSVOE2W' => array(
  142. 'name' => 'nangsuer',
  143. 'price' => '5153'
  144. ),
  145. 'A31EVTLIC13ORD' => array(
  146. 'name' => 'Wizard of Math',
  147. 'price' => '7599'
  148. ),
  149. 'A3SKJE188CW5XG' => array(
  150. 'name' => 'ReStockIt',
  151. 'price' => '5299'
  152. ),
  153. 'A1729W3053T57N' => array(
  154. 'name' => 'The Price Pros',
  155. 'price' => '5487'
  156. ),
  157. 'A29PHU0KPCGV8S' => array(
  158. 'name' => 'TheFactoryDepot',
  159. 'price' => '5821'
  160. ),
  161. 'AIHRRFGW11GJ8' => array(
  162. 'name' => 'Design Tec Office Products',
  163. 'price' => '5987'
  164. ),
  165. 'A27OK403WRHSGI' => array(
  166. 'name' => 'Kaplan Early Learning Company',
  167. 'price' => '7595'
  168. ),
  169. 'A25DVOZOPBFMAN' => array(
  170. 'name' => 'Deerso',
  171. 'price' => '7599'
  172. ),
  173. 'A6IFKC796Y64H' => array(
  174. 'name' => 'The Education Station Inc',
  175. 'price' => '7599'
  176. ),
  177. ),
  178. ),
  179. 'B00000194U' => array(
  180. 'offers' => array(
  181. 'A3UOG6723G7MG0' => array(
  182. 'name' => 'Efunctional',
  183. 'price' => '480'
  184. ),
  185. 'A3SNNXCKUIW1O2' => array(
  186. 'name' => 'Universal Mania',
  187. 'price' => '531'
  188. ),
  189. 'A18ACDNYOEMMOL' => array(
  190. 'name' => 'ApexSuppliers',
  191. 'price' => '589'
  192. ),
  193. 'A2NYACAJP9I1IY' => array(
  194. 'name' => 'GizmosForLife',
  195. 'price' => '608'
  196. ),
  197. 'A1729W3053T57N' => array(
  198. 'name' => 'The Price Pros',
  199. 'price' => '628'
  200. ),
  201. 'A29PHU0KPCGV8S' => array(
  202. 'name' => 'TheFactoryDepot',
  203. 'price' => '638'
  204. ),
  205. 'A3Q3IAIX1CLBMZ' => array(
  206. 'name' => 'ElectroGalaxy',
  207. 'price' => '697'
  208. ),
  209. 'A1PC5XI7QQLW5G' => array(
  210. 'name' => 'Long Trading Company',
  211. 'price' => '860'
  212. ),
  213. 'A2R0FX412W1BDT' => array(
  214. 'name' => 'Beach Audio',
  215. 'price' => '896'
  216. ),
  217. 'AKJJGJ0JKT8F1' => array(
  218. 'name' => 'Buy.com',
  219. 'price' => '899'
  220. ),
  221. ),
  222. ),
  223. );
  224. $result = new Zend_Service_Amazon_ResultSet($dom);
  225. foreach($result AS $item) {
  226. $data = $dataExpected[$item->ASIN];
  227. foreach($item->Offers->Offers as $offer) {
  228. $this->assertEquals($data['offers'][$offer->MerchantId]['name'], $offer->MerchantName);
  229. $this->assertEquals($data['offers'][$offer->MerchantId]['price'], $offer->Price);
  230. }
  231. }
  232. }
  233. public function dataSignatureEncryption()
  234. {
  235. return array(
  236. array(
  237. 'http://webservices.amazon.com',
  238. array(
  239. 'Service' => 'AWSECommerceService',
  240. 'AWSAccessKeyId' => '00000000000000000000',
  241. 'Operation' => 'ItemLookup',
  242. 'ItemId' => '0679722769',
  243. 'ResponseGroup' => 'ItemAttributes,Offers,Images,Reviews',
  244. 'Version' => '2009-01-06',
  245. 'Timestamp' => '2009-01-01T12:00:00Z',
  246. ),
  247. "GET\n".
  248. "webservices.amazon.com\n".
  249. "/onca/xml\n".
  250. "AWSAccessKeyId=00000000000000000000&ItemId=0679722769&Operation=I".
  251. "temLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReview".
  252. "s&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&".
  253. "Version=2009-01-06",
  254. 'Nace%2BU3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg%3D'
  255. ),
  256. array(
  257. 'http://ecs.amazonaws.co.uk',
  258. array(
  259. 'Service' => 'AWSECommerceService',
  260. 'AWSAccessKeyId' => '00000000000000000000',
  261. 'Operation' => 'ItemSearch',
  262. 'Actor' => 'Johnny Depp',
  263. 'ResponseGroup' => 'ItemAttributes,Offers,Images,Reviews,Variations',
  264. 'Version' => '2009-01-01',
  265. 'SearchIndex' => 'DVD',
  266. 'Sort' => 'salesrank',
  267. 'AssociateTag' => 'mytag-20',
  268. 'Timestamp' => '2009-01-01T12:00:00Z',
  269. ),
  270. "GET\n".
  271. "ecs.amazonaws.co.uk\n".
  272. "/onca/xml\n".
  273. "AWSAccessKeyId=00000000000000000000&Actor=Johnny%20Depp&Associate".
  274. "Tag=mytag-20&Operation=ItemSearch&ResponseGroup=ItemAttributes%2C".
  275. "Offers%2CImages%2CReviews%2CVariations&SearchIndex=DVD&Service=AW".
  276. "SECommerceService&Sort=salesrank&Timestamp=2009-01-01T12%3A00%3A0".
  277. "0Z&Version=2009-01-01",
  278. 'TuM6E5L9u%2FuNqOX09ET03BXVmHLVFfJIna5cxXuHxiU%3D',
  279. ),
  280. );
  281. }
  282. /**
  283. * Checking if signature Encryption due on August 15th for Amazon Webservice API is working correctly.
  284. *
  285. * @dataProvider dataSignatureEncryption
  286. * @group ZF-7033
  287. */
  288. public function testSignatureEncryption($baseUri, $params, $expectedStringToSign, $expectedSignature)
  289. {
  290. $this->assertEquals(
  291. $expectedStringToSign,
  292. Zend_Service_Amazon::buildRawSignature($baseUri, $params)
  293. );
  294. $this->assertEquals(
  295. $expectedSignature,
  296. rawurlencode(Zend_Service_Amazon::computeSignature(
  297. $baseUri, '1234567890', $params
  298. ))
  299. );
  300. }
  301. /**
  302. * Testing if Amazon service component can handle return values where the
  303. * item-list is not empty
  304. *
  305. * @group ZF-9547
  306. */
  307. public function testAmazonComponentHandlesValidBookResults()
  308. {
  309. $xml = file_get_contents(dirname(__FILE__)."/_files/amazon-response-valid.xml");
  310. $dom = new DOMDocument();
  311. $dom->loadXML($xml);
  312. $result = new Zend_Service_Amazon_ResultSet($dom);
  313. $currentItem = null;
  314. try {
  315. $currentItem = $result->current();
  316. } catch (Zend_Service_Amazon_Exception $e) {
  317. $this->fail('Unexpected exception was triggered');
  318. }
  319. $this->assertTrue($currentItem instanceof Zend_Service_Amazon_Item);
  320. $this->assertEquals('0754512673', $currentItem->ASIN);
  321. }
  322. /**
  323. * Testing if Amazon service component can handle return values where the
  324. * item-list is empty (no results found)
  325. *
  326. * @group ZF-9547
  327. */
  328. public function testAmazonComponentHandlesEmptyBookResults()
  329. {
  330. $xml = file_get_contents(dirname(__FILE__)."/_files/amazon-response-invalid.xml");
  331. $dom = new DOMDocument();
  332. $dom->loadXML($xml);
  333. $result = new Zend_Service_Amazon_ResultSet($dom);
  334. try {
  335. $result->current();
  336. $this->fail('Expected exception was not triggered');
  337. } catch (Zend_Service_Amazon_Exception $e) {
  338. return;
  339. }
  340. }
  341. }