Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClassReflectorTest.php
Go to the documentation of this file.
1 <?php
7 
11 class ClassReflectorTest extends \PHPUnit\Framework\TestCase
12 {
14  protected $_typeProcessor;
15 
17  protected $_classReflector;
18 
22  protected function setUp()
23  {
24  $this->_typeProcessor = $this->createPartialMock(
25  \Magento\Framework\Reflection\TypeProcessor::class,
26  ['process']
27  );
28  $this->_typeProcessor->expects(
29  $this->any()
30  )->method(
31  'process'
32  )->will(
33  $this->returnValueMap([['string', 'str'], ['int', 'int']])
34  );
35  $this->_classReflector = new \Magento\Webapi\Model\Config\ClassReflector($this->_typeProcessor);
36  }
37 
38  public function testReflectClassMethods()
39  {
40  $data = $this->_classReflector->reflectClassMethods(
41  \Magento\Webapi\Test\Unit\Model\Config\TestServiceForClassReflector::class,
42  ['generateRandomString' => ['method' => 'generateRandomString']]
43  );
44  $this->assertEquals(['generateRandomString' => $this->_getSampleReflectionData()], $data);
45  }
46 
47  public function testExtractMethodData()
48  {
49  $classReflection = new \Zend\Code\Reflection\ClassReflection(
50  \Magento\Webapi\Test\Unit\Model\Config\TestServiceForClassReflector::class
51  );
53  $methodReflection = $classReflection->getMethods()[0];
54  $methodData = $this->_classReflector->extractMethodData($methodReflection);
55  $expectedResponse = $this->_getSampleReflectionData();
56  $this->assertEquals($expectedResponse, $methodData);
57  }
58 
64  protected function _getSampleReflectionData()
65  {
66  return [
67  'documentation' => 'Basic random string generator. This line is short description ' .
68  'This line is long description. This is still the long description.',
69  'interface' => [
70  'in' => [
71  'parameters' => [
72  'length' => [
73  'type' => 'int',
74  'required' => true,
75  'documentation' => 'length of the random string',
76  ],
77  ],
78  ],
79  'out' => [
80  'parameters' => [
81  'result' => ['type' => 'string', 'documentation' => 'random string', 'required' => true],
82  ],
83  ],
84  ]
85  ];
86  }
87 }