PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Service/Amazon/OfflineTest.php

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