OnlineTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  26. /**
  27. * @see Zend_Service_Amazon_SimpleDb
  28. */
  29. require_once 'Zend/Service/Amazon/SimpleDb.php';
  30. /**
  31. * @see Zend_Service_Amazon_SimpleDb_Attribute
  32. */
  33. require_once 'Zend/Service/Amazon/SimpleDb/Attribute.php';
  34. /**
  35. * @see Zend_Service_Amazon_SimpleDb_Page
  36. */
  37. require_once 'Zend/Service/Amazon/SimpleDb/Page.php';
  38. /**
  39. * @see Zend_Http_Client_Adapter_Socket
  40. */
  41. require_once 'Zend/Http/Client/Adapter/Socket.php';
  42. /**
  43. * @see Zend_Config_Ini
  44. */
  45. require_once 'Zend/Config/Ini.php';
  46. /**
  47. * @category Zend
  48. * @package Zend_Service_Amazon_SimpleDb
  49. * @subpackage UnitTests
  50. * @copyright Copyright (c) 2005-2010 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_SimpleDb_OnlineTest extends PHPUnit_Framework_TestCase
  54. {
  55. /**
  56. * Reference to Amazon service consumer object
  57. *
  58. * @var Zend_Service_Amazon_SimpleDb
  59. */
  60. protected $_amazon;
  61. /**
  62. * Socket based HTTP client adapter
  63. *
  64. * @var Zend_Http_Client_Adapter_Socket
  65. */
  66. protected $_httpClientAdapterSocket;
  67. protected $_testDomainNamePrefix;
  68. protected $_testItemNamePrefix;
  69. protected $_testAttributeNamePrefix;
  70. // Because Amazon uses an eventual consistency model, this test period may
  71. // help avoid *but not guarantee* false negatives
  72. protected $_testWaitPeriod = 2;
  73. /**
  74. * Sets up this test case
  75. *
  76. * @return void
  77. */
  78. public function setUp()
  79. {
  80. $this->_amazon = new Zend_Service_Amazon_SimpleDb(
  81. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEY'),
  82. constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEY')
  83. );
  84. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  85. $this->_amazon->getHttpClient()
  86. ->setAdapter($this->_httpClientAdapterSocket);
  87. $this->_testDomainNamePrefix = 'TestsZendServiceAmazonSimpleDbDomain';
  88. $this->_testItemNamePrefix = 'TestsZendServiceAmazonSimpleDbItem';
  89. $this->_testAttributeNamePrefix = 'TestsZendServiceAmazonSimpleDbAttribute';
  90. $this->_wait();
  91. }
  92. public function testGetAttributes() {
  93. $domainName = $this->_testDomainNamePrefix . '_testGetAttributes';
  94. $this->_amazon->deleteDomain($domainName);
  95. $this->_amazon->createDomain($domainName);
  96. try {
  97. $itemName = $this->_testItemNamePrefix . '_testGetAttributes';
  98. $attributeName1 = $this->_testAttributeNamePrefix . '_testGetAttributes1';
  99. $attributeName2 = $this->_testAttributeNamePrefix . '_testGetAttributes2';
  100. $attributeValue1 = 'value1';
  101. $attributeValue2 = 'value2';
  102. $attributes = array(
  103. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  104. $attributeName2 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2)
  105. );
  106. // Now that everything's set up, test it
  107. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  108. $this->_wait();
  109. // One attribute
  110. $results = $this->_amazon->getAttributes($domainName, $itemName, $attributeName1);
  111. $this->assertEquals(1, count($results));
  112. $attribute = current($results);
  113. $this->assertEquals($attributeName1, $attribute->getName());
  114. $this->assertEquals($attributeValue1, current($attribute->getValues()));
  115. // Multiple attributes
  116. $results = $this->_amazon->getAttributes($domainName, $itemName);
  117. $this->assertEquals(2, count($results));
  118. $this->assertTrue(array_key_exists($attributeName1, $results));
  119. $this->assertTrue(array_key_exists($attributeName2, $results));
  120. $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  121. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  122. $this->_amazon->deleteDomain($domainName);
  123. } catch(Exception $e) {
  124. $this->_amazon->deleteDomain($domainName);
  125. throw $e;
  126. }
  127. }
  128. public function testPutAttributes() {
  129. $domainName = $this->_testDomainNamePrefix . '_testPutAttributes';
  130. $this->_amazon->deleteDomain($domainName);
  131. $this->_amazon->createDomain($domainName);
  132. try {
  133. $itemName = $this->_testItemNamePrefix . '_testPutAttributes';
  134. $attributeName1 = $this->_testAttributeNamePrefix . '_testPutAttributes1';
  135. $attributeName2 = $this->_testAttributeNamePrefix . '_testPutAttributes2';
  136. $attributeValue1 = 'value1';
  137. $attributeValue2 = 'value2';
  138. $attributes = array(
  139. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  140. $attributeName2 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2)
  141. );
  142. // Now that everything's set up, test it
  143. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  144. $this->_wait();
  145. // Multiple attributes
  146. $results = $this->_amazon->getAttributes($domainName, $itemName);
  147. $this->assertEquals(2, count($results));
  148. $this->assertTrue(array_key_exists($attributeName1, $results));
  149. $this->assertTrue(array_key_exists($attributeName2, $results));
  150. $this->assertEquals($attributes[$attributeName1], $results[$attributeName1]);
  151. $this->assertEquals($attributes[$attributeName2], $results[$attributeName2]);
  152. $this->_amazon->deleteDomain($domainName);
  153. } catch(Exception $e) {
  154. $this->_amazon->deleteDomain($domainName);
  155. throw $e;
  156. }
  157. }
  158. public function testBatchPutAttributes() {
  159. $domainName = $this->_testDomainNamePrefix . '_testBatchPutAttributes';
  160. $this->_amazon->deleteDomain($domainName);
  161. $this->_amazon->createDomain($domainName);
  162. try {
  163. $itemName1 = $this->_testItemNamePrefix . '_testBatchPutAttributes1';
  164. $itemName2 = $this->_testItemNamePrefix . '_testBatchPutAttributes2';
  165. $attributeName1 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes1';
  166. $attributeName2 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes2';
  167. $attributeName3 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes3';
  168. $attributeName4 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes4';
  169. $attributeValue1 = 'value1';
  170. $attributeValue2 = 'value2';
  171. $attributeValue3 = 'value3';
  172. $attributeValue4 = 'value4';
  173. $attributeValue5 = 'value5';
  174. $items = array(
  175. $itemName1 => array(
  176. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName1, $attributeName1, $attributeValue1),
  177. $attributeName2 =>new Zend_Service_Amazon_SimpleDb_Attribute($itemName1, $attributeName2, $attributeValue2)),
  178. $itemName2 => array(
  179. $attributeName3 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName2, $attributeName3, $attributeValue3),
  180. $attributeName4 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName2, $attributeName4, array($attributeValue4, $attributeValue5)))
  181. );
  182. $replace = array(
  183. $itemName1 => array(
  184. $attributeName1 => false,
  185. $attributeName2 => false
  186. ),
  187. $itemName2 => array(
  188. $attributeName3 => false,
  189. $attributeName4 => false
  190. )
  191. );
  192. $this->assertEquals(array(), $this->_amazon->getAttributes($domainName, $itemName1));
  193. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  194. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  195. $this->assertTrue(array_key_exists($attributeName1, $result));
  196. $this->assertEquals($attributeName1, $result[$attributeName1]->getName());
  197. $this->assertEquals($attributeValue1, current($result[$attributeName1]->getValues()));
  198. $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
  199. $this->assertTrue(array_key_exists($attributeName4, $result));
  200. $this->assertEquals(2, count($result[$attributeName4]->getValues()));
  201. $result = $this->_amazon->getAttributes($domainName, $itemName2);
  202. $this->assertEquals(2, count($result));
  203. $this->assertTrue(array_key_exists($attributeName3, $result));
  204. $this->assertEquals($attributeName3, $result[$attributeName3]->getName());
  205. $this->assertEquals(1, count($result[$attributeName3]));
  206. $this->assertEquals($attributeValue3, current($result[$attributeName3]->getValues()));
  207. $this->assertTrue(array_key_exists($attributeName4, $result));
  208. $this->assertEquals($attributeName4, $result[$attributeName4]->getName());
  209. $this->assertEquals(2, $result[$attributeName4]->getValues());
  210. $this->assertEquals(array($attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  211. // Test replace
  212. $oldItems = $items;
  213. $newAttributeValue1 = 'newValue1';
  214. $newAttributeValue4 = 'newValue4';
  215. $items[$itemName1][$attributeName1] = array($newAttributeValue1);
  216. $items[$itemName2][$attributeName4] = array($newAttributeValue4);
  217. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  218. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  219. $this->assertEquals($oldItems[$itemName1][$attributeName1], $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1));
  220. $this->assertEquals($oldItems[$itemName2][$attributeName4], $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4));
  221. $this->assertEquals($oldItems[$itemName1], $this->_amazon->getAttributes($domainName, $itemName1));
  222. $replace[$itemName1][$attributeName1] = true;
  223. $replace[$itemName2][$attributeName4] = true;
  224. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  225. $this->assertEquals($items[$itemName1][$attributeName1], $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1));
  226. $this->assertEquals($items[$itemName2][$attributeName4], $this->_amazon->getAttributes($domainName, $itemName2, $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. /**
  294. *
  295. * @param $maxNumberOfDomains Integer between 1 and 100
  296. * @param $nextToken Integer between 1 and 100
  297. * @return array 0 or more domain names
  298. */
  299. public function testListDomains() {
  300. $domainName = null;
  301. try {
  302. // Create some domains
  303. for($i = 1; $i <= 3; $i++) {
  304. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  305. $this->_amazon->deleteDomain($domainName);
  306. $this->_amazon->createDomain($domainName);
  307. }
  308. $this->_wait();
  309. $page = $this->_amazon->listDomains(3);
  310. $this->assertEquals(3, count($page->getData()));
  311. // Amazon returns an empty page as the last page :/
  312. $this->assertTrue($page->isLast());
  313. $this->assertEquals(1, count($this->_amazon->listDomains(1, $page->getToken())));
  314. $page = $this->_amazon->listDomains(4);
  315. $this->assertEquals(3, count($page->getData()));
  316. $this->assertTrue($page->isLast());
  317. $page = $this->_amazon->listDomains(2);
  318. $this->assertEquals(2, count($page->getData()));
  319. $this->assertFalse($page->isLast());
  320. $nextPage = $this->_amazon->listDomains(100, $page->getToken());
  321. $this->assertEquals(1, count($nextPage->getData()));
  322. // Amazon returns an empty page as the last page :/
  323. $this->assertTrue($nextPage->isLast());
  324. // Delete the domains
  325. for($i = 1; $i <= 3; $i++) {
  326. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  327. $this->_amazon->deleteDomain($domainName);
  328. }
  329. } catch(Exception $e) {
  330. // Delete the domains
  331. for($i = 1; $i <= 3; $i++) {
  332. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  333. $this->_amazon->deleteDomain($domainName);
  334. }
  335. throw $e;
  336. }
  337. }
  338. /**
  339. * @param $domainName string Name of the domain for which metadata will be requested
  340. * @return array Key/value array of metadatum names and values.
  341. */
  342. public function testDomainMetadata() {
  343. $domainName = $this->_testDomainNamePrefix . '_testDomainMetadata';
  344. $this->_amazon->deleteDomain($domainName);
  345. $this->_amazon->createDomain($domainName);
  346. try {
  347. $metadata = $this->_amazon->domainMetadata($domainName);
  348. $this->assertTrue(is_array($metadata));
  349. $this->assertGreaterThan(0, count($metadata));
  350. $this->assertTrue(array_key_exists('ItemCount', $metadata));
  351. $this->assertEquals(0, (int)$metadata['ItemCount']);
  352. $this->assertTrue(array_key_exists('ItemNamesSizeBytes', $metadata));
  353. $this->assertEquals(0, (int)$metadata['ItemNamesSizeBytes']);
  354. $this->assertTrue(array_key_exists('AttributeNameCount', $metadata));
  355. $this->assertEquals(0, (int)$metadata['AttributeNameCount']);
  356. $this->assertTrue(array_key_exists('AttributeValueCount', $metadata));
  357. $this->assertEquals(0, (int)$metadata['AttributeValueCount']);
  358. $this->assertTrue(array_key_exists('AttributeNamesSizeBytes', $metadata));
  359. $this->assertEquals(0, (int)$metadata['AttributeNamesSizeBytes']);
  360. $this->assertTrue(array_key_exists('AttributeValuesSizeBytes', $metadata));
  361. $this->assertEquals(0, (int)$metadata['AttributeValuesSizeBytes']);
  362. $this->assertTrue(array_key_exists('Timestamp', $metadata));
  363. // Delete the domain
  364. $this->_amazon->deleteDomain($domainName);
  365. } catch(Exception $e) {
  366. $this->_amazon->deleteDomain($domainName);
  367. throw $e;
  368. }
  369. }
  370. /**
  371. *
  372. * @param $domainName string Valid domain name of the domain to create
  373. * @return boolean True if successful, false if not
  374. */
  375. public function testCreateDomain() {
  376. $domainName = $this->_testDomainNamePrefix . '_testCreateDomain';
  377. $this->_amazon->deleteDomain($domainName);
  378. $this->_amazon->createDomain($domainName);
  379. try {
  380. $domainListPage = $this->_amazon->listDomains();
  381. $this->assertContains($domainName, $domainListPage->getData());
  382. // Delete the domain
  383. $this->_amazon->deleteDomain($domainName);
  384. } catch(Exception $e) {
  385. $this->_amazon->deleteDomain($domainName);
  386. throw $e;
  387. }
  388. }
  389. public function testDeleteDomain() {
  390. $domainName = $this->_testDomainNamePrefix . '_testDeleteDomain';
  391. $this->_amazon->deleteDomain($domainName);
  392. $this->_amazon->createDomain($domainName);
  393. try {
  394. $domainListPage = $this->_amazon->listDomains();
  395. $this->assertContains($domainName, $domainListPage->getData());
  396. $this->assertNull($domainListPage->getToken());
  397. // Delete the domain
  398. $this->_amazon->deleteDomain($domainName);
  399. $domainListPage = $this->_amazon->listDomains();
  400. $this->assertNotContains($domainName, $domainListPage->getData());
  401. } catch(Exception $e) {
  402. $this->_amazon->deleteDomain($domainName);
  403. throw $e;
  404. }
  405. }
  406. private function _wait() {
  407. sleep($this->_testWaitPeriod);
  408. }
  409. /**
  410. * Tear down the test case
  411. *
  412. * @return void
  413. */
  414. public function tearDown()
  415. {
  416. // $this->_amazon->deleteDomain($this->_testDomainNamePrefix);
  417. // Delete domains
  418. unset($this->_amazon);
  419. }
  420. }