OnlineTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 = 5;
  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. }
  91. public function testGetAttributes() {
  92. $domainName = $this->_testDomainNamePrefix . '_testGetAttributes';
  93. $this->_amazon->deleteDomain($domainName);
  94. $this->_amazon->createDomain($domainName);
  95. try {
  96. $itemName = $this->_testItemNamePrefix . '_testGetAttributes';
  97. $attributeName1 = $this->_testAttributeNamePrefix . '_testGetAttributes1';
  98. $attributeName2 = $this->_testAttributeNamePrefix . '_testGetAttributes2';
  99. $attributeValue1 = 'value1';
  100. $attributeValue2 = 'value2';
  101. $attributes = array(
  102. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  103. $attributeName2 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2)
  104. );
  105. // Now that everything's set up, test it
  106. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  107. // One attribute
  108. $results = $this->_amazon->getAttributes($domainName, $itemName, $attributeName1);
  109. $this->assertEquals(1, count($results));
  110. $attribute = current($results);
  111. $this->assertEquals($attributeName1, $attribute->getName());
  112. $this->assertEquals($attributeValue1, current($attribute->getValues()));
  113. // Multiple attributes
  114. $results = $this->_amazon->getAttributes($domainName, $itemName);
  115. $this->assertEquals(2, count($results));
  116. $this->assertTrue(array_key_exists($attributeName1, $results));
  117. $this->assertTrue(array_key_exists($attributeName2, $results));
  118. $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  119. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  120. $this->_amazon->deleteDomain($domainName);
  121. } catch(Exception $e) {
  122. $this->_amazon->deleteDomain($domainName);
  123. throw $e;
  124. }
  125. }
  126. public function testPutAttributes() {
  127. $domainName = $this->_testDomainNamePrefix . '_testPutAttributes';
  128. $this->_amazon->deleteDomain($domainName);
  129. $this->_amazon->createDomain($domainName);
  130. try {
  131. $itemName = $this->_testItemNamePrefix . '_testPutAttributes';
  132. $attributeName1 = $this->_testAttributeNamePrefix . '_testPutAttributes1';
  133. $attributeName2 = $this->_testAttributeNamePrefix . '_testPutAttributes2';
  134. $attributeValue1 = 'value1';
  135. $attributeValue2 = 'value2';
  136. $attributes = array(
  137. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  138. $attributeName2 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2)
  139. );
  140. // Now that everything's set up, test it
  141. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  142. // Multiple attributes
  143. $results = $this->_amazon->getAttributes($domainName, $itemName);
  144. $this->assertEquals(2, count($results));
  145. $this->assertTrue(array_key_exists($attributeName1, $results));
  146. $this->assertTrue(array_key_exists($attributeName2, $results));
  147. $this->assertEquals($attributes[$attributeName1], $results[$attributeName1]);
  148. $this->assertEquals($attributes[$attributeName2], $results[$attributeName2]);
  149. $this->_amazon->deleteDomain($domainName);
  150. } catch(Exception $e) {
  151. $this->_amazon->deleteDomain($domainName);
  152. throw $e;
  153. }
  154. }
  155. public function testBatchPutAttributes() {
  156. $domainName = $this->_testDomainNamePrefix . '_testBatchPutAttributes';
  157. $this->_amazon->deleteDomain($domainName);
  158. $this->_amazon->createDomain($domainName);
  159. try {
  160. $itemName1 = $this->_testItemNamePrefix . '_testBatchPutAttributes1';
  161. $itemName2 = $this->_testItemNamePrefix . '_testBatchPutAttributes2';
  162. $attributeName1 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes1';
  163. $attributeName2 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes2';
  164. $attributeName3 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes3';
  165. $attributeName4 = $this->_testAttributeNamePrefix . '_testBatchPutAttributes4';
  166. $attributeValue1 = 'value1';
  167. $attributeValue2 = 'value2';
  168. $attributeValue3 = 'value3';
  169. $attributeValue4 = 'value4';
  170. $attributeValue5 = 'value5';
  171. $items = array(
  172. $itemName1 => array(
  173. $attributeName1 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName1, $attributeName1, $attributeValue1),
  174. $attributeName2 =>new Zend_Service_Amazon_SimpleDb_Attribute($itemName1, $attributeName2, $attributeValue2)),
  175. $itemName2 => array(
  176. $attributeName3 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName2, $attributeName3, $attributeValue3),
  177. $attributeName4 => new Zend_Service_Amazon_SimpleDb_Attribute($itemName2, $attributeName4, array($attributeValue4, $attributeValue5)))
  178. );
  179. $replace = array(
  180. $itemName1 => array(
  181. $attributeName1 => false,
  182. $attributeName2 => false
  183. ),
  184. $itemName2 => array(
  185. $attributeName3 => false,
  186. $attributeName4 => false
  187. )
  188. );
  189. $this->assertEquals(array(), $this->_amazon->getAttributes($domainName, $itemName1));
  190. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  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, $result[$attributeName4]->getValues());
  207. $this->assertEquals(array($attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  208. // Test replace
  209. $oldItems = $items;
  210. $newAttributeValue1 = 'newValue1';
  211. $newAttributeValue4 = 'newValue4';
  212. $items[$itemName1][$attributeName1] = array($newAttributeValue1);
  213. $items[$itemName2][$attributeName4] = array($newAttributeValue4);
  214. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  215. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  216. $this->assertEquals($oldItems[$itemName1][$attributeName1], $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1));
  217. $this->assertEquals($oldItems[$itemName2][$attributeName4], $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4));
  218. $this->assertEquals($oldItems[$itemName1], $this->_amazon->getAttributes($domainName, $itemName1));
  219. $replace[$itemName1][$attributeName1] = true;
  220. $replace[$itemName2][$attributeName4] = true;
  221. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  222. $this->assertEquals($items[$itemName1][$attributeName1], $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1));
  223. $this->assertEquals($items[$itemName2][$attributeName4], $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4));
  224. $this->assertEquals($items[$itemName1], $this->_amazon->getAttributes($domainName, $itemName1));
  225. $this->_amazon->deleteDomain($domainName);
  226. } catch(Exception $e) {
  227. $this->_amazon->deleteDomain($domainName);
  228. throw $e;
  229. }
  230. }
  231. public function testDeleteAttributes() {
  232. $domainName = $this->_testDomainNamePrefix . '_testDeleteAttributes';
  233. $this->_amazon->deleteDomain($domainName);
  234. $this->_amazon->createDomain($domainName);
  235. try {
  236. $itemName = $this->_testItemNamePrefix . '_testDeleteAttributes';
  237. $attributeName1 = $this->_testAttributeNamePrefix . '_testDeleteAttributes1';
  238. $attributeName2 = $this->_testAttributeNamePrefix . '_testDeleteAttributes2';
  239. $attributeName3 = $this->_testAttributeNamePrefix . '_testDeleteAttributes3';
  240. $attributeName4 = $this->_testAttributeNamePrefix . '_testDeleteAttributes4';
  241. $attributeValue1 = 'value1';
  242. $attributeValue2 = 'value2';
  243. $attributeValue3 = 'value3';
  244. $attributeValue4 = 'value4';
  245. $attributes = array(
  246. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  247. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2),
  248. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName3, $attributeValue3),
  249. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName4, $attributeValue4)
  250. );
  251. // Now that everything's set up, test it
  252. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  253. $this->_wait();
  254. $results = $this->_amazon->getAttributes($domainName, $itemName);
  255. $this->assertEquals(4, count($results));
  256. $this->assertTrue(array_key_exists($attributeName1, $results));
  257. $this->assertTrue(array_key_exists($attributeName2, $results));
  258. $this->assertTrue(array_key_exists($attributeName3, $results));
  259. $this->assertTrue(array_key_exists($attributeName4, $results));
  260. $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  261. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  262. $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  263. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  264. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[0]));
  265. $this->_wait();
  266. $results = $this->_amazon->getAttributes($domainName, $itemName);
  267. $this->assertEquals(3, count($results));
  268. $this->assertTrue(array_key_exists($attributeName2, $results));
  269. $this->assertTrue(array_key_exists($attributeName3, $results));
  270. $this->assertTrue(array_key_exists($attributeName4, $results));
  271. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  272. $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  273. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  274. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[1], $attributes[2]));
  275. $this->_wait();
  276. $results = $this->_amazon->getAttributes($domainName, $itemName);
  277. $this->assertEquals(1, count($results));
  278. $this->assertTrue(array_key_exists($attributeName4, $results));
  279. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  280. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[3]));
  281. $this->_wait();
  282. $results = $this->_amazon->getAttributes($domainName, $itemName);
  283. $this->assertEquals(0, count($results));
  284. $this->_amazon->deleteDomain($domainName);
  285. } catch(Exception $e) {
  286. $this->_amazon->deleteDomain($domainName);
  287. throw $e;
  288. }
  289. }
  290. /**
  291. *
  292. * @param $maxNumberOfDomains Integer between 1 and 100
  293. * @param $nextToken Integer between 1 and 100
  294. * @return array 0 or more domain names
  295. */
  296. public function testListDomains() {
  297. $domainName = null;
  298. try {
  299. // Create some domains
  300. for($i = 1; $i <= 3; $i++) {
  301. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  302. $this->_amazon->deleteDomain($domainName);
  303. $this->_amazon->createDomain($domainName);
  304. }
  305. $this->_wait();
  306. $page = $this->_amazon->listDomains(3);
  307. $this->assertEquals(3, count($page->getData()));
  308. // Amazon returns an empty page as the last page :/
  309. $this->assertTrue($page->isLast());
  310. $this->assertEquals(1, count($this->_amazon->listDomains(1, $page->getToken())));
  311. $page = $this->_amazon->listDomains(4);
  312. $this->assertEquals(3, count($page->getData()));
  313. $this->assertTrue($page->isLast());
  314. $page = $this->_amazon->listDomains(2);
  315. $this->assertEquals(2, count($page->getData()));
  316. $this->assertFalse($page->isLast());
  317. $nextPage = $this->_amazon->listDomains(100, $page->getToken());
  318. $this->assertEquals(1, count($nextPage->getData()));
  319. // Amazon returns an empty page as the last page :/
  320. $this->assertTrue($nextPage->isLast());
  321. // Delete the domains
  322. for($i = 1; $i <= 3; $i++) {
  323. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  324. $this->_amazon->deleteDomain($domainName);
  325. }
  326. } catch(Exception $e) {
  327. // Delete the domains
  328. for($i = 1; $i <= 3; $i++) {
  329. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  330. $this->_amazon->deleteDomain($domainName);
  331. }
  332. throw $e;
  333. }
  334. }
  335. /**
  336. * @param $domainName string Name of the domain for which metadata will be requested
  337. * @return array Key/value array of metadatum names and values.
  338. */
  339. public function testDomainMetadata() {
  340. $domainName = $this->_testDomainNamePrefix . '_testDomainMetadata';
  341. $this->_amazon->deleteDomain($domainName);
  342. $this->_amazon->createDomain($domainName);
  343. try {
  344. $metadata = $this->_amazon->domainMetadata($domainName);
  345. $this->assertTrue(is_array($metadata));
  346. $this->assertGreaterThan(0, count($metadata));
  347. $this->assertTrue(array_key_exists('ItemCount', $metadata));
  348. $this->assertEquals(0, (int)$metadata['ItemCount']);
  349. $this->assertTrue(array_key_exists('ItemNamesSizeBytes', $metadata));
  350. $this->assertEquals(0, (int)$metadata['ItemNamesSizeBytes']);
  351. $this->assertTrue(array_key_exists('AttributeNameCount', $metadata));
  352. $this->assertEquals(0, (int)$metadata['AttributeNameCount']);
  353. $this->assertTrue(array_key_exists('AttributeValueCount', $metadata));
  354. $this->assertEquals(0, (int)$metadata['AttributeValueCount']);
  355. $this->assertTrue(array_key_exists('AttributeNamesSizeBytes', $metadata));
  356. $this->assertEquals(0, (int)$metadata['AttributeNamesSizeBytes']);
  357. $this->assertTrue(array_key_exists('AttributeValuesSizeBytes', $metadata));
  358. $this->assertEquals(0, (int)$metadata['AttributeValuesSizeBytes']);
  359. $this->assertTrue(array_key_exists('Timestamp', $metadata));
  360. // Delete the domain
  361. $this->_amazon->deleteDomain($domainName);
  362. } catch(Exception $e) {
  363. $this->_amazon->deleteDomain($domainName);
  364. throw $e;
  365. }
  366. }
  367. /**
  368. *
  369. * @param $domainName string Valid domain name of the domain to create
  370. * @return boolean True if successful, false if not
  371. */
  372. public function testCreateDomain() {
  373. $domainName = $this->_testDomainNamePrefix . '_testCreateDomain';
  374. $this->_amazon->deleteDomain($domainName);
  375. $this->_amazon->createDomain($domainName);
  376. try {
  377. $domainListPage = $this->_amazon->listDomains();
  378. $this->assertContains($domainName, $domainListPage->getData());
  379. // Delete the domain
  380. $this->_amazon->deleteDomain($domainName);
  381. } catch(Exception $e) {
  382. $this->_amazon->deleteDomain($domainName);
  383. throw $e;
  384. }
  385. }
  386. public function testDeleteDomain() {
  387. $domainName = $this->_testDomainNamePrefix . '_testDeleteDomain';
  388. $this->_amazon->deleteDomain($domainName);
  389. $this->_amazon->createDomain($domainName);
  390. try {
  391. $domainListPage = $this->_amazon->listDomains();
  392. $this->assertContains($domainName, $domainListPage->getData());
  393. $this->assertNull($domainListPage->getToken());
  394. // Delete the domain
  395. $this->_amazon->deleteDomain($domainName);
  396. $domainListPage = $this->_amazon->listDomains();
  397. $this->assertNotContains($domainName, $domainListPage->getData());
  398. } catch(Exception $e) {
  399. $this->_amazon->deleteDomain($domainName);
  400. throw $e;
  401. }
  402. }
  403. private function _wait() {
  404. sleep($this->_testWaitPeriod);
  405. }
  406. /**
  407. * Tear down the test case
  408. *
  409. * @return void
  410. */
  411. public function tearDown()
  412. {
  413. // $this->_amazon->deleteDomain($this->_testDomainNamePrefix);
  414. // Delete domains
  415. unset($this->_amazon);
  416. }
  417. }