Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClassGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
9 {
13  const FLAG_CONST = 'const';
14 
15  const FLAG_STATIC = 'static';
16 
17  const FLAG_FINAL = 'final';
18 
19  const FLAG_ABSTRACT = 'abstract';
20 
21  const FLAG_REFERENCE = 'passedByReference';
22 
23  const FLAG_VARIADIC = 'variadic';
24 
30  protected $_model;
31 
37  protected $_flagVerification = [
38  self::FLAG_CONST => 'isConst',
39  self::FLAG_STATIC => 'isStatic',
40  self::FLAG_FINAL => 'isFinal',
41  self::FLAG_ABSTRACT => 'isAbstract',
42  self::FLAG_REFERENCE => 'getPassedByReference',
43  self::FLAG_VARIADIC => 'getVariadic',
44  ];
45 
51  protected $_docBlockData = [
52  'shortDescription' => 'test_short_description',
53  'longDescription' => 'test_long_description',
54  'tags' => [
55  'tag1' => ['name' => 'tag1', 'description' => 'data1'],
56  'tag2' => ['name' => 'tag2', 'description' => 'data2'],
57  ],
58  ];
59 
65  protected $_methodData = [
66  'testmethod1' => [
67  'name' => 'testMethod1',
68  'final' => true,
69  'static' => true,
70  'parameters' => [
71  [
72  'name' => 'data',
73  'type' => 'array',
74  'defaultValue' => [],
75  'passedByReference' => true,
76  'variadic' => false
77  ],
78  ],
79  'body' => 'return 1;',
80  'docblock' => ['shortDescription' => 'test short description'],
81  ],
82  '_testmethod2' => [
83  'name' => '_testMethod2',
84  'visibility' => 'private',
85  'abstract' => true,
86  'parameters' => [
87  ['name' => 'data', 'defaultValue' => 'test_default'],
88  ['name' => 'flag', 'defaultValue' => true],
89  ],
90  'body' => 'return 2;',
91  'docblock' => [
92  'shortDescription' => 'test short description',
93  'longDescription' => 'test long description',
94  'tags' => [
95  'tag1' => ['name' => 'tag1', 'description' => 'data1'],
96  'tag2' => ['name' => 'tag2', 'description' => 'data2'],
97  ],
98  ],
99  ],
100  'testmethod3' => ['name' => 'testMethod3', 'body' => 'return 3;'],
101  ];
102 
108  protected $_propertyData = [
109  '_protectedProperty' => [
110  'name' => '_protectedProperty',
111  'visibility' => 'protected',
112  'static' => 'true',
113  'docblock' => [
114  'shortDescription' => 'Object Manager instance',
115  'tags' => ['var' => ['name' => 'var', 'description' => 'tag description']],
116  ],
117  ],
118  'publicProperty' => ['name' => 'publicProperty'],
119  ];
120 
121  protected function setUp()
122  {
123  $this->_model = new \Magento\Framework\Code\Generator\ClassGenerator();
124  }
125 
126  protected function tearDown()
127  {
128  unset($this->_model);
129  }
130 
131  public function testSetClassDocBlock()
132  {
133  $this->_model->setClassDocBlock($this->_docBlockData);
134  $actualDocBlock = $this->_model->getDocBlock();
135 
136  $this->_assertDocBlockData($this->_docBlockData, $actualDocBlock);
137  }
138 
143  protected function _assertDocBlockData(
144  array $expectedDocBlock,
145  \Zend\Code\Generator\DocBlockGenerator $actualDocBlock
146  ) {
147  // assert plain string data
148  foreach ($expectedDocBlock as $propertyName => $propertyData) {
149  if (is_string($propertyData)) {
150  $this->assertAttributeEquals($propertyData, $propertyName, $actualDocBlock);
151  }
152  }
153 
154  // assert tags
155  if (isset($expectedDocBlock['tags'])) {
156  $expectedTagsData = $expectedDocBlock['tags'];
157  $actualTags = $actualDocBlock->getTags();
158  $this->assertSameSize($expectedTagsData, $actualTags);
160  foreach ($actualTags as $actualTag) {
161  $tagName = $actualTag->getName();
162  $this->assertArrayHasKey($tagName, $expectedTagsData);
163  $this->assertEquals($expectedTagsData[$tagName]['name'], $tagName);
164  $this->assertEquals($expectedTagsData[$tagName]['description'], $actualTag->getDescription());
165  }
166  }
167  }
168 
169  public function testAddMethods()
170  {
171  $this->_model->addMethods($this->_methodData);
172  $actualMethods = $this->_model->getMethods();
173 
174  $this->assertSameSize($this->_methodData, $actualMethods);
175 
177  foreach ($actualMethods as $methodName => $method) {
178  $this->assertArrayHasKey($methodName, $this->_methodData);
179  $expectedMethodData = $this->_methodData[$methodName];
180 
181  $this->assertEquals($expectedMethodData['name'], $method->getName());
182  $this->assertEquals($expectedMethodData['body'], $method->getBody());
183 
184  // assert flags
185  $this->_assertFlag(self::FLAG_STATIC, $expectedMethodData, $method);
186  $this->_assertFlag(self::FLAG_FINAL, $expectedMethodData, $method);
187  $this->_assertFlag(self::FLAG_ABSTRACT, $expectedMethodData, $method);
188 
189  // assert visibility
190  $this->_assertVisibility($expectedMethodData, $method);
191 
192  // assert parameters
193  if (isset($expectedMethodData['parameters'])) {
194  $actualParameters = $method->getParameters();
195  $this->assertSameSize($expectedMethodData['parameters'], $actualParameters);
196  foreach ($expectedMethodData['parameters'] as $parameterData) {
197  $parameterName = $parameterData['name'];
198  $this->assertArrayHasKey($parameterName, $actualParameters);
200  $actualParameter = $actualParameters[$parameterName];
201  $this->assertEquals($parameterName, $actualParameter->getName());
202 
203  // assert reference flag
204  $this->_assertFlag(self::FLAG_REFERENCE, $parameterData, $actualParameter);
205 
206  // assert parameter type
207  if (isset($parameterData['type'])) {
208  $this->assertEquals($parameterData['type'], $actualParameter->getType());
209  }
210 
211  // assert default value
212  if (isset($parameterData['defaultValue'])) {
214  $actualDefaultValue = $actualParameter->getDefaultValue();
215  $this->assertEquals($parameterData['defaultValue'], $actualDefaultValue->getValue());
216  }
217 
218  // assert variadic flag
219  $this->_assertFlag(self::FLAG_VARIADIC, $parameterData, $actualParameter);
220  }
221  }
222 
223  // assert docblock
224  if (isset($expectedMethodData['docblock'])) {
225  $actualDocBlock = $method->getDocBlock();
226  $this->_assertDocBlockData($expectedMethodData['docblock'], $actualDocBlock);
227  }
228  }
229  }
230 
236  protected function _assertFlag($flagType, array $expectedData, $actualObject)
237  {
238  $expectedFlagValue = isset($expectedData[$flagType]) && $expectedData[$flagType];
239  $flagGetter = $this->_flagVerification[$flagType];
240  $this->assertEquals($expectedFlagValue, $actualObject->{$flagGetter}());
241  }
242 
247  protected function _assertVisibility(
248  array $expectedData,
249  \Zend\Code\Generator\AbstractMemberGenerator $actualObject
250  ) {
251  $expectedVisibility = isset($expectedData['visibility']) ? $expectedData['visibility'] : 'public';
252  $this->assertEquals($expectedVisibility, $actualObject->getVisibility());
253  }
254 
261  public function testAddMethodFromGenerator()
262  {
263  $invalidMethod = new \Zend\Code\Generator\MethodGenerator();
264  $this->_model->addMethodFromGenerator($invalidMethod);
265  }
266 
267  public function testAddProperties()
268  {
269  $this->_model->addProperties($this->_propertyData);
270  $actualProperties = $this->_model->getProperties();
271 
272  $this->assertSameSize($this->_propertyData, $actualProperties);
273 
275  foreach ($actualProperties as $propertyName => $property) {
276  $this->assertArrayHasKey($propertyName, $this->_propertyData);
277  $expectedPropertyData = $this->_propertyData[$propertyName];
278 
279  $this->assertEquals($expectedPropertyData['name'], $property->getName());
280 
281  // assert flags
282  $this->_assertFlag(self::FLAG_CONST, $expectedPropertyData, $property);
283  $this->_assertFlag(self::FLAG_STATIC, $expectedPropertyData, $property);
284 
285  // assert visibility
286  $this->_assertVisibility($expectedPropertyData, $property);
287 
288  // assert default value
289  if (isset($expectedPropertyData['defaultValue'])) {
291  $actualDefaultValue = $property->getDefaultValue();
292  $this->assertEquals($expectedPropertyData['defaultValue'], $actualDefaultValue->getValue());
293  }
294 
295  // assert docblock
296  if (isset($expectedPropertyData['docblock'])) {
297  $actualDocBlock = $property->getDocBlock();
298  $this->_assertDocBlockData($expectedPropertyData['docblock'], $actualDocBlock);
299  }
300  }
301  }
302 
310  {
311  $invalidProperty = new \Zend\Code\Generator\PropertyGenerator();
312  $this->_model->addPropertyFromGenerator($invalidProperty);
313  }
314 
320  public function testNamespaceName($actualNamespace, $expectedNamespace)
321  {
322  $this->assertEquals(
323  $expectedNamespace,
324  $this->_model->setNamespaceName($actualNamespace)
325  ->getNamespaceName()
326  );
327  }
328 
333  public function providerNamespaces()
334  {
335  return [
336  ['Zend', 'Zend'],
337  ['\Zend', 'Zend'],
338  ['\Zend\SomeClass', 'Zend\SomeClass'],
339  ['', null],
340  ];
341  }
342 }
return false
Definition: gallery.phtml:36
_assertFlag($flagType, array $expectedData, $actualObject)
_assertVisibility(array $expectedData, \Zend\Code\Generator\AbstractMemberGenerator $actualObject)
$method
Definition: info.phtml:13