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