DynamicTableEntityTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_WindowsAzure
  17. * @subpackage UnitTests
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Service_WindowsAzure_DynamicTableEntityTest::main');
  24. }
  25. /**
  26. * Test helpers
  27. */
  28. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  29. require_once dirname(__FILE__) . '/../../../TestConfiguration.php.dist';
  30. /** Zend_Service_WindowsAzure_Storage_Table */
  31. require_once 'Zend/Service/WindowsAzure/Storage/Table.php';
  32. /** Zend_Service_WindowsAzure_Storage_DynamicTableEntity */
  33. require_once 'Zend/Service/WindowsAzure/Storage/DynamicTableEntity.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Service_WindowsAzure
  37. * @subpackage UnitTests
  38. * @version $Id$
  39. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Service_WindowsAzure_DynamicTableEntityTest extends PHPUnit_Framework_TestCase
  43. {
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Service_WindowsAzure_DynamicTableEntityTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Test teardown
  51. */
  52. protected function tearDown()
  53. {
  54. $storageClient = $this->createStorageInstance();
  55. for ($i = 1; $i <= self::$uniqId; $i++)
  56. {
  57. try { $storageClient->deleteTable(TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_TABLENAME_PREFIX . $i); } catch (Exception $e) { }
  58. }
  59. }
  60. protected function createStorageInstance()
  61. {
  62. $storageClient = null;
  63. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_RUNONPROD) {
  64. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_HOST_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_PROD, false, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  65. } else {
  66. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_HOST_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_DEV, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_DEV, true, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  67. }
  68. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY) {
  69. $storageClient->setProxy(TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_USEPROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_PORT, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_PROXY_CREDENTIALS);
  70. }
  71. return $storageClient;
  72. }
  73. protected static $uniqId = 0;
  74. protected function generateName()
  75. {
  76. self::$uniqId++;
  77. return TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_TABLENAME_PREFIX . self::$uniqId;
  78. }
  79. /**
  80. * Test constructor
  81. */
  82. public function testConstructor()
  83. {
  84. $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity('partition1', '000001');
  85. $this->assertEquals('partition1', $target->getPartitionKey());
  86. $this->assertEquals('000001', $target->getRowKey());
  87. }
  88. /**
  89. * Test get Azure values
  90. */
  91. public function testGetAzureValues()
  92. {
  93. $dateTimeValue = new DateTime();
  94. $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity('partition1', '000001');
  95. $target->Name = 'Name';
  96. $target->Age = 25;
  97. $target->DateInService = $dateTimeValue;
  98. $result = $target->getAzureValues();
  99. $this->assertEquals('Name', $result[0]->Name);
  100. $this->assertEquals('Name', $result[0]->Value);
  101. $this->assertEquals('Edm.String', $result[0]->Type);
  102. $this->assertEquals('Age', $result[1]->Name);
  103. $this->assertEquals(25, $result[1]->Value);
  104. $this->assertEquals('Edm.Int32', $result[1]->Type);
  105. $this->assertEquals('DateInService', $result[2]->Name);
  106. $this->assertEquals($dateTimeValue, $result[2]->Value);
  107. $this->assertEquals('Edm.DateTime', $result[2]->Type);
  108. $this->assertEquals('partition1', $result[3]->Value);
  109. $this->assertEquals('000001', $result[4]->Value);
  110. }
  111. /**
  112. * Test set Azure values
  113. */
  114. public function testSetAzureValues()
  115. {
  116. $dateTimeValue = new DateTime();
  117. $values = array(
  118. 'PartitionKey' => 'partition1',
  119. 'RowKey' => '000001',
  120. 'Name' => 'Maarten',
  121. 'Age' => 25,
  122. 'Visible' => true,
  123. 'DateInService' => $dateTimeValue
  124. );
  125. $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity();
  126. $target->setAzureValues($values);
  127. $target->setAzurePropertyType('Age', 'Edm.Int32');
  128. $this->assertEquals('partition1', $target->getPartitionKey());
  129. $this->assertEquals('000001', $target->getRowKey());
  130. $this->assertEquals('Maarten', $target->Name);
  131. $this->assertEquals(25, $target->Age);
  132. $this->assertEquals('Edm.Int32', $target->getAzurePropertyType('Age'));
  133. $this->assertEquals(true, $target->Visible);
  134. $this->assertEquals($dateTimeValue, $target->DateInService);
  135. $this->assertEquals('Edm.DateTime', $target->getAzurePropertyType('DateInService'));
  136. }
  137. /**
  138. * Test insert entity
  139. */
  140. public function testInsertEntity()
  141. {
  142. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_TABLE_RUNTESTS) {
  143. $tableName = $this->generateName();
  144. $storageClient = $this->createStorageInstance();
  145. $storageClient->createTable($tableName);
  146. $entity = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity();
  147. $entity->Name = 'Maarten';
  148. $entity->Age = 25;
  149. $entity->Inserted = new DateTime();
  150. $entity->TestValue = 200000;
  151. $entity->NullStringValue = null;
  152. $result = $storageClient->insertEntity($tableName, $entity);
  153. $this->assertNotEquals('0001-01-01T00:00:00', $result->getTimestamp());
  154. $this->assertNotEquals('', $result->getEtag());
  155. $this->assertEquals($entity->getAzureValues(), $result->getAzureValues());
  156. }
  157. }
  158. }
  159. // Call Zend_Service_WindowsAzure_DynamicTableEntityTest::main() if this source file is executed directly.
  160. if (PHPUnit_MAIN_METHOD == "Zend_Service_WindowsAzure_DynamicTableEntityTest::main") {
  161. Zend_Service_WindowsAzure_DynamicTableEntityTest::main();
  162. }