Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TypeProcessorTest.php
Go to the documentation of this file.
1 <?php
6 // @codingStandardsIgnoreStart
8 
12 use Zend\Code\Reflection\ClassReflection;
13 
14 class TypeProcessorTest extends \PHPUnit\Framework\TestCase
15 {
19  private $typeProcessor;
20 
24  protected function setUp()
25  {
26  $this->typeProcessor = new TypeProcessor();
27  }
28 
32  public function testGetTypesData()
33  {
34  $this->typeProcessor->setTypeData('typeA', ['dataA']);
35  $this->typeProcessor->setTypeData('typeB', ['dataB']);
36  $this->assertEquals(
37  ['typeA' => ['dataA'], 'typeB' => ['dataB']],
38  $this->typeProcessor->getTypesData()
39  );
40  }
41 
45  public function testSetTypesData()
46  {
47  $this->typeProcessor->setTypeData('typeC', ['dataC']);
48  $this->assertEquals(['typeC' => ['dataC']], $this->typeProcessor->getTypesData());
49  $typeData = ['typeA' => ['dataA'], 'typeB' => ['dataB']];
50  $this->typeProcessor->setTypesData($typeData);
51  $this->assertEquals($typeData, $this->typeProcessor->getTypesData());
52  }
53 
59  {
60  $this->typeProcessor->getTypeData('NonExistentType');
61  }
62 
66  public function testGetTypeData()
67  {
68  $this->typeProcessor->setTypeData('typeA', ['dataA']);
69  $this->assertEquals(['dataA'], $this->typeProcessor->getTypeData('typeA'));
70  }
71 
75  public function testSetTypeDataArrayMerge()
76  {
77  $this->typeProcessor->setTypeData('typeA', ['dataA1']);
78  $this->typeProcessor->setTypeData('typeA', ['dataA2']);
79  $this->typeProcessor->setTypeData('typeA', ['dataA3']);
80  $this->typeProcessor->setTypeData('typeA', [null]);
81  $this->assertEquals(
82  ['dataA1', 'dataA2', 'dataA3', null],
83  $this->typeProcessor->getTypeData('typeA')
84  );
85  }
86 
87  public function testNormalizeType()
88  {
89  $this->assertEquals('blah', $this->typeProcessor->normalizeType('blah'));
90  $this->assertEquals('string', $this->typeProcessor->normalizeType('str'));
91  $this->assertEquals('int', $this->typeProcessor->normalizeType('integer'));
92  $this->assertEquals('boolean', $this->typeProcessor->normalizeType('bool'));
93  $this->assertEquals('anyType', $this->typeProcessor->normalizeType('mixed'));
94  }
95 
96  public function testIsTypeSimple()
97  {
98  $this->assertTrue($this->typeProcessor->isTypeSimple('string'));
99  $this->assertTrue($this->typeProcessor->isTypeSimple('string[]'));
100  $this->assertTrue($this->typeProcessor->isTypeSimple('int'));
101  $this->assertTrue($this->typeProcessor->isTypeSimple('float'));
102  $this->assertTrue($this->typeProcessor->isTypeSimple('double'));
103  $this->assertTrue($this->typeProcessor->isTypeSimple('boolean'));
104  $this->assertFalse($this->typeProcessor->isTypeSimple('blah'));
105  }
106 
107  public function testIsTypeAny()
108  {
109  $this->assertTrue($this->typeProcessor->isTypeAny('mixed'));
110  $this->assertTrue($this->typeProcessor->isTypeAny('mixed[]'));
111  $this->assertFalse($this->typeProcessor->isTypeAny('int'));
112  $this->assertFalse($this->typeProcessor->isTypeAny('int[]'));
113  }
114 
115  public function testIsArrayType()
116  {
117  $this->assertFalse($this->typeProcessor->isArrayType('string'));
118  $this->assertTrue($this->typeProcessor->isArrayType('string[]'));
119  }
120 
121  public function testIsValidTypeDeclaration()
122  {
123  $this->assertTrue($this->typeProcessor->isValidTypeDeclaration('Traversable')); // Interface
124  $this->assertTrue($this->typeProcessor->isValidTypeDeclaration('stdObj')); // Class
125  $this->assertTrue($this->typeProcessor->isValidTypeDeclaration('array'));
126  $this->assertTrue($this->typeProcessor->isValidTypeDeclaration('callable'));
127  $this->assertTrue($this->typeProcessor->isValidTypeDeclaration('self'));
128  $this->assertTrue($this->typeProcessor->isValidTypeDeclaration('self'));
129  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('string'));
130  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('string[]'));
131  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('int'));
132  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('float'));
133  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('double'));
134  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('boolean'));
135  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('[]'));
136  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('mixed[]'));
137  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('stdObj[]'));
138  $this->assertFalse($this->typeProcessor->isValidTypeDeclaration('Traversable[]'));
139  }
140 
141  public function getArrayItemType()
142  {
143  $this->assertEquals('string', $this->typeProcessor->getArrayItemType('str[]'));
144  $this->assertEquals('string', $this->typeProcessor->getArrayItemType('string[]'));
145  $this->assertEquals('integer', $this->typeProcessor->getArrayItemType('int[]'));
146  $this->assertEquals('boolean', $this->typeProcessor->getArrayItemType('bool[]'));
147  $this->assertEquals('any', $this->typeProcessor->getArrayItemType('mixed[]'));
148  }
149 
150  public function testTranslateTypeName()
151  {
152  $this->assertEquals(
153  'TestModule1V1EntityItem',
154  $this->typeProcessor->translateTypeName(\Magento\TestModule1\Service\V1\Entity\Item::class)
155  );
156  $this->assertEquals(
157  'TestModule3V1EntityParameter[]',
158  $this->typeProcessor->translateTypeName('\Magento\TestModule3\Service\V1\Entity\Parameter[]')
159  );
160  }
161 
167  {
168  $this->typeProcessor->translateTypeName('\Magento\TestModule3\V1\Parameter[]');
169  }
170 
171  public function testTranslateArrayTypeName()
172  {
173  $this->assertEquals('ArrayOfComplexType', $this->typeProcessor->translateArrayTypeName('complexType'));
174  }
175 
177  {
178  $value = 1;
179  $type = 'string';
180  $this->assertSame('1', $this->typeProcessor->processSimpleAndAnyType($value, $type));
181  }
182 
184  {
185  $value = '1';
186  $type = 'int';
187  $this->assertSame(1, $this->typeProcessor->processSimpleAndAnyType($value, $type));
188  }
189 
190  public function testProcessSimpleTypeMixed()
191  {
192  $value = 1;
193  $type = 'mixed';
194  $this->assertSame(1, $this->typeProcessor->processSimpleAndAnyType($value, $type));
195  }
196 
198  {
199  $value = [1, 2, 3, 4, 5];
200  $type = 'string[]';
201  $this->assertSame(['1', '2', '3', '4', '5'], $this->typeProcessor->processSimpleAndAnyType($value, $type));
202  }
203 
205  {
206  $value = ['1', '2', '3', '4', '5'];
207  $type = 'int[]';
208  $this->assertSame([1, 2, 3, 4, 5], $this->typeProcessor->processSimpleAndAnyType($value, $type));
209  }
210 
215  {
216  $this->expectException(
217  SerializationException::class,
218  'Invalid type for value: "' . $value . '". Expected Type: "' . $type . '"'
219  );
220  $this->typeProcessor->processSimpleAndAnyType($value, $type);
221  }
222 
226  public static function processSimpleTypeExceptionProvider()
227  {
228  return [
229  "int type, string value" => ['test', 'int'],
230  "float type, string value" => ['test', 'float'],
231  ];
232  }
233 
239  {
240  $value = 1;
241  $type = 'int[]';
242  $this->typeProcessor->processSimpleAndAnyType($value, $type);
243  }
244 
250  {
251  $class = new ClassReflection(DataObject::class);
252  $methodReflection = $class->getMethod('setName');
253  $paramsReflection = $methodReflection->getParameters();
254  $this->typeProcessor->getParamType($paramsReflection[0]);
255  }
256 
264  public function testGetArrayParamType(string $methodName, string $type)
265  {
266  $class = new ClassReflection(DataObject::class);
267  $methodReflection = $class->getMethod($methodName);
268  $params = $methodReflection->getParameters();
269  $this->assertEquals($type, $this->typeProcessor->getParamType(array_pop($params)));
270  }
271 
277  public function arrayParamTypeDataProvider()
278  {
279  return [
280  ['method name' => 'addData', 'type' => 'array[]'],
281  ['method name' => 'addObjectList', 'type' => 'TSampleInterface[]']
282  ];
283  }
284 
292  public function testGetParameterDescription(string $methodName, array $descriptions)
293  {
294  $class = new ClassReflection(DataObject::class);
295  $methodReflection = $class->getMethod($methodName);
296  $paramsReflection = $methodReflection->getParameters();
297  foreach ($paramsReflection as $paramReflection) {
298  $description = array_shift($descriptions);
299  $this->assertEquals(
300  $description,
301  $this->typeProcessor->getParamDescription($paramReflection)
302  );
303  }
304  }
305 
311  public function methodParamsDataProvider()
312  {
313  return [
314  ['method name' => 'setName', 'descriptions' => ['Name of the attribute']],
315  ['method name' => 'setData', 'descriptions' => ['Key is used as index', null]],
316  ];
317  }
318 
319  public function testGetOperationName()
320  {
321  $this->assertEquals(
322  "resNameMethodName",
323  $this->typeProcessor->getOperationName("resName", "methodName")
324  );
325  }
326 
331  {
332  $expected = [
333  'type' => 'string',
334  'isRequired' => true,
335  'description' => null,
336  'parameterCount' => 0
337  ];
338 
339  $classReflection = new ClassReflection(TSample::class);
340  $methodReflection = $classReflection->getMethod('getPropertyName');
341 
342  self::assertEquals($expected, $this->typeProcessor->getGetterReturnType($methodReflection));
343  }
344 
352  {
353  $classReflection = new ClassReflection(TSample::class);
354  $methodReflection = $classReflection->getMethod('getName');
355  $this->typeProcessor->getGetterReturnType($methodReflection);
356  }
357 }
testGetParameterDescription(string $methodName, array $descriptions)
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18