/tests/Unit/Table/Internal/JsonODataReaderWriterTest.php

https://github.com/Azure/azure-storage-php · PHP · 210 lines · 127 code · 31 blank · 52 comment · 1 complexity · 4e5f14ca6b9f0a015548f74a67048a15 MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE: The MIT License (the "License")
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * https://github.com/azure/azure-storage-php/LICENSE
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package Tests\Unit\MicrosoftAzure\Storage\Table\internal
  18. * @author Azure Storage PHP SDK <dmsh@microsoft.com>
  19. * @copyright 2017 Microsoft Corporation
  20. * @license https://github.com/azure/azure-storage-php/LICENSE
  21. * @link https://github.com/azure/azure-storage-php
  22. */
  23. namespace Tests\Unit\MicrosoftAzure\Storage\Table\Models\internal;
  24. use MicrosoftAzure\Storage\Tests\Framework\TestResources;
  25. use MicrosoftAzure\Storage\Common\Internal\Utilities;
  26. use MicrosoftAzure\Storage\Common\Internal\Resources;
  27. use MicrosoftAzure\Storage\Table\Internal\JsonODataReaderWriter;
  28. use MicrosoftAzure\Storage\Table\Models\EdmType;
  29. use MicrosoftAzure\Storage\Table\Models\Entity;
  30. /**
  31. * Unit tests for class JsonODataReaderWriter
  32. *
  33. * @category Microsoft
  34. * @package Tests\Unit\MicrosoftAzure\Storage\Table\internal
  35. * @author Azure Storage PHP SDK <dmsh@microsoft.com>
  36. * @copyright 2017 Microsoft Corporation
  37. * @license https://github.com/azure/azure-storage-php/LICENSE
  38. * @link https://github.com/azure/azure-storage-php
  39. */
  40. class JsonODataReaderWriterTest extends \PHPUnit\Framework\TestCase
  41. {
  42. public function testGetTable()
  43. {
  44. // Setup
  45. $serializer = new JsonODataReaderWriter();
  46. $tablename = 'mytable';
  47. $expected = TestResources::getTableJSONFormat($tablename);
  48. // Test
  49. $actual = $serializer->getTable($tablename);
  50. // Assert
  51. $this->assertEquals($expected, $actual);
  52. return $actual;
  53. }
  54. public function testGetEntity()
  55. {
  56. // Setup
  57. $serializer = new JsonODataReaderWriter();
  58. $entity = TestResources::getTestEntity('123', '456');
  59. $entity->addProperty('Cost', EdmType::DOUBLE, 12.45);
  60. $expected = TestResources::ENTITY_JSON_STRING;
  61. // Test
  62. $actual = $serializer->getEntity($entity);
  63. // Assert
  64. $this->assertEquals($expected, $actual);
  65. return $actual;
  66. }
  67. public function testParseTable()
  68. {
  69. // Setup
  70. $serializer = new JsonODataReaderWriter();
  71. $expected = 'mytable';
  72. $tableJSON = TestResources::getTableEntryMinimalMetaResult();
  73. // Test
  74. $actual = $serializer->parseTable($tableJSON);
  75. // Assert
  76. $this->assertEquals($expected, $actual);
  77. }
  78. public function testParseTables()
  79. {
  80. // Setup
  81. $serializer = new JsonODataReaderWriter();
  82. $expected = array('mytable1', 'mytable2', 'mytable3', 'mytable4', 'mytable5');
  83. $tableJSON0 = TestResources::getTableEntriesMinimalMetaResult();
  84. $tableJSON1 = TestResources::getTableEntriesNoMetaResult();
  85. $tableJSON2 = TestResources::getTableEntriesFullMetaResult();
  86. // Test
  87. $actual0 = $serializer->parseTableEntries($tableJSON0);
  88. $actual1 = $serializer->parseTableEntries($tableJSON1);
  89. $actual2 = $serializer->parseTableEntries($tableJSON2);
  90. // Assert
  91. $this->assertEquals($expected, $actual0);
  92. $this->assertEquals($expected, $actual1);
  93. $this->assertEquals($expected, $actual2);
  94. }
  95. public function testParseEntity()
  96. {
  97. // Setup
  98. $serializer = new JsonODataReaderWriter();
  99. $expected = TestResources::getExpectedTestEntity('123', '456');
  100. $json = TestResources::getEntityMinimalMetaResult('123', '456');
  101. // Test
  102. $actual = $serializer->parseEntity($json);
  103. // Assert
  104. $expectedProperties = $expected->getProperties();
  105. $actualProperties = $actual->getProperties();
  106. foreach ($expectedProperties as $key => $property) {
  107. $this->assertEquals(
  108. $property->getEdmType(),
  109. $actualProperties[$key]->getEdmType()
  110. );
  111. $this->assertEquals(
  112. $property->getValue(),
  113. $actualProperties[$key]->getValue()
  114. );
  115. }
  116. }
  117. public function testParseEntityStringKeys()
  118. {
  119. // Setup
  120. $serializer = new JsonODataReaderWriter();
  121. $expected = TestResources::getExpectedTestEntity('0e123', '123e456');
  122. $json = TestResources::getEntityMinimalMetaResult('0e123', '123e456');
  123. // Test
  124. $actual = $serializer->parseEntity($json);
  125. // Assert
  126. $this->assertSame(
  127. $expected->getPartitionKey(),
  128. $actual->getPartitionKey()
  129. );
  130. $this->assertSame(
  131. $expected->getRowKey(),
  132. $actual->getRowKey()
  133. );
  134. }
  135. public function testParseEntities()
  136. {
  137. // Setup
  138. $serializer = new JsonODataReaderWriter();
  139. $pk1 = '123';
  140. $pk2 = '124';
  141. $pk3 = '125';
  142. $e1 = TestResources::getExpectedTestEntity($pk1, '1');
  143. $e2 = TestResources::getExpectedTestEntity($pk2, '2');
  144. $e3 = TestResources::getExpectedTestEntity($pk3, '3');
  145. $e1->setETag('W/"datetime\'2012-05-17T00%3A59%3A32.1131734Z\'"');
  146. $e2->setETag('W/"datetime\'2012-05-17T00%3A59%3A32.4252358Z\'"');
  147. $e3->setETag('W/"datetime\'2012-05-17T00%3A59%3A32.7533014Z\'"');
  148. $e1->setTimestamp(Utilities::convertToDateTime('2012-05-17T00:59:32.1131734Z'));
  149. $e2->setTimestamp(Utilities::convertToDateTime('2012-05-17T00:59:32.4252358Z'));
  150. $e3->setTimestamp(Utilities::convertToDateTime('2012-05-17T00:59:32.7533014Z'));
  151. $expected = array($e1, $e2, $e3);
  152. $entitiesJSON = TestResources::getEntitiesMinimalMetaResult();
  153. // Test
  154. $actual = $serializer->parseEntities($entitiesJSON);
  155. // Assert
  156. for ($i = 0; $i < 3; ++$i) {
  157. $expectedProperties = $expected[$i]->getProperties();
  158. $actualProperties = $actual[$i]->getProperties();
  159. foreach ($expectedProperties as $key => $property) {
  160. $this->assertEquals(
  161. $property->getEdmType(),
  162. $actualProperties[$key]->getEdmType()
  163. );
  164. $this->assertEquals(
  165. $property->getValue(),
  166. $actualProperties[$key]->getValue()
  167. );
  168. }
  169. }
  170. }
  171. public function testVariousTypes()
  172. {
  173. $serializer = new JsonODataReaderWriter();
  174. $e = TestResources::getVariousTypesEntity();
  175. $jsonString = $serializer->getEntity($e);
  176. $a = $serializer->parseEntity($jsonString);
  177. $this->assertEquals($e, $a);
  178. }
  179. }