OnlineTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: OnlineTest.php 11973 2008-10-15 16:00:56Z matthew $
  21. */
  22. /**
  23. * @see Zend_Service_Amazon_SimpleDb
  24. */
  25. require_once 'Zend/Service/Amazon/SimpleDb.php';
  26. /**
  27. * @see Zend_Service_Amazon_SimpleDb_Attribute
  28. */
  29. require_once 'Zend/Service/Amazon/SimpleDb/Attribute.php';
  30. /**
  31. * @see Zend_Service_Amazon_SimpleDb_Page
  32. */
  33. require_once 'Zend/Service/Amazon/SimpleDb/Page.php';
  34. /**
  35. * @see Zend_Http_Client_Adapter_Socket
  36. */
  37. require_once 'Zend/Http/Client/Adapter/Socket.php';
  38. /**
  39. * @see Zend_Config_Ini
  40. */
  41. require_once 'Zend/Config/Ini.php';
  42. /**
  43. * @category Zend
  44. * @package Zend_Service_Amazon_SimpleDb
  45. * @subpackage UnitTests
  46. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. */
  49. class Zend_Service_Amazon_SimpleDb_OnlineTest extends PHPUnit_Framework_TestCase
  50. {
  51. /**
  52. * Reference to Amazon service consumer object
  53. *
  54. * @var Zend_Service_Amazon_SimpleDb
  55. */
  56. protected $_amazon;
  57. /**
  58. * Socket based HTTP client adapter
  59. *
  60. * @var Zend_Http_Client_Adapter_Socket
  61. */
  62. protected $_httpClientAdapterSocket;
  63. protected $_testDomainNamePrefix;
  64. protected $_testItemNamePrefix;
  65. protected $_testAttributeNamePrefix;
  66. // Because Amazon uses an eventual consistency model, this test period may
  67. // help avoid *but not guarantee* false negatives
  68. protected $_testWaitPeriod = 2;
  69. /**
  70. * Sets up this test case
  71. *
  72. * @return void
  73. */
  74. public function setUp()
  75. {
  76. $this->_amazon = new Zend_Service_Amazon_SimpleDb(
  77. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEY'),
  78. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  79. );
  80. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  81. $this->_amazon->getHttpClient()
  82. ->setAdapter($this->_httpClientAdapterSocket);
  83. $this->_testDomainNamePrefix = 'TestsZendServiceAmazonSimpleDbDomain';
  84. $this->_testItemNamePrefix = 'TestsZendServiceAmazonSimpleDbItem';
  85. $this->_testAttributeNamePrefix = 'TestsZendServiceAmazonSimpleDbAttribute';
  86. $this->_wait();
  87. }
  88. public function testGetAttributes() {
  89. $domainName = $this->_testDomainNamePrefix . '_testGetAttributes';
  90. $this->_amazon->deleteDomain($domainName);
  91. $this->_amazon->createDomain($domainName);
  92. try {
  93. $itemName = $this->_testItemNamePrefix . '_testGetAttributes';
  94. $attributeName1 = $this->_testAttributeNamePrefix . '_testGetAttributes1';
  95. $attributeName2 = $this->_testAttributeNamePrefix . '_testGetAttributes2';
  96. $attributeValue1 = 'value1';
  97. $attributeValue2 = 'value2';
  98. $attributes = array(
  99. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  100. $attributeName2 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2)
  101. );
  102. // Now that everything's set up, test it
  103. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  104. $this->_wait();
  105. // One attribute
  106. $results = $this->_amazon->getAttributes($domainName, $itemName, $attributeName1);
  107. $this->assertEquals(1, count($results));
  108. $attribute = current($results);
  109. $this->assertEquals($attributeName1, $attribute->getName());
  110. $this->assertEquals($attributeValue1, current($attribute->getValues()));
  111. // Multiple attributes
  112. $results = $this->_amazon->getAttributes($domainName, $itemName);
  113. $this->assertEquals(2, count($results));
  114. $this->assertTrue(array_key_exists($attributeName1, $results));
  115. $this->assertTrue(array_key_exists($attributeName2, $results));
  116. $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  117. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  118. $this->_amazon->deleteDomain($domainName);
  119. } catch(Exception $e) {
  120. $this->_amazon->deleteDomain($domainName);
  121. throw $e;
  122. }
  123. }
  124. public function testPutAttributes() {
  125. $domainName = $this->_testDomainNamePrefix . '_testPutAttributes';
  126. $this->_amazon->deleteDomain($domainName);
  127. $this->_amazon->createDomain($domainName);
  128. try {
  129. $itemName = $this->_testItemNamePrefix . '_testPutAttributes';
  130. $attributeName1 = $this->_testAttributeNamePrefix . '_testPutAttributes1';
  131. $attributeName2 = $this->_testAttributeNamePrefix . '_testPutAttributes2';
  132. $attributeValue1 = 'value1';
  133. $attributeValue2 = 'value2';
  134. $attributes = array(
  135. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  136. $attributeName2 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2)
  137. );
  138. // Now that everything's set up, test it
  139. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  140. $this->_wait();
  141. // Multiple attributes
  142. $results = $this->_amazon->getAttributes($domainName, $itemName);
  143. $this->assertEquals(2, count($results));
  144. $this->assertTrue(array_key_exists($attributeName1, $results));
  145. $this->assertTrue(array_key_exists($attributeName2, $results));
  146. $this->assertEquals($attributes[$attributeName1], $results[$attributeName1]);
  147. $this->assertEquals($attributes[$attributeName2], $results[$attributeName2]);
  148. $this->_amazon->deleteDomain($domainName);
  149. } catch(Exception $e) {
  150. $this->_amazon->deleteDomain($domainName);
  151. throw $e;
  152. }
  153. }
  154. public function testBatchPutAttributes() {
  155. $domainName = $this->_testDomainNamePrefix . '_testBatchPutAttributes';
  156. $this->_amazon->deleteDomain($domainName);
  157. $this->_amazon->createDomain($domainName);
  158. try {
  159. $itemName1 = $this->_testItemNamePrefix . '_testBatchPutAttributes1';
  160. $itemName2 = $this->_testItemNamePrefix . '_testBatchPutAttributes2';
  161. $attributeName1 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes1';
  162. $attributeName2 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes2';
  163. $attributeName3 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes3';
  164. $attributeName4 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes4';
  165. $attributeValue1 = 'value1';
  166. $attributeValue2 = 'value2';
  167. $attributeValue3 = 'value3';
  168. $attributeValue4 = 'value4';
  169. $attributeValue5 = 'value5';
  170. $items = array(
  171. $itemName1 => array(
  172. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName1, $attributeName1, $attributeValue1),
  173. $attributeName2 =>new Zend_Service_Amazon_SimpleDb_Attribute($itemName1, $attributeName2, $attributeValue2)),
  174. $itemName2 => array(
  175. $attributeName3 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName2, $attributeName3, $attributeValue3),
  176. $attributeName4 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName2, $attributeName4, array($attributeValue4, $attributeValue5)))
  177. );
  178. $replace = array(
  179. $itemName1 => array(
  180. $attributeName1 => false,
  181. $attributeName2 => false
  182. ),
  183. $itemName2 => array(
  184. $attributeName3 => false,
  185. $attributeName4 => false
  186. )
  187. );
  188. $this->assertEquals(array(), $this->_amazon->getAttributes($domainName, $itemName1));
  189. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  190. $this->_wait();
  191. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  192. $this->assertTrue(array_key_exists($attributeName1, $result));
  193. $this->assertEquals($attributeName1, $result[$attributeName1]->getName());
  194. $this->assertEquals($attributeValue1, current($result[$attributeName1]->getValues()));
  195. $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
  196. $this->assertTrue(array_key_exists($attributeName4, $result));
  197. $this->assertEquals(2, count($result[$attributeName4]->getValues()));
  198. $result = $this->_amazon->getAttributes($domainName, $itemName2);
  199. $this->assertEquals(2, count($result));
  200. $this->assertTrue(array_key_exists($attributeName3, $result));
  201. $this->assertEquals($attributeName3, $result[$attributeName3]->getName());
  202. $this->assertEquals(1, count($result[$attributeName3]));
  203. $this->assertEquals($attributeValue3, current($result[$attributeName3]->getValues()));
  204. $this->assertTrue(array_key_exists($attributeName4, $result));
  205. $this->assertEquals($attributeName4, $result[$attributeName4]->getName());
  206. $this->assertEquals(2, count($result[$attributeName4]->getValues()));
  207. $this->assertEquals(array($attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  208. // Test replace
  209. $newAttributeValue1 = 'newValue1';
  210. $newAttributeValue4 = 'newValue4';
  211. $items[$itemName1][$attributeName1]->setValues(array($newAttributeValue1));
  212. $items[$itemName2][$attributeName4]->setValues(array($newAttributeValue4));
  213. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  214. $this->_wait();
  215. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  216. $this->assertEquals(array($newAttributeValue1, $attributeValue1), $result[$attributeName1]->getValues());
  217. $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
  218. $this->assertEquals(array($newAttributeValue4, $attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  219. $replace[$itemName1][$attributeName1] = true;
  220. $replace[$itemName2][$attributeName4] = true;
  221. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  222. $this->_wait();
  223. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  224. $this->assertEquals($items[$itemName1][$attributeName1], $result[$attributeName1]);
  225. $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
  226. $this->assertEquals($items[$itemName2][$attributeName4], $result[$attributeName4]);
  227. $this->assertEquals($items[$itemName1], $this->_amazon->getAttributes($domainName, $itemName1));
  228. $this->_amazon->deleteDomain($domainName);
  229. } catch(Exception $e) {
  230. $this->_amazon->deleteDomain($domainName);
  231. throw $e;
  232. }
  233. }
  234. public function testDeleteAttributes() {
  235. $domainName = $this->_testDomainNamePrefix . '_testDeleteAttributes';
  236. $this->_amazon->deleteDomain($domainName);
  237. $this->_amazon->createDomain($domainName);
  238. try {
  239. $itemName = $this->_testItemNamePrefix . '_testDeleteAttributes';
  240. $attributeName1 = $this->_testAttributeNamePrefix . '_testDeleteAttributes1';
  241. $attributeName2 = $this->_testAttributeNamePrefix . '_testDeleteAttributes2';
  242. $attributeName3 = $this->_testAttributeNamePrefix . '_testDeleteAttributes3';
  243. $attributeName4 = $this->_testAttributeNamePrefix . '_testDeleteAttributes4';
  244. $attributeValue1 = 'value1';
  245. $attributeValue2 = 'value2';
  246. $attributeValue3 = 'value3';
  247. $attributeValue4 = 'value4';
  248. $attributes = array(
  249. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  250. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2),
  251. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName3, $attributeValue3),
  252. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName4, $attributeValue4)
  253. );
  254. // Now that everything's set up, test it
  255. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  256. $this->_wait();
  257. $results = $this->_amazon->getAttributes($domainName, $itemName);
  258. $this->assertEquals(4, count($results));
  259. $this->assertTrue(array_key_exists($attributeName1, $results));
  260. $this->assertTrue(array_key_exists($attributeName2, $results));
  261. $this->assertTrue(array_key_exists($attributeName3, $results));
  262. $this->assertTrue(array_key_exists($attributeName4, $results));
  263. $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  264. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  265. $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  266. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  267. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[0]));
  268. $this->_wait();
  269. $results = $this->_amazon->getAttributes($domainName, $itemName);
  270. $this->assertEquals(3, count($results));
  271. $this->assertTrue(array_key_exists($attributeName2, $results));
  272. $this->assertTrue(array_key_exists($attributeName3, $results));
  273. $this->assertTrue(array_key_exists($attributeName4, $results));
  274. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  275. $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  276. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  277. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[1], $attributes[2]));
  278. $this->_wait();
  279. $results = $this->_amazon->getAttributes($domainName, $itemName);
  280. $this->assertEquals(1, count($results));
  281. $this->assertTrue(array_key_exists($attributeName4, $results));
  282. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  283. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[3]));
  284. $this->_wait();
  285. $results = $this->_amazon->getAttributes($domainName, $itemName);
  286. $this->assertEquals(0, count($results));
  287. $this->_amazon->deleteDomain($domainName);
  288. } catch(Exception $e) {
  289. $this->_amazon->deleteDomain($domainName);
  290. throw $e;
  291. }
  292. }
  293. public function testListDomains() {
  294. $domainName = null;
  295. try {
  296. // Create some domains
  297. for($i = 1; $i <= 3; $i++) {
  298. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  299. $this->_amazon->deleteDomain($domainName);
  300. $this->_amazon->createDomain($domainName);
  301. }
  302. $this->_wait();
  303. $page = $this->_amazon->listDomains(3);
  304. $this->assertEquals(3, count($page->getData()));
  305. // Amazon returns an empty page as the last page :/
  306. $this->assertTrue($page->isLast());
  307. $this->assertEquals(1, count($this->_amazon->listDomains(1, $page->getToken())));
  308. $page = $this->_amazon->listDomains(4);
  309. $this->assertEquals(3, count($page->getData()));
  310. $this->assertTrue($page->isLast());
  311. $page = $this->_amazon->listDomains(2);
  312. $this->assertEquals(2, count($page->getData()));
  313. $this->assertFalse($page->isLast());
  314. $nextPage = $this->_amazon->listDomains(100, $page->getToken());
  315. $this->assertEquals(1, count($nextPage->getData()));
  316. // Amazon returns an empty page as the last page :/
  317. $this->assertTrue($nextPage->isLast());
  318. // Delete the domains
  319. for($i = 1; $i <= 3; $i++) {
  320. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  321. $this->_amazon->deleteDomain($domainName);
  322. }
  323. } catch(Exception $e) {
  324. // Delete the domains
  325. for($i = 1; $i <= 3; $i++) {
  326. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  327. $this->_amazon->deleteDomain($domainName);
  328. }
  329. throw $e;
  330. }
  331. }
  332. public function testDomainMetadata() {
  333. $domainName = $this->_testDomainNamePrefix . '_testDomainMetadata';
  334. $this->_amazon->deleteDomain($domainName);
  335. $this->_amazon->createDomain($domainName);
  336. try {
  337. $metadata = $this->_amazon->domainMetadata($domainName);
  338. $this->assertTrue(is_array($metadata));
  339. $this->assertGreaterThan(0, count($metadata));
  340. $this->assertTrue(array_key_exists('ItemCount', $metadata));
  341. $this->assertEquals(0, (int)$metadata['ItemCount']);
  342. $this->assertTrue(array_key_exists('ItemNamesSizeBytes', $metadata));
  343. $this->assertEquals(0, (int)$metadata['ItemNamesSizeBytes']);
  344. $this->assertTrue(array_key_exists('AttributeNameCount', $metadata));
  345. $this->assertEquals(0, (int)$metadata['AttributeNameCount']);
  346. $this->assertTrue(array_key_exists('AttributeValueCount', $metadata));
  347. $this->assertEquals(0, (int)$metadata['AttributeValueCount']);
  348. $this->assertTrue(array_key_exists('AttributeNamesSizeBytes', $metadata));
  349. $this->assertEquals(0, (int)$metadata['AttributeNamesSizeBytes']);
  350. $this->assertTrue(array_key_exists('AttributeValuesSizeBytes', $metadata));
  351. $this->assertEquals(0, (int)$metadata['AttributeValuesSizeBytes']);
  352. $this->assertTrue(array_key_exists('Timestamp', $metadata));
  353. // Delete the domain
  354. $this->_amazon->deleteDomain($domainName);
  355. } catch(Exception $e) {
  356. $this->_amazon->deleteDomain($domainName);
  357. throw $e;
  358. }
  359. }
  360. public function testCreateDomain() {
  361. $domainName = $this->_testDomainNamePrefix . '_testCreateDomain';
  362. $this->_amazon->deleteDomain($domainName);
  363. $this->_amazon->createDomain($domainName);
  364. try {
  365. $domainListPage = $this->_amazon->listDomains();
  366. $this->assertContains($domainName, $domainListPage->getData());
  367. // Delete the domain
  368. $this->_amazon->deleteDomain($domainName);
  369. } catch(Exception $e) {
  370. $this->_amazon->deleteDomain($domainName);
  371. throw $e;
  372. }
  373. }
  374. public function testDeleteDomain() {
  375. $domainName = $this->_testDomainNamePrefix . '_testDeleteDomain';
  376. $this->_amazon->deleteDomain($domainName);
  377. $this->_amazon->createDomain($domainName);
  378. try {
  379. $domainListPage = $this->_amazon->listDomains();
  380. $this->assertContains($domainName, $domainListPage->getData());
  381. $this->assertNull($domainListPage->getToken());
  382. // Delete the domain
  383. $this->_amazon->deleteDomain($domainName);
  384. $domainListPage = $this->_amazon->listDomains();
  385. $this->assertNotContains($domainName, $domainListPage->getData());
  386. } catch(Exception $e) {
  387. $this->_amazon->deleteDomain($domainName);
  388. throw $e;
  389. }
  390. }
  391. private function _wait() {
  392. sleep($this->_testWaitPeriod);
  393. }
  394. /**
  395. * Tear down the test case
  396. *
  397. * @return void
  398. */
  399. public function tearDown()
  400. {
  401. // $this->_amazon->deleteDomain($this->_testDomainNamePrefix);
  402. // Delete domains
  403. unset($this->_amazon);
  404. }
  405. }