Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

ZF-11137 and ZF-11138: Merged from SVN trunk

fix SimpleDB unit tests:
- Ensured appropriate constant for access key is used
- Longer wait between requests to ensure tests can run
  • Loading branch information...
commit 2344f95d89b2e61feaf0a0a3c6956b211a55dc66 1 parent e1f5420
matthew authored weierophinney committed

Showing 1 changed file with 513 additions and 473 deletions. Show diff stats Hide diff stats

  1. +513 473 tests/Zend/Service/Amazon/SimpleDb/OnlineTest.php
986 tests/Zend/Service/Amazon/SimpleDb/OnlineTest.php
... ... @@ -1,473 +1,513 @@
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\SimpleDb
17   - * @subpackage UnitTests
18   - * @copyright Copyright (c) 2005-2011 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   -/**
24   - * @namespace
25   - */
26   -namespace ZendTest\Service\Amazon\SimpleDb;
27   -
28   -use Zend\Service\Amazon\SimpleDb,
29   - Zend\Service\Amazon\SimpleDb\Exception,
30   - Zend\Http\Client\Adapter\Socket,
31   - Zend\Config\Ini as Config;
32   -
33   -
34   -/**
35   - * @category Zend
36   - * @package Zend\Service\Amazon\SimpleDb
37   - * @subpackage UnitTests
38   - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
39   - * @license http://framework.zend.com/license/new-bsd New BSD License
40   - */
41   -class OnlineTest extends \PHPUnit_Framework_TestCase
42   -{
43   - /**
44   - * Reference to Amazon service consumer object
45   - *
46   - * @var Zend\Service\Amazon\SimpleDb
47   - */
48   - protected $_amazon;
49   -
50   - /**
51   - * Socket based HTTP client adapter
52   - *
53   - * @var Zend\Http\Client\Adapter\Socket
54   - */
55   - protected $_httpClientAdapterSocket;
56   -
57   - protected $_testDomainNamePrefix;
58   -
59   - protected $_testItemNamePrefix;
60   -
61   - protected $_testAttributeNamePrefix;
62   -
63   - // Because Amazon uses an eventual consistency model, this test period may
64   - // help avoid *but not guarantee* false negatives
65   - protected $_testWaitPeriod = 2;
66   -
67   - /**
68   - * Sets up this test case
69   - *
70   - * @return void
71   - */
72   - public function setUp()
73   - {
74   - $this->_amazon = new SimpleDb\SimpleDb(
75   - constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEY'),
76   - constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
77   - );
78   -
79   - $this->_httpClientAdapterSocket = new Socket();
80   -
81   - $this->_amazon->getHttpClient()
82   - ->setAdapter($this->_httpClientAdapterSocket);
83   -
84   - $this->_testDomainNamePrefix = 'TestsZendServiceAmazonSimpleDbDomain';
85   -
86   - $this->_testItemNamePrefix = 'TestsZendServiceAmazonSimpleDbItem';
87   -
88   - $this->_testAttributeNamePrefix = 'TestsZendServiceAmazonSimpleDbAttribute';
89   -
90   - $this->_wait();
91   - }
92   -
93   - public function testGetAttributes() {
94   - $domainName = $this->_testDomainNamePrefix . '_testGetAttributes';
95   - $this->_amazon->deleteDomain($domainName);
96   - $this->_amazon->createDomain($domainName);
97   - try {
98   - $itemName = $this->_testItemNamePrefix . '_testGetAttributes';
99   - $attributeName1 = $this->_testAttributeNamePrefix . '_testGetAttributes1';
100   - $attributeName2 = $this->_testAttributeNamePrefix . '_testGetAttributes2';
101   - $attributeValue1 = 'value1';
102   - $attributeValue2 = 'value2';
103   - $attributes = array(
104   - $attributeName1 => new SimpleDb\Attribute($itemName, $attributeName1, $attributeValue1),
105   - $attributeName2 => new SimpleDb\Attribute($itemName, $attributeName2, $attributeValue2)
106   - );
107   -
108   - // Now that everything's set up, test it
109   - $this->_amazon->putAttributes($domainName, $itemName, $attributes);
110   - $this->_wait();
111   -
112   - // One attribute
113   - $results = $this->_amazon->getAttributes($domainName, $itemName, $attributeName1);
114   - $this->assertEquals(1, count($results));
115   - $attribute = current($results);
116   - $this->assertEquals($attributeName1, $attribute->getName());
117   - $this->assertEquals($attributeValue1, current($attribute->getValues()));
118   -
119   - // Multiple attributes
120   - $results = $this->_amazon->getAttributes($domainName, $itemName);
121   - $this->assertEquals(2, count($results));
122   - $this->assertTrue(array_key_exists($attributeName1, $results));
123   - $this->assertTrue(array_key_exists($attributeName2, $results));
124   - $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
125   - $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
126   -
127   - $this->_amazon->deleteDomain($domainName);
128   - } catch(Exception $e) {
129   - $this->_amazon->deleteDomain($domainName);
130   - throw $e;
131   - }
132   - }
133   -
134   - public function testPutAttributes() {
135   - $domainName = $this->_testDomainNamePrefix . '_testPutAttributes';
136   - $this->_amazon->deleteDomain($domainName);
137   - $this->_amazon->createDomain($domainName);
138   - try {
139   - $itemName = $this->_testItemNamePrefix . '_testPutAttributes';
140   - $attributeName1 = $this->_testAttributeNamePrefix . '_testPutAttributes1';
141   - $attributeName2 = $this->_testAttributeNamePrefix . '_testPutAttributes2';
142   - $attributeValue1 = 'value1';
143   - $attributeValue2 = 'value2';
144   - $attributes = array(
145   - $attributeName1 => new SimpleDb\Attribute($itemName, $attributeName1, $attributeValue1),
146   - $attributeName2 => new SimpleDb\Attribute($itemName, $attributeName2, $attributeValue2)
147   - );
148   -
149   - // Now that everything's set up, test it
150   - $this->_amazon->putAttributes($domainName, $itemName, $attributes);
151   - $this->_wait(); // allow to propagate
152   -
153   - // Multiple attributes
154   - $results = $this->_amazon->getAttributes($domainName, $itemName);
155   - $this->assertEquals(2, count($results));
156   - $this->assertTrue(array_key_exists($attributeName1, $results));
157   - $this->assertTrue(array_key_exists($attributeName2, $results));
158   - $this->assertEquals($attributes[$attributeName1], $results[$attributeName1]);
159   - $this->assertEquals($attributes[$attributeName2], $results[$attributeName2]);
160   - $this->_amazon->deleteDomain($domainName);
161   - } catch(Exception $e) {
162   - $this->_amazon->deleteDomain($domainName);
163   - throw $e;
164   - }
165   - }
166   -
167   - public function testBatchPutAttributes() {
168   - $domainName = $this->_testDomainNamePrefix . '_testBatchPutAttributes';
169   - $this->_amazon->deleteDomain($domainName);
170   - $this->_amazon->createDomain($domainName);
171   - try {
172   - $itemName1 = $this->_testItemNamePrefix . '_testBatchPutAttributes1';
173   - $itemName2 = $this->_testItemNamePrefix . '_testBatchPutAttributes2';
174   - $attributeName1 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes1';
175   - $attributeName2 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes2';
176   - $attributeName3 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes3';
177   - $attributeName4 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes4';
178   - $attributeValue1 = 'value1';
179   - $attributeValue2 = 'value2';
180   - $attributeValue3 = 'value3';
181   - $attributeValue4 = 'value4';
182   - $attributeValue5 = 'value5';
183   - $items = array(
184   - $itemName1 => array(
185   - $attributeName1 => new SimpleDb\Attribute($itemName1, $attributeName1, $attributeValue1),
186   - $attributeName2 => new SimpleDb\Attribute($itemName1, $attributeName2, $attributeValue2)),
187   - $itemName2 => array(
188   - $attributeName3 => new SimpleDb\Attribute($itemName2, $attributeName3, $attributeValue3),
189   - $attributeName4 => new SimpleDb\Attribute($itemName2, $attributeName4, array($attributeValue4, $attributeValue5)))
190   - );
191   -
192   -
193   - $replace = array(
194   - $itemName1 => array(
195   - $attributeName1 => false,
196   - $attributeName2 => false
197   - ),
198   - $itemName2 => array(
199   - $attributeName3 => false,
200   - $attributeName4 => false
201   - )
202   - );
203   -
204   - $this->assertEquals(array(), $this->_amazon->getAttributes($domainName, $itemName1));
205   - $this->_amazon->batchPutAttributes($items, $domainName, $replace);
206   - $this->_wait();
207   -
208   - $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
209   - $this->assertTrue(array_key_exists($attributeName1, $result));
210   - $this->assertEquals($attributeName1, $result[$attributeName1]->getName());
211   - $this->assertEquals($attributeValue1, current($result[$attributeName1]->getValues()));
212   -
213   - $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
214   - $this->assertTrue(array_key_exists($attributeName4, $result));
215   - $this->assertEquals(2, count($result[$attributeName4]->getValues()));
216   -
217   - $result = $this->_amazon->getAttributes($domainName, $itemName2);
218   - $this->assertEquals(2, count($result));
219   - $this->assertTrue(array_key_exists($attributeName3, $result));
220   - $this->assertEquals($attributeName3, $result[$attributeName3]->getName());
221   - $this->assertEquals(1, count($result[$attributeName3]));
222   - $this->assertEquals($attributeValue3, current($result[$attributeName3]->getValues()));
223   - $this->assertTrue(array_key_exists($attributeName4, $result));
224   - $this->assertEquals($attributeName4, $result[$attributeName4]->getName());
225   - $this->assertEquals(2, count($result[$attributeName4]->getValues()));
226   - $this->assertEquals(array($attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
227   -
228   - // Test replace
229   - $newAttributeValue1 = 'newValue1';
230   - $newAttributeValue4 = 'newValue4';
231   - $items[$itemName1][$attributeName1]->setValues(array($newAttributeValue1));
232   - $items[$itemName2][$attributeName4]->setValues(array($newAttributeValue4));
233   -
234   - $this->_amazon->batchPutAttributes($items, $domainName, $replace);
235   - $this->_wait();
236   -
237   - $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
238   - $this->assertEquals(array($newAttributeValue1, $attributeValue1), $result[$attributeName1]->getValues());
239   -
240   - $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
241   - $this->assertEquals(array($newAttributeValue4, $attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
242   -
243   - $replace[$itemName1][$attributeName1] = true;
244   - $replace[$itemName2][$attributeName4] = true;
245   -
246   - $this->_amazon->batchPutAttributes($items, $domainName, $replace);
247   - $this->_wait();
248   -
249   - $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
250   - $this->assertEquals($items[$itemName1][$attributeName1], $result[$attributeName1]);
251   -
252   - $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
253   - $this->assertEquals($items[$itemName2][$attributeName4], $result[$attributeName4]);
254   - $this->assertEquals($items[$itemName1], $this->_amazon->getAttributes($domainName, $itemName1));
255   -
256   - $this->_amazon->deleteDomain($domainName);
257   - } catch(Exception $e) {
258   - $this->_amazon->deleteDomain($domainName);
259   - throw $e;
260   - }
261   - }
262   -
263   - public function testDeleteAttributes() {
264   - $domainName = $this->_testDomainNamePrefix . '_testDeleteAttributes';
265   - $this->_amazon->deleteDomain($domainName);
266   - $this->_amazon->createDomain($domainName);
267   - try {
268   - $itemName = $this->_testItemNamePrefix . '_testDeleteAttributes';
269   - $attributeName1 = $this->_testAttributeNamePrefix . '_testDeleteAttributes1';
270   - $attributeName2 = $this->_testAttributeNamePrefix . '_testDeleteAttributes2';
271   - $attributeName3 = $this->_testAttributeNamePrefix . '_testDeleteAttributes3';
272   - $attributeName4 = $this->_testAttributeNamePrefix . '_testDeleteAttributes4';
273   - $attributeValue1 = 'value1';
274   - $attributeValue2 = 'value2';
275   - $attributeValue3 = 'value3';
276   - $attributeValue4 = 'value4';
277   - $attributes = array(
278   - new SimpleDb\Attribute($itemName, $attributeName1, $attributeValue1),
279   - new SimpleDb\Attribute($itemName, $attributeName2, $attributeValue2),
280   - new SimpleDb\Attribute($itemName, $attributeName3, $attributeValue3),
281   - new SimpleDb\Attribute($itemName, $attributeName4, $attributeValue4)
282   - );
283   -
284   - // Now that everything's set up, test it
285   - $this->_amazon->putAttributes($domainName, $itemName, $attributes);
286   - $this->_wait();
287   -
288   - $results = $this->_amazon->getAttributes($domainName, $itemName);
289   - $this->assertEquals(4, count($results));
290   - $this->assertTrue(array_key_exists($attributeName1, $results));
291   - $this->assertTrue(array_key_exists($attributeName2, $results));
292   - $this->assertTrue(array_key_exists($attributeName3, $results));
293   - $this->assertTrue(array_key_exists($attributeName4, $results));
294   - $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
295   - $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
296   - $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
297   - $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
298   -
299   - $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[0]));
300   - $this->_wait();
301   -
302   - $results = $this->_amazon->getAttributes($domainName, $itemName);
303   - $this->assertEquals(3, count($results));
304   - $this->assertTrue(array_key_exists($attributeName2, $results));
305   - $this->assertTrue(array_key_exists($attributeName3, $results));
306   - $this->assertTrue(array_key_exists($attributeName4, $results));
307   - $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
308   - $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
309   - $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
310   -
311   - $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[1], $attributes[2]));
312   - $this->_wait();
313   -
314   - $results = $this->_amazon->getAttributes($domainName, $itemName);
315   - $this->assertEquals(1, count($results));
316   - $this->assertTrue(array_key_exists($attributeName4, $results));
317   - $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
318   -
319   -
320   - $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[3]));
321   - $this->_wait();
322   -
323   - $results = $this->_amazon->getAttributes($domainName, $itemName);
324   - $this->assertEquals(0, count($results));
325   -
326   - $this->_amazon->deleteDomain($domainName);
327   - } catch(Exception $e) {
328   - $this->_amazon->deleteDomain($domainName);
329   - throw $e;
330   - }
331   - }
332   -
333   - /**
334   - *
335   - * @param $maxNumberOfDomains Integer between 1 and 100
336   - * @param $nextToken Integer between 1 and 100
337   - * @return array 0 or more domain names
338   - */
339   - public function testListDomains() {
340   - $domainName = null;
341   - try {
342   - // Create some domains
343   - for($i = 1; $i <= 3; $i++) {
344   - $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
345   - $this->_amazon->deleteDomain($domainName);
346   - $this->_amazon->createDomain($domainName);
347   - }
348   - $this->_wait();
349   -
350   - $page = $this->_amazon->listDomains(3);
351   - $this->assertEquals(3, count($page->getData()));
352   - // Amazon returns an empty page as the last page :/
353   - $this->assertTrue($page->isLast());
354   - $this->assertEquals(1, count($this->_amazon->listDomains(1, $page->getToken())));
355   -
356   - $page = $this->_amazon->listDomains(4);
357   - $this->assertEquals(3, count($page->getData()));
358   - $this->assertTrue($page->isLast());
359   -
360   - $page = $this->_amazon->listDomains(2);
361   - $this->assertEquals(2, count($page->getData()));
362   - $this->assertFalse($page->isLast());
363   -
364   - $nextPage = $this->_amazon->listDomains(100, $page->getToken());
365   - $this->assertEquals(1, count($nextPage->getData()));
366   - // Amazon returns an empty page as the last page :/
367   - $this->assertTrue($nextPage->isLast());
368   -
369   - // Delete the domains
370   - for($i = 1; $i <= 3; $i++) {
371   - $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
372   - $this->_amazon->deleteDomain($domainName);
373   - }
374   - } catch(Exception $e) {
375   - // Delete the domains
376   - for($i = 1; $i <= 3; $i++) {
377   - $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
378   - $this->_amazon->deleteDomain($domainName);
379   - }
380   - throw $e;
381   - }
382   - }
383   -
384   - /**
385   - * @param $domainName string Name of the domain for which metadata will be requested
386   - * @return array Key/value array of metadatum names and values.
387   - */
388   - public function testDomainMetadata() {
389   - $domainName = $this->_testDomainNamePrefix . '_testDomainMetadata';
390   - $this->_amazon->deleteDomain($domainName);
391   - $this->_amazon->createDomain($domainName);
392   - try {
393   - $metadata = $this->_amazon->domainMetadata($domainName);
394   - $this->assertTrue(is_array($metadata));
395   - $this->assertGreaterThan(0, count($metadata));
396   - $this->assertTrue(array_key_exists('ItemCount', $metadata));
397   - $this->assertEquals(0, (int)$metadata['ItemCount']);
398   - $this->assertTrue(array_key_exists('ItemNamesSizeBytes', $metadata));
399   - $this->assertEquals(0, (int)$metadata['ItemNamesSizeBytes']);
400   - $this->assertTrue(array_key_exists('AttributeNameCount', $metadata));
401   - $this->assertEquals(0, (int)$metadata['AttributeNameCount']);
402   - $this->assertTrue(array_key_exists('AttributeValueCount', $metadata));
403   - $this->assertEquals(0, (int)$metadata['AttributeValueCount']);
404   - $this->assertTrue(array_key_exists('AttributeNamesSizeBytes', $metadata));
405   - $this->assertEquals(0, (int)$metadata['AttributeNamesSizeBytes']);
406   - $this->assertTrue(array_key_exists('AttributeValuesSizeBytes', $metadata));
407   - $this->assertEquals(0, (int)$metadata['AttributeValuesSizeBytes']);
408   - $this->assertTrue(array_key_exists('Timestamp', $metadata));
409   -
410   - // Delete the domain
411   - $this->_amazon->deleteDomain($domainName);
412   - } catch(Exception $e) {
413   - $this->_amazon->deleteDomain($domainName);
414   - throw $e;
415   - }
416   - }
417   -
418   - /**
419   - *
420   - * @param $domainName string Valid domain name of the domain to create
421   - * @return boolean True if successful, false if not
422   - */
423   - public function testCreateDomain() {
424   - $domainName = $this->_testDomainNamePrefix . '_testCreateDomain';
425   - $this->_amazon->deleteDomain($domainName);
426   - $this->_amazon->createDomain($domainName);
427   - try {
428   - $domainListPage = $this->_amazon->listDomains();
429   - $this->assertContains($domainName, $domainListPage->getData());
430   - // Delete the domain
431   - $this->_amazon->deleteDomain($domainName);
432   - } catch(Exception $e) {
433   - $this->_amazon->deleteDomain($domainName);
434   - throw $e;
435   - }
436   - }
437   -
438   - public function testDeleteDomain() {
439   - $domainName = $this->_testDomainNamePrefix . '_testDeleteDomain';
440   - $this->_amazon->deleteDomain($domainName);
441   - $this->_amazon->createDomain($domainName);
442   - try {
443   - $domainListPage = $this->_amazon->listDomains();
444   - $this->assertContains($domainName, $domainListPage->getData());
445   - $this->assertNull($domainListPage->getToken());
446   - // Delete the domain
447   - $this->_amazon->deleteDomain($domainName);
448   - $domainListPage = $this->_amazon->listDomains();
449   - $this->assertNotContains($domainName, $domainListPage->getData());
450   - } catch(Exception $e) {
451   - $this->_amazon->deleteDomain($domainName);
452   - throw $e;
453   - }
454   - }
455   -
456   - private function _wait() {
457   - sleep($this->_testWaitPeriod);
458   - }
459   -
460   - /**
461   - * Tear down the test case
462   - *
463   - * @return void
464   - */
465   - public function tearDown()
466   - {
467   -
468   - // $this->_amazon->deleteDomain($this->_testDomainNamePrefix);
469   - // Delete domains
470   -
471   - unset($this->_amazon);
472   - }
473   -}
  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\SimpleDb
  17 + * @subpackage UnitTests
  18 + * @copyright Copyright (c) 2005-2011 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 +/**
  24 + * @namespace
  25 + */
  26 +namespace ZendTest\Service\Amazon\SimpleDb;
  27 +
  28 +use Zend\Service\Amazon\SimpleDb,
  29 + Zend\Service\Amazon\SimpleDb\Exception,
  30 + Zend\Http\Client\Adapter\Socket,
  31 + Zend\Config\Ini as Config;
  32 +
  33 +
  34 +/**
  35 + * @category Zend
  36 + * @package Zend\Service\Amazon\SimpleDb
  37 + * @subpackage UnitTests
  38 + * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  39 + * @license http://framework.zend.com/license/new-bsd New BSD License
  40 + */
  41 +class OnlineTest extends \PHPUnit_Framework_TestCase
  42 +{
  43 + /**
  44 + * Reference to Amazon service consumer object
  45 + *
  46 + * @var Zend\Service\Amazon\SimpleDb
  47 + */
  48 + protected $_amazon;
  49 +
  50 + /**
  51 + * Socket based HTTP client adapter
  52 + *
  53 + * @var Zend\Http\Client\Adapter\Socket
  54 + */
  55 + protected $_httpClientAdapterSocket;
  56 +
  57 + protected $_testDomainNamePrefix;
  58 +
  59 + protected $_testItemNamePrefix;
  60 +
  61 + protected $_testAttributeNamePrefix;
  62 +
  63 + // Because Amazon uses an eventual consistency model, this test period may
  64 + // help avoid *but not guarantee* false negatives
  65 + protected $_testWaitPeriod = 2;
  66 +
  67 + /**
  68 + * Maximum attempts performed in request()
  69 + *
  70 + * @var int
  71 + */
  72 + protected $_testWaitRetries = 3;
  73 +
  74 + /**
  75 + * Sets up this test case
  76 + *
  77 + * @return void
  78 + */
  79 + public function setUp()
  80 + {
  81 + $this->_amazon = new SimpleDb\SimpleDb(
  82 + constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'),
  83 + constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  84 + );
  85 +
  86 + $this->_httpClientAdapterSocket = new Socket();
  87 +
  88 + $this->_amazon->getHttpClient()
  89 + ->setAdapter($this->_httpClientAdapterSocket);
  90 +
  91 + $this->_testDomainNamePrefix = 'TestsZendServiceAmazonSimpleDbDomain';
  92 +
  93 + $this->_testItemNamePrefix = 'TestsZendServiceAmazonSimpleDbItem';
  94 +
  95 + $this->_testAttributeNamePrefix = 'TestsZendServiceAmazonSimpleDbAttribute';
  96 +
  97 + $this->_wait();
  98 + }
  99 +
  100 + /**
  101 + * Wrapper around remote calls to retry, apply wait, etc.
  102 + *
  103 + * @param string $method SimpleDB method name
  104 + * @param array $args Method argument list
  105 + * @return void
  106 + */
  107 + public function request($method, $args = array())
  108 + {
  109 + $response = null;
  110 + for ($try = 1; $try <= $this->_testWaitRetries; $try++) {
  111 + try {
  112 + $this->_wait();
  113 + $response = call_user_func_array(array($this->_amazon, $method), $args);
  114 + break;
  115 + } catch (Zend_Service_Amazon_SimpleDb_Exception $e) {
  116 + // Only retry after throtte-related error
  117 + if (false === strpos($e->getMessage(), 'currently unavailable')) {
  118 + throw $e;
  119 + }
  120 + }
  121 + }
  122 + return $response;
  123 + }
  124 +
  125 + public function testGetAttributes() {
  126 + $domainName = $this->_testDomainNamePrefix . '_testGetAttributes';
  127 + $this->request('deleteDomain', array($domainName));
  128 + $this->request('createDomain', array($domainName));
  129 + try {
  130 + $itemName = $this->_testItemNamePrefix . '_testGetAttributes';
  131 + $attributeName1 = $this->_testAttributeNamePrefix . '_testGetAttributes1';
  132 + $attributeName2 = $this->_testAttributeNamePrefix . '_testGetAttributes2';
  133 + $attributeValue1 = 'value1';
  134 + $attributeValue2 = 'value2';
  135 + $attributes = array(
  136 + $attributeName1 => new SimpleDb\Attribute($itemName, $attributeName1, $attributeValue1),
  137 + $attributeName2 => new SimpleDb\Attribute($itemName, $attributeName2, $attributeValue2)
  138 + );
  139 +
  140 + // Now that everything's set up, test it
  141 + $this->request('putAttributes', array($domainName, $itemName, $attributes));
  142 +
  143 + // One attribute
  144 + $results = $this->request('getAttributes', array($domainName, $itemName, $attributeName1));
  145 + $this->assertEquals(1, count($results));
  146 + $attribute = current($results);
  147 + $this->assertEquals($attributeName1, $attribute->getName());
  148 + $this->assertEquals($attributeValue1, current($attribute->getValues()));
  149 +
  150 + // Multiple attributes
  151 + $results = $this->request('getAttributes', array($domainName, $itemName));
  152 + $this->assertEquals(2, count($results));
  153 + $this->assertTrue(array_key_exists($attributeName1, $results));
  154 + $this->assertTrue(array_key_exists($attributeName2, $results));
  155 + $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  156 + $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  157 +
  158 + $this->request('deleteDomain', array($domainName));
  159 + } catch(Exception $e) {
  160 + $this->request('deleteDomain', array($domainName));
  161 + throw $e;
  162 + }
  163 + }
  164 +
  165 + public function testPutAttributes() {
  166 + $domainName = $this->_testDomainNamePrefix . '_testPutAttributes';
  167 + $this->request('deleteDomain', array($domainName));
  168 + $this->request('createDomain', array($domainName));
  169 + try {
  170 + $itemName = $this->_testItemNamePrefix . '_testPutAttributes';
  171 + $attributeName1 = $this->_testAttributeNamePrefix . '_testPutAttributes1';
  172 + $attributeName2 = $this->_testAttributeNamePrefix . '_testPutAttributes2';
  173 + $attributeValue1 = 'value1';
  174 + $attributeValue2 = 'value2';
  175 + $attributes = array(
  176 + $attributeName1 => new SimpleDb\Attribute($itemName, $attributeName1, $attributeValue1),
  177 + $attributeName2 => new SimpleDb\Attribute($itemName, $attributeName2, $attributeValue2)
  178 + );
  179 +
  180 + // Now that everything's set up, test it
  181 + $this->request('putAttributes', array($domainName, $itemName, $attributes));
  182 +
  183 + // Multiple attributes
  184 + $results = $this->request('getAttributes', array($domainName, $itemName));
  185 + $this->assertEquals(2, count($results));
  186 + $this->assertTrue(array_key_exists($attributeName1, $results));
  187 + $this->assertTrue(array_key_exists($attributeName2, $results));
  188 + $this->assertEquals($attributes[$attributeName1], $results[$attributeName1]);
  189 + $this->assertEquals($attributes[$attributeName2], $results[$attributeName2]);
  190 + $this->request('deleteDomain', array($domainName));
  191 + } catch(Exception $e) {
  192 + $this->request('deleteDomain', array($domainName));
  193 + throw $e;
  194 + }
  195 + }
  196 +
  197 + public function testBatchPutAttributes() {
  198 + $domainName = $this->_testDomainNamePrefix . '_testBatchPutAttributes';
  199 + $this->request('deleteDomain', array($domainName));
  200 + $this->request('createDomain', array($domainName));
  201 + try {
  202 + $itemName1 = $this->_testItemNamePrefix . '_testBatchPutAttributes1';
  203 + $itemName2 = $this->_testItemNamePrefix . '_testBatchPutAttributes2';
  204 + $attributeName1 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes1';
  205 + $attributeName2 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes2';
  206 + $attributeName3 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes3';
  207 + $attributeName4 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes4';
  208 + $attributeValue1 = 'value1';
  209 + $attributeValue2 = 'value2';
  210 + $attributeValue3 = 'value3';
  211 + $attributeValue4 = 'value4';
  212 + $attributeValue5 = 'value5';
  213 + $items = array(
  214 + $itemName1 => array(
  215 + $attributeName1 => new SimpleDb\Attribute($itemName1, $attributeName1, $attributeValue1),
  216 + $attributeName2 => new SimpleDb\Attribute($itemName1, $attributeName2, $attributeValue2)),
  217 + $itemName2 => array(
  218 + $attributeName3 => new SimpleDb\Attribute($itemName2, $attributeName3, $attributeValue3),
  219 + $attributeName4 => new SimpleDb\Attribute($itemName2, $attributeName4, array($attributeValue4, $attributeValue5)))
  220 + );
  221 +
  222 +
  223 + $replace = array(
  224 + $itemName1 => array(
  225 + $attributeName1 => false,
  226 + $attributeName2 => false
  227 + ),
  228 + $itemName2 => array(
  229 + $attributeName3 => false,
  230 + $attributeName4 => false
  231 + )
  232 + );
  233 +
  234 + $this->assertEquals(array(), $this->request('getAttributes', array($domainName, $itemName1)));
  235 + $this->request('batchPutAttributes', array($items, $domainName, $replace));
  236 +
  237 + $result = $this->request('getAttributes', array($domainName, $itemName1, $attributeName1));
  238 +
  239 + $this->assertTrue(array_key_exists($attributeName1, $result));
  240 + $this->assertEquals($attributeName1, $result[$attributeName1]->getName());
  241 + $this->assertEquals($attributeValue1, current($result[$attributeName1]->getValues()));
  242 + $result = $this->request('getAttributes', array($domainName, $itemName2, $attributeName4));
  243 + $this->assertTrue(array_key_exists($attributeName4, $result));
  244 + $this->assertEquals(2, count($result[$attributeName4]->getValues()));
  245 + $result = $this->request('getAttributes', array($domainName, $itemName2));
  246 + $this->assertEquals(2, count($result));
  247 + $this->assertTrue(array_key_exists($attributeName3, $result));
  248 + $this->assertEquals($attributeName3, $result[$attributeName3]->getName());
  249 + $this->assertEquals(1, count($result[$attributeName3]));
  250 + $this->assertEquals($attributeValue3, current($result[$attributeName3]->getValues()));
  251 + $this->assertTrue(array_key_exists($attributeName4, $result));
  252 + $this->assertEquals($attributeName4, $result[$attributeName4]->getName());
  253 + $this->assertEquals(2, count($result[$attributeName4]->getValues()));
  254 + $this->assertEquals(array($attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  255 +
  256 + // Test replace
  257 + $newAttributeValue1 = 'newValue1';
  258 + $newAttributeValue4 = 'newValue4';
  259 + $items[$itemName1][$attributeName1]->setValues(array($newAttributeValue1));
  260 + $items[$itemName2][$attributeName4]->setValues(array($newAttributeValue4));
  261 +
  262 + $this->request('batchPutAttributes', array($items, $domainName, $replace));
  263 +
  264 + $result = $this->request('getAttributes', array($domainName, $itemName1, $attributeName1));
  265 + $this->assertEquals(array($newAttributeValue1, $attributeValue1), $result[$attributeName1]->getValues());
  266 +
  267 + $result = $this->request('getAttributes', array($domainName, $itemName2, $attributeName4));
  268 + $this->assertEquals(array($newAttributeValue4, $attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  269 +
  270 + $replace[$itemName1][$attributeName1] = true;
  271 + $replace[$itemName2][$attributeName4] = true;
  272 +
  273 + $this->request('batchPutAttributes', array($items, $domainName, $replace));
  274 +
  275 + $result = $this->request('getAttributes', array($domainName, $itemName1, $attributeName1));
  276 + $this->assertEquals($items[$itemName1][$attributeName1], $result[$attributeName1]);
  277 +
  278 + $result = $this->request('getAttributes', array($domainName, $itemName2, $attributeName4));
  279 + $this->assertEquals($items[$itemName2][$attributeName4], $result[$attributeName4]);
  280 + $this->assertEquals($items[$itemName1], $this->request('getAttributes', array($domainName, $itemName1)));
  281 +
  282 + $this->request('deleteDomain', array($domainName));
  283 + } catch(Exception $e) {
  284 + $this->request('deleteDomain', array($domainName));
  285 + throw $e;
  286 + }
  287 + }
  288 +
  289 + public function testDeleteAttributes() {
  290 + $domainName = $this->_testDomainNamePrefix . '_testDeleteAttributes';
  291 + $this->request('deleteDomain', array($domainName));
  292 + $this->request('createDomain', array($domainName));
  293 + try {
  294 + $itemName = $this->_testItemNamePrefix . '_testDeleteAttributes';
  295 + $attributeName1 = $this->_testAttributeNamePrefix . '_testDeleteAttributes1';
  296 + $attributeName2 = $this->_testAttributeNamePrefix . '_testDeleteAttributes2';
  297 + $attributeName3 = $this->_testAttributeNamePrefix . '_testDeleteAttributes3';
  298 + $attributeName4 = $this->_testAttributeNamePrefix . '_testDeleteAttributes4';
  299 + $attributeValue1 = 'value1';
  300 + $attributeValue2 = 'value2';
  301 + $attributeValue3 = 'value3';
  302 + $attributeValue4 = 'value4';
  303 + $attributes = array(
  304 + new SimpleDb\Attribute($itemName, $attributeName1, $attributeValue1),
  305 + new SimpleDb\Attribute($itemName, $attributeName2, $attributeValue2),
  306 + new SimpleDb\Attribute($itemName, $attributeName3, $attributeValue3),
  307 + new SimpleDb\Attribute($itemName, $attributeName4, $attributeValue4)
  308 + );
  309 +
  310 + // Now that everything's set up, test it
  311 + $this->request('putAttributes', array($domainName, $itemName, $attributes));
  312 +
  313 + $results = $this->request('getAttributes', array($domainName, $itemName));
  314 + $this->assertEquals(4, count($results));
  315 + $this->assertTrue(array_key_exists($attributeName1, $results));
  316 + $this->assertTrue(array_key_exists($attributeName2, $results));
  317 + $this->assertTrue(array_key_exists($attributeName3, $results));
  318 + $this->assertTrue(array_key_exists($attributeName4, $results));
  319 + $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  320 + $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  321 + $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  322 + $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  323 +
  324 + $this->request('deleteAttributes', array($domainName, $itemName, array($attributes[0])));
  325 +
  326 + $results = $this->request('getAttributes', array($domainName, $itemName));
  327 + $this->assertEquals(3, count($results));
  328 + $this->assertTrue(array_key_exists($attributeName2, $results));
  329 + $this->assertTrue(array_key_exists($attributeName3, $results));
  330 + $this->assertTrue(array_key_exists($attributeName4, $results));
  331 + $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  332 + $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  333 + $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  334 +
  335 + $this->request('deleteAttributes', array($domainName, $itemName, array($attributes[1], $attributes[2])));
  336 +
  337 + $results = $this->request('getAttributes', array($domainName, $itemName));
  338 + $this->assertEquals(1, count($results));
  339 + $this->assertTrue(array_key_exists($attributeName4, $results));
  340 + $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  341 +
  342 +
  343 + $this->request('deleteAttributes', array($domainName, $itemName, array($attributes[3])));
  344 +
  345 + $results = $this->request('getAttributes', array($domainName, $itemName));
  346 + $this->assertEquals(0, count($results));
  347 +
  348 + $this->request('deleteDomain', array($domainName));
  349 + } catch(Exception $e) {
  350 + $this->request('deleteDomain', array($domainName));
  351 + throw $e;
  352 + }
  353 + }
  354 +
  355 + /**
  356 + *
  357 + * @param $maxNumberOfDomains Integer between 1 and 100
  358 + * @param $nextToken Integer between 1 and 100
  359 + * @return array 0 or more domain names
  360 + */
  361 + public function testListDomains() {
  362 + $domainName = null;
  363 + try {
  364 + // Create some domains
  365 + for($i = 1; $i <= 3; $i++) {
  366 + $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  367 + $this->request('deleteDomain', array($domainName));
  368 + $this->request('createDomain', array($domainName));
  369 + }
  370 +
  371 + $page = $this->request('listDomains', array(3));
  372 + $this->assertEquals(3, count($page->getData()));
  373 + // Amazon returns an empty page as the last page :/
  374 + $isLast = $page->isLast();
  375 + if (!$isLast) {
  376 + // The old isLast() assertTrue failed in full suite runs. Token often
  377 + // decodes to 'TestsZendServiceAmazonSimpleDbDomain_testPutAttributes'
  378 + // which no longer exists. Instead of a plain assertTrue, which seemed
  379 + // to pass only in single-case runs, we'll make sure the token's
  380 + // presence is worth a negative.
  381 + $token = $page->getToken();
  382 + if ($token) {
  383 + $tokenDomainName = base64_decode($token);
  384 + if (false !== strpos($tokenDomainName, $this->_testDomainNamePrefix)) {
  385 + try {
  386 + $this->request('domainMetadata', array($tokenDomainName));
  387 + $this->fail('listDomains call with 3 domain maximum did not return last page');
  388 + } catch (Exception $e) {
  389 + $this->assertContains('The specified domain does not exist', $e->getMessage());
  390 + }
  391 + }
  392 + }
  393 + }
  394 + $this->assertEquals(1, count($this->request('listDomains', array(1, $page->getToken()))));
  395 +
  396 + $page = $this->request('listDomains', array(4));
  397 + $this->assertEquals(3, count($page->getData()));
  398 + $this->assertTrue($page->isLast());
  399 +
  400 + $page = $this->request('listDomains', array(2));
  401 + $this->assertEquals(2, count($page->getData()));
  402 + $this->assertFalse($page->isLast());
  403 +
  404 + $nextPage = $this->request('listDomains', array(100, $page->getToken()));
  405 + $this->assertEquals(1, count($nextPage->getData()));
  406 + // Amazon returns an empty page as the last page :/
  407 + $this->assertTrue($nextPage->isLast());
  408 +
  409 + // Delete the domains
  410 + for($i = 1; $i <= 3; $i++) {
  411 + $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  412 + $this->request('deleteDomain', array($domainName));
  413 + }
  414 + } catch(Exception $e) {
  415 + // Delete the domains
  416 + for($i = 1; $i <= 3; $i++) {
  417 + $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  418 + $this->request('deleteDomain', array($domainName));
  419 + }
  420 + throw $e;
  421 + }
  422 + }
  423 +
  424 + /**
  425 + * @param $domainName string Name of the domain for which metadata will be requested
  426 + * @return array Key/value array of metadatum names and values.
  427 + */
  428 + public function testDomainMetadata() {
  429 + $domainName = $this->_testDomainNamePrefix . '_testDomainMetadata';
  430 + $this->request('deleteDomain', array($domainName));
  431 + $this->request('createDomain', array($domainName));
  432 + try {
  433 + $metadata = $this->request('domainMetadata', array($domainName));
  434 + $this->assertTrue(is_array($metadata));
  435 + $this->assertGreaterThan(0, count($metadata));
  436 + $this->assertTrue(array_key_exists('ItemCount', $metadata));
  437 + $this->assertEquals(0, (int)$metadata['ItemCount']);
  438 + $this->assertTrue(array_key_exists('ItemNamesSizeBytes', $metadata));
  439 + $this->assertEquals(0, (int)$metadata['ItemNamesSizeBytes']);
  440 + $this->assertTrue(array_key_exists('AttributeNameCount', $metadata));
  441 + $this->assertEquals(0, (int)$metadata['AttributeNameCount']);
  442 + $this->assertTrue(array_key_exists('AttributeValueCount', $metadata));
  443 + $this->assertEquals(0, (int)$metadata['AttributeValueCount']);
  444 + $this->assertTrue(array_key_exists('AttributeNamesSizeBytes', $metadata));
  445 + $this->assertEquals(0, (int)$metadata['AttributeNamesSizeBytes']);
  446 + $this->assertTrue(array_key_exists('AttributeValuesSizeBytes', $metadata));
  447 + $this->assertEquals(0, (int)$metadata['AttributeValuesSizeBytes']);
  448 + $this->assertTrue(array_key_exists('Timestamp', $metadata));
  449 +
  450 + // Delete the domain
  451 + $this->request('deleteDomain', array($domainName));
  452 + } catch(Exception $e) {
  453 + $this->request('deleteDomain', array($domainName));
  454 + throw $e;
  455 + }
  456 + }
  457 +
  458 + /**
  459 + *
  460 + * @param $domainName string Valid domain name of the domain to create
  461 + * @return boolean True if successful, false if not
  462 + */
  463 + public function testCreateDomain() {
  464 + $domainName = $this->_testDomainNamePrefix . '_testCreateDomain';
  465 + $this->request('deleteDomain', array($domainName));
  466 + $this->request('createDomain', array($domainName));
  467 + try {
  468 + $domainListPage = $this->request('listDomains');
  469 + $this->assertContains($domainName, $domainListPage->getData());
  470 + // Delete the domain
  471 + $this->request('deleteDomain', array($domainName));
  472 + } catch(Exception $e) {
  473 + $this->request('deleteDomain', array($domainName));
  474 + throw $e;
  475 + }
  476 + }
  477 +
  478 + public function testDeleteDomain() {
  479 + $domainName = $this->_testDomainNamePrefix . '_testDeleteDomain';
  480 + $this->request('deleteDomain', array($domainName));
  481 + $this->request('createDomain', array($domainName));
  482 + try {
  483 + $domainListPage = $this->request('listDomains');
  484 + $this->assertContains($domainName, $domainListPage->getData());
  485 + $this->assertNull($domainListPage->getToken());
  486 + // Delete the domain
  487 + $this->request('deleteDomain', array($domainName));
  488 + $domainListPage = $this->request('listDomains');
  489 + $this->assertNotContains($domainName, $domainListPage->getData());
  490 + } catch(Exception $e) {
  491 + $this->request('deleteDomain', array($domainName));
  492 + throw $e;
  493 + }
  494 + }
  495 +
  496 + private function _wait() {
  497 + sleep($this->_testWaitPeriod);
  498 + }
  499 +
  500 + /**
  501 + * Tear down the test case
  502 + *
  503 + * @return void
  504 + */
  505 + public function tearDown()
  506 + {
  507 +
  508 + // $this->request('deleteDomain', array($this->_testDomainNamePrefix));
  509 + // Delete domains
  510 +
  511 + unset($this->_amazon);
  512 + }
  513 +}

0 comments on commit 2344f95

Please sign in to comment.
Something went wrong with that request. Please try again.