PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/framework/Reflection/Test/Unit/TypeProcessorTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 253 lines | 174 code | 29 blank | 50 comment | 0 complexity | 3cb071e67c565da3ace02babe1159708 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreStart
  7. namespace Magento\Framework\Reflection\Test\Unit;
  8. use Zend\Code\Reflection\ClassReflection;
  9. /**
  10. * Type processor Test
  11. */
  12. class TypeProcessorTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\Reflection\TypeProcessor
  16. */
  17. protected $_typeProcessor;
  18. /**
  19. * Set up helper.
  20. */
  21. protected function setUp()
  22. {
  23. $this->_typeProcessor = new \Magento\Framework\Reflection\TypeProcessor();
  24. }
  25. /**
  26. * Test Retrieving of processed types data.
  27. */
  28. public function testGetTypesData()
  29. {
  30. $this->_typeProcessor->setTypeData('typeA', ['dataA']);
  31. $this->_typeProcessor->setTypeData('typeB', ['dataB']);
  32. $this->assertEquals(
  33. ['typeA' => ['dataA'], 'typeB' => ['dataB']],
  34. $this->_typeProcessor->getTypesData()
  35. );
  36. }
  37. /**
  38. * Test set of processed types data.
  39. */
  40. public function testSetTypesData()
  41. {
  42. $this->_typeProcessor->setTypeData('typeC', ['dataC']);
  43. $this->assertEquals(['typeC' => ['dataC']], $this->_typeProcessor->getTypesData());
  44. $typeData = ['typeA' => ['dataA'], 'typeB' => ['dataB']];
  45. $this->_typeProcessor->setTypesData($typeData);
  46. $this->assertEquals($typeData, $this->_typeProcessor->getTypesData());
  47. }
  48. /**
  49. * @expectedException \InvalidArgumentException
  50. * @expectedExceptionMessage Data type "NonExistentType" is not declared.
  51. */
  52. public function testGetTypeDataInvalidArgumentException()
  53. {
  54. $this->_typeProcessor->getTypeData('NonExistentType');
  55. }
  56. /**
  57. * Test retrieval of data type details for the given type name.
  58. */
  59. public function testGetTypeData()
  60. {
  61. $this->_typeProcessor->setTypeData('typeA', ['dataA']);
  62. $this->assertEquals(['dataA'], $this->_typeProcessor->getTypeData('typeA'));
  63. }
  64. /**
  65. * Test data type details for the same type name set multiple times.
  66. */
  67. public function testSetTypeDataArrayMerge()
  68. {
  69. $this->_typeProcessor->setTypeData('typeA', ['dataA1']);
  70. $this->_typeProcessor->setTypeData('typeA', ['dataA2']);
  71. $this->_typeProcessor->setTypeData('typeA', ['dataA3']);
  72. $this->_typeProcessor->setTypeData('typeA', [null]);
  73. $this->assertEquals(['dataA1', 'dataA2', 'dataA3', null], $this->_typeProcessor->getTypeData('typeA'));
  74. }
  75. public function testNormalizeType()
  76. {
  77. $this->assertEquals('blah', $this->_typeProcessor->normalizeType('blah'));
  78. $this->assertEquals('string', $this->_typeProcessor->normalizeType('str'));
  79. $this->assertEquals('int', $this->_typeProcessor->normalizeType('integer'));
  80. $this->assertEquals('boolean', $this->_typeProcessor->normalizeType('bool'));
  81. $this->assertEquals('anyType', $this->_typeProcessor->normalizeType('mixed'));
  82. }
  83. public function testIsTypeSimple()
  84. {
  85. $this->assertTrue($this->_typeProcessor->isTypeSimple('string'));
  86. $this->assertTrue($this->_typeProcessor->isTypeSimple('string[]'));
  87. $this->assertTrue($this->_typeProcessor->isTypeSimple('int'));
  88. $this->assertTrue($this->_typeProcessor->isTypeSimple('float'));
  89. $this->assertTrue($this->_typeProcessor->isTypeSimple('double'));
  90. $this->assertTrue($this->_typeProcessor->isTypeSimple('boolean'));
  91. $this->assertFalse($this->_typeProcessor->isTypeSimple('blah'));
  92. }
  93. public function testIsTypeAny()
  94. {
  95. $this->assertTrue($this->_typeProcessor->isTypeAny('mixed'));
  96. $this->assertTrue($this->_typeProcessor->isTypeAny('mixed[]'));
  97. $this->assertFalse($this->_typeProcessor->isTypeAny('int'));
  98. $this->assertFalse($this->_typeProcessor->isTypeAny('int[]'));
  99. }
  100. public function testIsArrayType()
  101. {
  102. $this->assertFalse($this->_typeProcessor->isArrayType('string'));
  103. $this->assertTrue($this->_typeProcessor->isArrayType('string[]'));
  104. }
  105. public function getArrayItemType()
  106. {
  107. $this->assertEquals('string', $this->_typeProcessor->getArrayItemType('str[]'));
  108. $this->assertEquals('string', $this->_typeProcessor->getArrayItemType('string[]'));
  109. $this->assertEquals('integer', $this->_typeProcessor->getArrayItemType('int[]'));
  110. $this->assertEquals('boolean', $this->_typeProcessor->getArrayItemType('bool[]'));
  111. $this->assertEquals('any', $this->_typeProcessor->getArrayItemType('mixed[]'));
  112. }
  113. public function testTranslateTypeName()
  114. {
  115. $this->assertEquals(
  116. 'TestModule1V1EntityItem',
  117. $this->_typeProcessor->translateTypeName('\Magento\TestModule1\Service\V1\Entity\Item')
  118. );
  119. $this->assertEquals(
  120. 'TestModule3V1EntityParameter[]',
  121. $this->_typeProcessor->translateTypeName('\Magento\TestModule3\Service\V1\Entity\Parameter[]')
  122. );
  123. }
  124. /**
  125. * @expectedException \InvalidArgumentException
  126. * @expectedExceptionMessage Invalid parameter type "\Magento\TestModule3\V1\Parameter[]".
  127. */
  128. public function testTranslateTypeNameInvalidArgumentException()
  129. {
  130. $this->_typeProcessor->translateTypeName('\Magento\TestModule3\V1\Parameter[]');
  131. }
  132. public function testTranslateArrayTypeName()
  133. {
  134. $this->assertEquals('ArrayOfComplexType', $this->_typeProcessor->translateArrayTypeName('complexType'));
  135. }
  136. public function testProcessSimpleTypeIntToString()
  137. {
  138. $value = 1;
  139. $type = 'string';
  140. $this->assertSame('1', $this->_typeProcessor->processSimpleAndAnyType($value, $type));
  141. }
  142. public function testProcessSimpleTypeStringToInt()
  143. {
  144. $value = '1';
  145. $type = 'int';
  146. $this->assertSame(1, $this->_typeProcessor->processSimpleAndAnyType($value, $type));
  147. }
  148. public function testProcessSimpleTypeMixed()
  149. {
  150. $value = 1;
  151. $type = 'mixed';
  152. $this->assertSame(1, $this->_typeProcessor->processSimpleAndAnyType($value, $type));
  153. }
  154. public function testProcessSimpleTypeIntArrayToStringArray()
  155. {
  156. $value = [1, 2, 3, 4, 5];
  157. $type = 'string[]';
  158. $this->assertSame(['1', '2', '3', '4', '5'], $this->_typeProcessor->processSimpleAndAnyType($value, $type));
  159. }
  160. public function testProcessSimpleTypeStringArrayToIntArray()
  161. {
  162. $value = ['1', '2', '3', '4', '5'];
  163. $type = 'int[]';
  164. $this->assertSame([1, 2, 3, 4, 5], $this->_typeProcessor->processSimpleAndAnyType($value, $type));
  165. }
  166. /**
  167. * @expectedException \Magento\Framework\Exception\SerializationException
  168. * @expectedExceptionMessage Invalid type for value: "integer". Expected Type: "int[]".
  169. */
  170. public function testProcessSimpleTypeInvalidType()
  171. {
  172. $value = 1;
  173. $type = 'int[]';
  174. $this->_typeProcessor->processSimpleAndAnyType($value, $type);
  175. }
  176. public function testFindSetterMethodName()
  177. {
  178. $class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
  179. $setterName = $this->_typeProcessor->findSetterMethodName($class, 'AttrName');
  180. $this->assertEquals("setAttrName", $setterName);
  181. $booleanSetterName = $this->_typeProcessor->findSetterMethodName($class, 'Active');
  182. $this->assertEquals("setIsActive", $booleanSetterName);
  183. }
  184. /**
  185. * @expectedException \Exception
  186. * @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
  187. */
  188. public function testFindSetterMethodNameInvalidAttribute()
  189. {
  190. $class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
  191. $this->_typeProcessor->findSetterMethodName($class, 'InvalidAttribute');
  192. }
  193. /**
  194. * @expectedException \Exception
  195. * @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
  196. */
  197. public function testFindSetterMethodNameWrongCamelCasedAttribute()
  198. {
  199. $class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
  200. $this->_typeProcessor->findSetterMethodName($class, 'ActivE');
  201. }
  202. /**
  203. * @expectedException \LogicException
  204. * @expectedExceptionMessageRegExp /@param annotation is incorrect for the parameter "name" \w+/
  205. */
  206. public function testGetParamType()
  207. {
  208. $class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
  209. $methodReflection = $class->getMethod('setName');
  210. $paramsReflection = $methodReflection->getParameters();
  211. $this->_typeProcessor->getParamType($paramsReflection[0]);
  212. }
  213. public function testGetParameterDescription()
  214. {
  215. $class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
  216. $methodReflection = $class->getMethod('setName');
  217. $paramsReflection = $methodReflection->getParameters();
  218. $this->assertEquals('Name of the attribute', $this->_typeProcessor->getParamDescription($paramsReflection[0]));
  219. }
  220. public function testGetOperationName()
  221. {
  222. $this->assertEquals("resNameMethodName", $this->_typeProcessor->getOperationName("resName", "methodName"));
  223. }
  224. }