MethodsMapTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Reflection\Test\Unit;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. use Magento\Framework\Reflection\MethodsMap;
  9. use Magento\Framework\Reflection\TypeProcessor;
  10. /**
  11. * MethodsMap test
  12. */
  13. class MethodsMapTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var MethodsMap
  17. */
  18. private $object;
  19. /** @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */
  20. private $serializerMock;
  21. /**
  22. * Set up helper.
  23. */
  24. protected function setUp()
  25. {
  26. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  27. $cacheMock = $this->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class)
  28. ->getMockForAbstractClass();
  29. $cacheMock->expects($this->any())
  30. ->method('save');
  31. $cacheMock->expects($this->any())
  32. ->method('load')
  33. ->will($this->returnValue(null));
  34. $attributeTypeResolverMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeTypeResolverInterface::class)
  35. ->getMockForAbstractClass();
  36. $fieldNamerMock = $this->getMockBuilder(\Magento\Framework\Reflection\FieldNamer::class)
  37. ->getMockForAbstractClass();
  38. $this->object = $objectManager->getObject(
  39. \Magento\Framework\Reflection\MethodsMap::class,
  40. [
  41. 'cache' => $cacheMock,
  42. 'typeProcessor' => new TypeProcessor(),
  43. 'typeResolver' => $attributeTypeResolverMock,
  44. 'fieldNamer' => $fieldNamerMock,
  45. ]
  46. );
  47. $this->serializerMock = $this->createMock(SerializerInterface::class);
  48. $objectManager->setBackwardCompatibleProperty(
  49. $this->object,
  50. 'serializer',
  51. $this->serializerMock
  52. );
  53. }
  54. public function testGetMethodReturnType()
  55. {
  56. $this->assertEquals(
  57. 'string',
  58. $this->object->getMethodReturnType(
  59. \Magento\Framework\Reflection\FieldNamer::class,
  60. 'getFieldNameForMethodName'
  61. )
  62. );
  63. $this->assertEquals(
  64. 'mixed',
  65. $this->object->getMethodReturnType(
  66. \Magento\Framework\Reflection\TypeCaster::class,
  67. 'castValueToType'
  68. )
  69. );
  70. $this->assertEquals(
  71. 'array',
  72. $this->object->getMethodReturnType(
  73. \Magento\Framework\Reflection\MethodsMap::class,
  74. 'getMethodsMap'
  75. )
  76. );
  77. }
  78. public function testGetMethodsMap()
  79. {
  80. $data = [
  81. 'getMethodReturnType' => [
  82. 'type' => 'string',
  83. 'isRequired' => true,
  84. 'description' => null,
  85. 'parameterCount' => 2,
  86. ],
  87. 'getMethodsMap' => [
  88. 'type' => 'array',
  89. 'isRequired' => true,
  90. 'description' => "<pre> Service methods' reflection data stored in cache as 'methodName' => "
  91. . "'returnType' ex. [ 'create' => '\Magento\Customer\Api\Data\Customer', 'validatePassword' "
  92. . "=> 'boolean' ] </pre>",
  93. 'parameterCount' => 1,
  94. ],
  95. 'getMethodParams' => [
  96. 'type' => 'array',
  97. 'isRequired' => true,
  98. 'description' => null,
  99. 'parameterCount' => 2
  100. ],
  101. 'isMethodValidForDataField' => [
  102. 'type' => 'bool',
  103. 'isRequired' => true,
  104. 'description' => null,
  105. 'parameterCount' => 2,
  106. ],
  107. 'isMethodReturnValueRequired' => [
  108. 'type' => 'bool',
  109. 'isRequired' => true,
  110. 'description' => null,
  111. 'parameterCount' => 2,
  112. ],
  113. ];
  114. $this->serializerMock->expects($this->once())
  115. ->method('serialize')
  116. ->with($data);
  117. $methodsMap = $this->object->getMethodsMap(\Magento\Framework\Reflection\MethodsMap::class);
  118. $this->assertEquals(
  119. $data,
  120. $methodsMap
  121. );
  122. }
  123. /**
  124. * @param string $type
  125. * @param string $methodName
  126. * @param bool $expectedResult
  127. * @dataProvider isMethodValidForDataFieldProvider
  128. */
  129. public function testIsMethodValidForDataField($type, $methodName, $expectedResult)
  130. {
  131. $this->assertEquals($this->object->isMethodValidForDataField($type, $methodName), $expectedResult);
  132. }
  133. /**
  134. * @return array
  135. */
  136. public function isMethodValidForDataFieldProvider()
  137. {
  138. return [
  139. 'MethodsMap#isMethodValidForDataField' => [\Magento\Framework\Reflection\MethodsMap::class,
  140. 'isMethodValidForDataField',
  141. false,
  142. ],
  143. 'DataObject#getAttrName' => [\Magento\Framework\Reflection\Test\Unit\DataObject::class,
  144. 'getAttrName',
  145. true,
  146. ],
  147. 'DataObject#isActive' => [\Magento\Framework\Reflection\Test\Unit\DataObject::class,
  148. 'isActive',
  149. true,
  150. ],
  151. ];
  152. }
  153. /**
  154. * @param string $type
  155. * @param string $methodName
  156. * @param bool $expectedResult
  157. * @dataProvider isMethodReturnValueRequiredProvider
  158. */
  159. public function testIsMethodReturnValueRequired($type, $methodName, $expectedResult)
  160. {
  161. $this->assertEquals($this->object->isMethodValidForDataField($type, $methodName), $expectedResult);
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function isMethodReturnValueRequiredProvider()
  167. {
  168. return [
  169. 'DataObject#getAttrName' => [\Magento\Framework\Reflection\Test\Unit\DataObject::class,
  170. 'getAttrName',
  171. true,
  172. ],
  173. 'DataObject#isActive' => [\Magento\Framework\Reflection\Test\Unit\DataObject::class,
  174. 'isActive',
  175. true,
  176. ],
  177. 'FieldNamer#getFieldNameForMethodName' => [\Magento\Framework\Reflection\FieldNamer::class,
  178. 'getFieldNameForMethodName',
  179. false,
  180. ],
  181. ];
  182. }
  183. }