PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/tests/Microsoft/WindowsAzure/DynamicTableEntityTest.php

#
PHP | 181 lines | 103 code | 22 blank | 56 comment | 5 complexity | 245bad262c8a79e3620ec1e8afbc0c53 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2012, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @subpackage UnitTests
  31. * @version $Id: BlobStorageTest.php 14561 2009-05-07 08:05:12Z unknown $
  32. * @copyright Copyright (c) 2009 - 2012, RealDolmen (http://www.realdolmen.com)
  33. * @license http://phpazure.codeplex.com/license
  34. */
  35. date_default_timezone_set('UTC');
  36. require_once dirname(__FILE__) . '/../../TestConfiguration.php';
  37. require_once 'PHPUnit/Framework/TestCase.php';
  38. require_once 'Microsoft/WindowsAzure/Storage/Table.php';
  39. require_once 'Microsoft/WindowsAzure/Storage/DynamicTableEntity.php';
  40. /**
  41. * @category Microsoft
  42. * @package Microsoft_WindowsAzure
  43. * @subpackage UnitTests
  44. * @version $Id: BlobStorageTest.php 14561 2009-05-07 08:05:12Z unknown $
  45. * @copyright Copyright (c) 2009 - 2012, RealDolmen (http://www.realdolmen.com)
  46. * @license http://phpazure.codeplex.com/license
  47. */
  48. class Microsoft_WindowsAzure_DynamicTableEntityTest extends PHPUnit_Framework_TestCase
  49. {
  50. /**
  51. * Test teardown
  52. */
  53. protected function tearDown()
  54. {
  55. $storageClient = $this->createStorageInstance();
  56. for ($i = 1; $i <= self::$uniqId; $i++)
  57. {
  58. try { $storageClient->deleteTable(TESTS_TABLE_TABLENAME_PREFIX . $i); } catch (Exception $e) { }
  59. }
  60. }
  61. protected function createStorageInstance()
  62. {
  63. $storageClient = null;
  64. if (TESTS_TABLE_RUNONPROD) {
  65. $storageClient = new Microsoft_WindowsAzure_Storage_Table(TESTS_TABLE_HOST_PROD, TESTS_STORAGE_ACCOUNT_PROD, TESTS_STORAGE_KEY_PROD, false, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  66. } else {
  67. $storageClient = new Microsoft_WindowsAzure_Storage_Table(TESTS_TABLE_HOST_DEV, TESTS_STORAGE_ACCOUNT_DEV, TESTS_STORAGE_KEY_DEV, true, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  68. }
  69. if (TESTS_STORAGE_USEPROXY) {
  70. $storageClient->setProxy(TESTS_STORAGE_USEPROXY, TESTS_STORAGE_PROXY, TESTS_STORAGE_PROXY_PORT, TESTS_STORAGE_PROXY_CREDENTIALS);
  71. }
  72. return $storageClient;
  73. }
  74. protected static $uniqId = 0;
  75. protected function generateName()
  76. {
  77. self::$uniqId++;
  78. return TESTS_TABLE_TABLENAME_PREFIX . self::$uniqId;
  79. }
  80. /**
  81. * Test constructor
  82. */
  83. public function testConstructor()
  84. {
  85. $target = new Microsoft_WindowsAzure_Storage_DynamicTableEntity('partition1', '000001');
  86. $this->assertEquals('partition1', $target->getPartitionKey());
  87. $this->assertEquals('000001', $target->getRowKey());
  88. }
  89. /**
  90. * Test get Azure values
  91. */
  92. public function testGetAzureValues()
  93. {
  94. $dateTimeValue = new DateTime();
  95. $target = new Microsoft_WindowsAzure_Storage_DynamicTableEntity('partition1', '000001');
  96. $target->Name = 'Name';
  97. $target->Age = 25;
  98. $target->DateInService = $dateTimeValue;
  99. $result = $target->getAzureValues();
  100. $this->assertEquals('Name', $result[0]->Name);
  101. $this->assertEquals('Name', $result[0]->Value);
  102. $this->assertEquals('Edm.String', $result[0]->Type);
  103. $this->assertEquals('Age', $result[1]->Name);
  104. $this->assertEquals(25, $result[1]->Value);
  105. $this->assertEquals('Edm.Int32', $result[1]->Type);
  106. $this->assertEquals('DateInService', $result[2]->Name);
  107. $this->assertEquals($dateTimeValue, $result[2]->Value);
  108. $this->assertEquals('Edm.DateTime', $result[2]->Type);
  109. $this->assertEquals('partition1', $result[3]->Value);
  110. $this->assertEquals('000001', $result[4]->Value);
  111. }
  112. /**
  113. * Test set Azure values
  114. */
  115. public function testSetAzureValues()
  116. {
  117. $dateTimeValue = new DateTime();
  118. $values = array(
  119. 'PartitionKey' => 'partition1',
  120. 'RowKey' => '000001',
  121. 'Name' => 'Maarten',
  122. 'Age' => 25,
  123. 'Visible' => true,
  124. 'DateInService' => $dateTimeValue
  125. );
  126. $target = new Microsoft_WindowsAzure_Storage_DynamicTableEntity();
  127. $target->setAzureValues($values);
  128. $target->setAzurePropertyType('Age', 'Edm.Int32');
  129. $this->assertEquals('partition1', $target->getPartitionKey());
  130. $this->assertEquals('000001', $target->getRowKey());
  131. $this->assertEquals('Maarten', $target->Name);
  132. $this->assertEquals(25, $target->Age);
  133. $this->assertEquals('Edm.Int32', $target->getAzurePropertyType('Age'));
  134. $this->assertEquals(true, $target->Visible);
  135. $this->assertEquals($dateTimeValue, $target->DateInService);
  136. $this->assertEquals('Edm.DateTime', $target->getAzurePropertyType('DateInService'));
  137. }
  138. /**
  139. * Test insert entity
  140. */
  141. public function testInsertEntity()
  142. {
  143. if (TESTS_TABLE_RUNTESTS) {
  144. $tableName = $this->generateName();
  145. $storageClient = $this->createStorageInstance();
  146. $storageClient->createTable($tableName);
  147. $entity = new Microsoft_WindowsAzure_Storage_DynamicTableEntity();
  148. $entity->Name = 'Maarten';
  149. $entity->Age = 25;
  150. $entity->Inserted = new DateTime();
  151. $entity->TestValue = 200000;
  152. $entity->NullStringValue = null;
  153. $result = $storageClient->insertEntity($tableName, $entity);
  154. $this->assertNotEquals('0001-01-01T00:00:00', $result->getTimestamp());
  155. $this->assertNotEquals('', $result->getEtag());
  156. $this->assertEquals($entity->getAzureValues(), $result->getAzureValues());
  157. }
  158. }
  159. }