OfflineTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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-2009 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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. */
  53. class Zend_Service_Amazon_OfflineTest extends PHPUnit_Framework_TestCase
  54. {
  55. /**
  56. * Reference to Amazon service consumer object
  57. *
  58. * @var Zend_Service_Amazon
  59. */
  60. protected $_amazon;
  61. /**
  62. * HTTP client adapter for testing
  63. *
  64. * @var Zend_Http_Client_Adapter_Test
  65. */
  66. protected $_httpClientAdapterTest;
  67. /**
  68. * Sets up this test case
  69. *
  70. * @return void
  71. */
  72. public function setUp()
  73. {
  74. $this->_amazon = new Zend_Service_Amazon(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'));
  75. $this->_httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  76. }
  77. /**
  78. * Ensures that __construct() throws an exception when given an invalid country code
  79. *
  80. * @return void
  81. */
  82. public function testConstructExceptionCountryCodeInvalid()
  83. {
  84. try {
  85. $amazon = new Zend_Service_Amazon(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'), 'oops');
  86. $this->fail('Expected Zend_Service_Exception not thrown');
  87. } catch (Zend_Service_Exception $e) {
  88. $this->assertContains('Unknown country code', $e->getMessage());
  89. }
  90. }
  91. /**
  92. * @group ZF-2056
  93. */
  94. public function testMozardSearchFromFile()
  95. {
  96. $xml = file_get_contents(dirname(__FILE__)."/_files/mozart_result.xml");
  97. $dom = new DOMDocument();
  98. $dom->loadXML($xml);
  99. $mozartTracks = array(
  100. 'B00005A8JZ' => '29',
  101. 'B0000058HV' => '25',
  102. 'B000BLI3K2' => '500',
  103. 'B00004X0QF' => '9',
  104. 'B000004194' => '19',
  105. 'B00000I9M0' => '9',
  106. 'B000004166' => '20',
  107. 'B00002DEH1' => '58',
  108. 'B0000041EV' => '12',
  109. 'B00004SA87' => '42',
  110. );
  111. $result = new Zend_Service_Amazon_ResultSet($dom);
  112. foreach($result AS $item) {
  113. $trackCount = $mozartTracks[$item->ASIN];
  114. $this->assertEquals($trackCount, count($item->Tracks));
  115. }
  116. }
  117. /**
  118. * @group ZF-2749
  119. */
  120. public function testSimilarProductConstructorMissingAttributeDoesNotThrowNotice()
  121. {
  122. $dom = new DOMDocument();
  123. $asin = $dom->createElement("ASIN", "TEST");
  124. $product = $dom->createElement("product");
  125. $product->appendChild($asin);
  126. $similarproduct = new Zend_Service_Amazon_SimilarProduct($product);
  127. }
  128. public function dataSignatureEncryption()
  129. {
  130. return array(
  131. array(
  132. 'http://webservices.amazon.com',
  133. array(
  134. 'Service' => 'AWSECommerceService',
  135. 'AWSAccessKeyId' => '00000000000000000000',
  136. 'Operation' => 'ItemLookup',
  137. 'ItemId' => '0679722769',
  138. 'ResponseGroup' => 'ItemAttributes,Offers,Images,Reviews',
  139. 'Version' => '2009-01-06',
  140. 'Timestamp' => '2009-01-01T12:00:00Z',
  141. ),
  142. "GET\n".
  143. "webservices.amazon.com\n".
  144. "/onca/xml\n".
  145. "AWSAccessKeyId=00000000000000000000&ItemId=0679722769&Operation=I".
  146. "temLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReview".
  147. "s&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&".
  148. "Version=2009-01-06",
  149. 'Nace%2BU3Az4OhN7tISqgs1vdLBHBEijWcBeCqL5xN9xg%3D'
  150. ),
  151. array(
  152. 'http://ecs.amazonaws.co.uk',
  153. array(
  154. 'Service' => 'AWSECommerceService',
  155. 'AWSAccessKeyId' => '00000000000000000000',
  156. 'Operation' => 'ItemSearch',
  157. 'Actor' => 'Johnny Depp',
  158. 'ResponseGroup' => 'ItemAttributes,Offers,Images,Reviews,Variations',
  159. 'Version' => '2009-01-01',
  160. 'SearchIndex' => 'DVD',
  161. 'Sort' => 'salesrank',
  162. 'AssociateTag' => 'mytag-20',
  163. 'Timestamp' => '2009-01-01T12:00:00Z',
  164. ),
  165. "GET\n".
  166. "ecs.amazonaws.co.uk\n".
  167. "/onca/xml\n".
  168. "AWSAccessKeyId=00000000000000000000&Actor=Johnny%20Depp&Associate".
  169. "Tag=mytag-20&Operation=ItemSearch&ResponseGroup=ItemAttributes%2C".
  170. "Offers%2CImages%2CReviews%2CVariations&SearchIndex=DVD&Service=AW".
  171. "SECommerceService&Sort=salesrank&Timestamp=2009-01-01T12%3A00%3A0".
  172. "0Z&Version=2009-01-01",
  173. 'TuM6E5L9u%2FuNqOX09ET03BXVmHLVFfJIna5cxXuHxiU%3D',
  174. ),
  175. );
  176. }
  177. /**
  178. * Checking if signature Encryption due on August 15th for Amazon Webservice API is working correctly.
  179. *
  180. * @dataProvider dataSignatureEncryption
  181. * @group ZF-7033
  182. */
  183. public function testSignatureEncryption($baseUri, $params, $expectedStringToSign, $expectedSignature)
  184. {
  185. $this->assertEquals(
  186. $expectedStringToSign,
  187. Zend_Service_Amazon::buildRawSignature($baseUri, $params)
  188. );
  189. $this->assertEquals(
  190. $expectedSignature,
  191. rawurlencode(Zend_Service_Amazon::computeSignature(
  192. $baseUri, '1234567890', $params
  193. ))
  194. );
  195. }
  196. }