2
0

OnlineTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. $this->_wait();
  195. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  196. $this->assertTrue(array_key_exists($attributeName1, $result));
  197. $this->assertEquals($attributeName1, $result[$attributeName1]->getName());
  198. $this->assertEquals($attributeValue1, current($result[$attributeName1]->getValues()));
  199. $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
  200. $this->assertTrue(array_key_exists($attributeName4, $result));
  201. $this->assertEquals(2, count($result[$attributeName4]->getValues()));
  202. $result = $this->_amazon->getAttributes($domainName, $itemName2);
  203. $this->assertEquals(2, count($result));
  204. $this->assertTrue(array_key_exists($attributeName3, $result));
  205. $this->assertEquals($attributeName3, $result[$attributeName3]->getName());
  206. $this->assertEquals(1, count($result[$attributeName3]));
  207. $this->assertEquals($attributeValue3, current($result[$attributeName3]->getValues()));
  208. $this->assertTrue(array_key_exists($attributeName4, $result));
  209. $this->assertEquals($attributeName4, $result[$attributeName4]->getName());
  210. $this->assertEquals(2, count($result[$attributeName4]->getValues()));
  211. $this->assertEquals(array($attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  212. // Test replace
  213. $newAttributeValue1 = 'newValue1';
  214. $newAttributeValue4 = 'newValue4';
  215. $items[$itemName1][$attributeName1]->setValues(array($newAttributeValue1));
  216. $items[$itemName2][$attributeName4]->setValues(array($newAttributeValue4));
  217. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  218. $this->_wait();
  219. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  220. $this->assertEquals(array($newAttributeValue1, $attributeValue1), $result[$attributeName1]->getValues());
  221. $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
  222. $this->assertEquals(array($newAttributeValue4, $attributeValue4, $attributeValue5), $result[$attributeName4]->getValues());
  223. $replace[$itemName1][$attributeName1] = true;
  224. $replace[$itemName2][$attributeName4] = true;
  225. $this->_amazon->batchPutAttributes($items, $domainName, $replace);
  226. $this->_wait();
  227. $result = $this->_amazon->getAttributes($domainName, $itemName1, $attributeName1);
  228. $this->assertEquals($items[$itemName1][$attributeName1], $result[$attributeName1]);
  229. $result = $this->_amazon->getAttributes($domainName, $itemName2, $attributeName4);
  230. $this->assertEquals($items[$itemName2][$attributeName4], $result[$attributeName4]);
  231. $this->assertEquals($items[$itemName1], $this->_amazon->getAttributes($domainName, $itemName1));
  232. $this->_amazon->deleteDomain($domainName);
  233. } catch(Exception $e) {
  234. $this->_amazon->deleteDomain($domainName);
  235. throw $e;
  236. }
  237. }
  238. public function testDeleteAttributes() {
  239. $domainName = $this->_testDomainNamePrefix . '_testDeleteAttributes';
  240. $this->_amazon->deleteDomain($domainName);
  241. $this->_amazon->createDomain($domainName);
  242. try {
  243. $itemName = $this->_testItemNamePrefix . '_testDeleteAttributes';
  244. $attributeName1 = $this->_testAttributeNamePrefix . '_testDeleteAttributes1';
  245. $attributeName2 = $this->_testAttributeNamePrefix . '_testDeleteAttributes2';
  246. $attributeName3 = $this->_testAttributeNamePrefix . '_testDeleteAttributes3';
  247. $attributeName4 = $this->_testAttributeNamePrefix . '_testDeleteAttributes4';
  248. $attributeValue1 = 'value1';
  249. $attributeValue2 = 'value2';
  250. $attributeValue3 = 'value3';
  251. $attributeValue4 = 'value4';
  252. $attributes = array(
  253. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName1, $attributeValue1),
  254. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName2, $attributeValue2),
  255. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName3, $attributeValue3),
  256. new Zend_Service_Amazon_SimpleDb_Attribute($itemName, $attributeName4, $attributeValue4)
  257. );
  258. // Now that everything's set up, test it
  259. $this->_amazon->putAttributes($domainName, $itemName, $attributes);
  260. $this->_wait();
  261. $results = $this->_amazon->getAttributes($domainName, $itemName);
  262. $this->assertEquals(4, count($results));
  263. $this->assertTrue(array_key_exists($attributeName1, $results));
  264. $this->assertTrue(array_key_exists($attributeName2, $results));
  265. $this->assertTrue(array_key_exists($attributeName3, $results));
  266. $this->assertTrue(array_key_exists($attributeName4, $results));
  267. $this->assertEquals($attributeValue1, current($results[$attributeName1]->getValues()));
  268. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  269. $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  270. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  271. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[0]));
  272. $this->_wait();
  273. $results = $this->_amazon->getAttributes($domainName, $itemName);
  274. $this->assertEquals(3, count($results));
  275. $this->assertTrue(array_key_exists($attributeName2, $results));
  276. $this->assertTrue(array_key_exists($attributeName3, $results));
  277. $this->assertTrue(array_key_exists($attributeName4, $results));
  278. $this->assertEquals($attributeValue2, current($results[$attributeName2]->getValues()));
  279. $this->assertEquals($attributeValue3, current($results[$attributeName3]->getValues()));
  280. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  281. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[1], $attributes[2]));
  282. $this->_wait();
  283. $results = $this->_amazon->getAttributes($domainName, $itemName);
  284. $this->assertEquals(1, count($results));
  285. $this->assertTrue(array_key_exists($attributeName4, $results));
  286. $this->assertEquals($attributeValue4, current($results[$attributeName4]->getValues()));
  287. $this->_amazon->deleteAttributes($domainName, $itemName, array($attributes[3]));
  288. $this->_wait();
  289. $results = $this->_amazon->getAttributes($domainName, $itemName);
  290. $this->assertEquals(0, count($results));
  291. $this->_amazon->deleteDomain($domainName);
  292. } catch(Exception $e) {
  293. $this->_amazon->deleteDomain($domainName);
  294. throw $e;
  295. }
  296. }
  297. /**
  298. *
  299. * @param $maxNumberOfDomains Integer between 1 and 100
  300. * @param $nextToken Integer between 1 and 100
  301. * @return array 0 or more domain names
  302. */
  303. public function testListDomains() {
  304. $domainName = null;
  305. try {
  306. // Create some domains
  307. for($i = 1; $i <= 3; $i++) {
  308. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  309. $this->_amazon->deleteDomain($domainName);
  310. $this->_amazon->createDomain($domainName);
  311. }
  312. $this->_wait();
  313. $page = $this->_amazon->listDomains(3);
  314. $this->assertEquals(3, count($page->getData()));
  315. // Amazon returns an empty page as the last page :/
  316. $this->assertTrue($page->isLast());
  317. $this->assertEquals(1, count($this->_amazon->listDomains(1, $page->getToken())));
  318. $page = $this->_amazon->listDomains(4);
  319. $this->assertEquals(3, count($page->getData()));
  320. $this->assertTrue($page->isLast());
  321. $page = $this->_amazon->listDomains(2);
  322. $this->assertEquals(2, count($page->getData()));
  323. $this->assertFalse($page->isLast());
  324. $nextPage = $this->_amazon->listDomains(100, $page->getToken());
  325. $this->assertEquals(1, count($nextPage->getData()));
  326. // Amazon returns an empty page as the last page :/
  327. $this->assertTrue($nextPage->isLast());
  328. // Delete the domains
  329. for($i = 1; $i <= 3; $i++) {
  330. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  331. $this->_amazon->deleteDomain($domainName);
  332. }
  333. } catch(Exception $e) {
  334. // Delete the domains
  335. for($i = 1; $i <= 3; $i++) {
  336. $domainName = $this->_testDomainNamePrefix . '_testListDomains' . $i;
  337. $this->_amazon->deleteDomain($domainName);
  338. }
  339. throw $e;
  340. }
  341. }
  342. /**
  343. * @param $domainName string Name of the domain for which metadata will be requested
  344. * @return array Key/value array of metadatum names and values.
  345. */
  346. public function testDomainMetadata() {
  347. $domainName = $this->_testDomainNamePrefix . '_testDomainMetadata';
  348. $this->_amazon->deleteDomain($domainName);
  349. $this->_amazon->createDomain($domainName);
  350. try {
  351. $metadata = $this->_amazon->domainMetadata($domainName);
  352. $this->assertTrue(is_array($metadata));
  353. $this->assertGreaterThan(0, count($metadata));
  354. $this->assertTrue(array_key_exists('ItemCount', $metadata));
  355. $this->assertEquals(0, (int)$metadata['ItemCount']);
  356. $this->assertTrue(array_key_exists('ItemNamesSizeBytes', $metadata));
  357. $this->assertEquals(0, (int)$metadata['ItemNamesSizeBytes']);
  358. $this->assertTrue(array_key_exists('AttributeNameCount', $metadata));
  359. $this->assertEquals(0, (int)$metadata['AttributeNameCount']);
  360. $this->assertTrue(array_key_exists('AttributeValueCount', $metadata));
  361. $this->assertEquals(0, (int)$metadata['AttributeValueCount']);
  362. $this->assertTrue(array_key_exists('AttributeNamesSizeBytes', $metadata));
  363. $this->assertEquals(0, (int)$metadata['AttributeNamesSizeBytes']);
  364. $this->assertTrue(array_key_exists('AttributeValuesSizeBytes', $metadata));
  365. $this->assertEquals(0, (int)$metadata['AttributeValuesSizeBytes']);
  366. $this->assertTrue(array_key_exists('Timestamp', $metadata));
  367. // Delete the domain
  368. $this->_amazon->deleteDomain($domainName);
  369. } catch(Exception $e) {
  370. $this->_amazon->deleteDomain($domainName);
  371. throw $e;
  372. }
  373. }
  374. /**
  375. *
  376. * @param $domainName string Valid domain name of the domain to create
  377. * @return boolean True if successful, false if not
  378. */
  379. public function testCreateDomain() {
  380. $domainName = $this->_testDomainNamePrefix . '_testCreateDomain';
  381. $this->_amazon->deleteDomain($domainName);
  382. $this->_amazon->createDomain($domainName);
  383. try {
  384. $domainListPage = $this->_amazon->listDomains();
  385. $this->assertContains($domainName, $domainListPage->getData());
  386. // Delete the domain
  387. $this->_amazon->deleteDomain($domainName);
  388. } catch(Exception $e) {
  389. $this->_amazon->deleteDomain($domainName);
  390. throw $e;
  391. }
  392. }
  393. public function testDeleteDomain() {
  394. $domainName = $this->_testDomainNamePrefix . '_testDeleteDomain';
  395. $this->_amazon->deleteDomain($domainName);
  396. $this->_amazon->createDomain($domainName);
  397. try {
  398. $domainListPage = $this->_amazon->listDomains();
  399. $this->assertContains($domainName, $domainListPage->getData());
  400. $this->assertNull($domainListPage->getToken());
  401. // Delete the domain
  402. $this->_amazon->deleteDomain($domainName);
  403. $domainListPage = $this->_amazon->listDomains();
  404. $this->assertNotContains($domainName, $domainListPage->getData());
  405. } catch(Exception $e) {
  406. $this->_amazon->deleteDomain($domainName);
  407. throw $e;
  408. }
  409. }
  410. private function _wait() {
  411. sleep($this->_testWaitPeriod);
  412. }
  413. /**
  414. * Tear down the test case
  415. *
  416. * @return void
  417. */
  418. public function tearDown()
  419. {
  420. // $this->_amazon->deleteDomain($this->_testDomainNamePrefix);
  421. // Delete domains
  422. unset($this->_amazon);
  423. }
  424. }