OfflineTest.php 12 KB

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