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

/tests/unit/WindowsAzure/MediaServices/Internal/ContentPropertiesSerializerTest.php

http://github.com/WindowsAzure/azure-sdk-for-php
PHP | 313 lines | 192 code | 37 blank | 84 comment | 0 complexity | 99b846570971f8e59c3d1349c402cb03 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (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. * http://www.apache.org/licenses/LICENSE-2.0.
  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. *
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. *
  22. * @link https://github.com/WindowsAzure/azure-sdk-for-php
  23. */
  24. namespace Tests\unit\WindowsAzure\MediaServices\Internal;
  25. use WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer;
  26. use WindowsAzure\Common\Internal\Resources;
  27. use WindowsAzure\MediaServices\Models\Asset;
  28. use WindowsAzure\MediaServices\Models\IngestManifest;
  29. use WindowsAzure\MediaServices\Models\Task;
  30. use WindowsAzure\MediaServices\Models\TaskOptions;
  31. use PHPUnit\Framework\TestCase;
  32. /**
  33. * Unit tests for class ContentPropertiesSerializer.
  34. *
  35. * @category Microsoft
  36. *
  37. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  38. * @copyright Microsoft Corporation
  39. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  40. *
  41. * @version Release: 0.5.0_2016-11
  42. *
  43. * @link https://github.com/WindowsAzure/azure-sdk-for-php
  44. */
  45. class ContentPropertiesSerializerTest extends TestCase
  46. {
  47. /**
  48. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::unserialize
  49. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::_unserializeRecursive
  50. */
  51. public function testUnserializeSimple()
  52. {
  53. // Setup
  54. $testString = 'testString';
  55. $nameKey = 'name';
  56. $xmlString = '<properties xmlns="'.Resources::DSM_XML_NAMESPACE.'" xmlns:d="'.Resources::DS_XML_NAMESPACE.'">
  57. <d:'.$nameKey.'>'.$testString.'</d:'.$nameKey.'>
  58. </properties>';
  59. $xml = simplexml_load_string($xmlString);
  60. // Test
  61. $result = ContentPropertiesSerializer::unserialize($xml);
  62. // Assert
  63. $this->assertEquals(1, count($result));
  64. $this->assertEquals($testString, $result[$nameKey]);
  65. }
  66. /**
  67. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::unserialize
  68. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::_unserializeRecursive
  69. */
  70. public function testUnserializeElement()
  71. {
  72. // Setup
  73. $testString = 'testString';
  74. $nameKey = 'name';
  75. $objectKey = 'object';
  76. $xmlString = '<properties xmlns="'.Resources::DSM_XML_NAMESPACE.'" xmlns:d="'.Resources::DS_XML_NAMESPACE.'">
  77. <d:'.$nameKey.'>'.$testString.'</d:'.$nameKey.'>
  78. <d:'.$objectKey.'>
  79. <d:'.$nameKey.'>'.$testString.'</d:'.$nameKey.'>
  80. </d:'.$objectKey.'>
  81. </properties>';
  82. $xml = simplexml_load_string($xmlString);
  83. // Test
  84. $result = ContentPropertiesSerializer::unserialize($xml);
  85. // Assert
  86. $this->assertEquals(2, count($result));
  87. $this->assertEquals($testString, $result[$nameKey]);
  88. $this->assertEquals(1, count($result[$objectKey]));
  89. $this->assertEquals($testString, $result[$objectKey][$nameKey]);
  90. }
  91. /**
  92. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::unserialize
  93. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::_unserializeRecursive
  94. */
  95. public function testUnserializeCollection()
  96. {
  97. // Setup
  98. $testString = 'testString';
  99. $nameKey = 'name';
  100. $otherNameKey = 'name';
  101. $objectKey = 'object';
  102. $xmlString = '<properties xmlns="'.Resources::DSM_XML_NAMESPACE.'" xmlns:d="'.Resources::DS_XML_NAMESPACE.'">
  103. <d:'.$nameKey.'>'.$testString.'</d:'.$nameKey.'>
  104. <d:'.$objectKey.'>
  105. <d:element>
  106. <d:'.$nameKey.'>'.$testString.'</d:'.$nameKey.'>
  107. </d:element>
  108. <d:element>
  109. <d:'.$otherNameKey.'>'.$testString.'</d:'.$otherNameKey.'>
  110. </d:element>
  111. </d:'.$objectKey.'>
  112. </properties>';
  113. $xml = simplexml_load_string($xmlString);
  114. // Test
  115. $result = ContentPropertiesSerializer::unserialize($xml);
  116. // Assert
  117. $this->assertEquals(2, count($result));
  118. $this->assertEquals($testString, $result[$nameKey]);
  119. $this->assertEquals(2, count($result[$objectKey]));
  120. $this->assertEquals(1, count($result[$objectKey][0]));
  121. $this->assertEquals($testString, $result[$objectKey][0][$nameKey]);
  122. $this->assertEquals(1, count($result[$objectKey][1]));
  123. $this->assertEquals($testString, $result[$objectKey][1][$otherNameKey]);
  124. }
  125. /**
  126. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::serialize
  127. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::_serializeRecursive
  128. */
  129. public function testSerializeSimple()
  130. {
  131. // Setup
  132. $name = 'NameName';
  133. $nameKey = 'Name';
  134. $optionsKey = 'Options';
  135. $option = Asset::OPTIONS_STORAGE_ENCRYPTED;
  136. $assetArray = [
  137. $nameKey => $name,
  138. $optionsKey => $option,
  139. ];
  140. $asset = Asset::createFromOptions($assetArray);
  141. $expected = '
  142. <meta:properties xmlns:meta="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  143. <data:'.$optionsKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$option.'</data:'.$optionsKey.'>
  144. <data:'.$nameKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$name.'</data:'.$nameKey.'>
  145. </meta:properties>
  146. ';
  147. // Test
  148. $result = ContentPropertiesSerializer::serialize($asset);
  149. // Assert
  150. $this->assertXmlStringEqualsXmlString($expected, $result);
  151. }
  152. /**
  153. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::serialize
  154. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::_serializeRecursive
  155. */
  156. public function testSerializeDate()
  157. {
  158. // Setup
  159. $name = 'NameName';
  160. $nameKey = 'Name';
  161. $optionsKey = 'Options';
  162. $option = Asset::OPTIONS_STORAGE_ENCRYPTED;
  163. $dateKey = 'Created';
  164. $date = '2013-12-31T01:16:25+01:00';
  165. $assetArray = [
  166. $nameKey => $name,
  167. $optionsKey => $option,
  168. $dateKey => $date,
  169. ];
  170. $asset = Asset::createFromOptions($assetArray);
  171. $expected = '
  172. <meta:properties xmlns:meta="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  173. <data:'.$optionsKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$option.'</data:'.$optionsKey.'>
  174. <data:'.$nameKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$name.'</data:'.$nameKey.'>
  175. <data:'.$dateKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$date.'</data:'.$dateKey.'>
  176. </meta:properties>
  177. ';
  178. // Test
  179. $result = ContentPropertiesSerializer::serialize($asset);
  180. // Assert
  181. $this->assertXmlStringEqualsXmlString($expected, $result);
  182. }
  183. /**
  184. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::serialize
  185. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::_serializeRecursive
  186. */
  187. public function testSerializeElement()
  188. {
  189. // Setup
  190. $name = 'NameName';
  191. $nameKey = 'Name';
  192. $statKey = 'Statistics';
  193. $statPendingFilesKey = 'PendingFilesCount';
  194. $statPendingFiles = 1;
  195. $statFinishedFilesKey = 'FinishedFilesCount';
  196. $statFinishedFiles = 2;
  197. $stat = [
  198. $statPendingFilesKey => $statPendingFiles,
  199. $statFinishedFilesKey => $statFinishedFiles,
  200. ];
  201. $objArray = [
  202. $nameKey => $name,
  203. $statKey => $stat,
  204. ];
  205. $obj = IngestManifest::createFromOptions($objArray);
  206. $expected = '
  207. <meta:properties xmlns:meta="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  208. <data:'.$statKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">
  209. <data:'.$statFinishedFilesKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$statFinishedFiles.'</data:'.$statFinishedFilesKey.'>
  210. <data:'.$statPendingFilesKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$statPendingFiles.'</data:'.$statPendingFilesKey.'>
  211. </data:'.$statKey.'>
  212. <data:'.$nameKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$name.'</data:'.$nameKey.'>
  213. </meta:properties>
  214. ';
  215. // Test
  216. $result = ContentPropertiesSerializer::serialize($obj);
  217. // Assert
  218. $this->assertXmlStringEqualsXmlString($expected, $result);
  219. }
  220. /**
  221. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::serialize
  222. * @covers \WindowsAzure\MediaServices\Internal\ContentPropertiesSerializer::_serializeRecursive
  223. */
  224. public function testSerializeCollection()
  225. {
  226. // Setup
  227. $taskBodyKey = 'TaskBody';
  228. $taskBody = 'TaskBody';
  229. $optionsKey = 'Options';
  230. $options = TaskOptions::NONE;
  231. $mediaProcessorIdKey = 'MediaProcessorId';
  232. $mediaProcessorId = 'MediaProcessorId';
  233. $errorKey = 'ErrorDetails';
  234. $errorCodeKey = 'Code';
  235. $errorCode = 1;
  236. $errorMessageKey = 'Message';
  237. $errorMessage = 'Error message';
  238. $error = [
  239. [
  240. $errorCodeKey => $errorCode,
  241. $errorMessageKey => $errorMessage,
  242. ],
  243. [
  244. $errorCodeKey => $errorCode,
  245. $errorMessageKey => $errorMessage,
  246. ],
  247. ];
  248. $objArray = [
  249. $taskBodyKey => $taskBody,
  250. $optionsKey => $options,
  251. $mediaProcessorIdKey => $mediaProcessorId,
  252. $errorKey => $error,
  253. ];
  254. $obj = Task::createFromOptions($objArray);
  255. $expected = '
  256. <meta:properties xmlns:meta="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  257. <data:'.$taskBodyKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$taskBody.'</data:'.$taskBodyKey.'>
  258. <data:'.$mediaProcessorIdKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$mediaProcessorId.'</data:'.$mediaProcessorIdKey.'>
  259. <data:'.$errorKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">
  260. <data:element xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">
  261. <data:'.$errorMessageKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$errorMessage.'</data:'.$errorMessageKey.'>
  262. <data:'.$errorCodeKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$errorCode.'</data:'.$errorCodeKey.'>
  263. </data:element>
  264. <data:element xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">
  265. <data:'.$errorMessageKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$errorMessage.'</data:'.$errorMessageKey.'>
  266. <data:'.$errorCodeKey.' xmlns:data="http://schemas.microsoft.com/ado/2007/08/dataservices">'.$errorCode.'</data:'.$errorCodeKey.'>
  267. </data:element>
  268. </data:'.$errorKey.'>
  269. </meta:properties>
  270. ';
  271. // Test
  272. $result = ContentPropertiesSerializer::serialize($obj);
  273. // Assert
  274. $this->assertXmlStringEqualsXmlString($expected, $result);
  275. }
  276. }