Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Member Functions
TypeProcessorTest Class Reference
Inheritance diagram for TypeProcessorTest:

Public Member Functions

 testGetTypesData ()
 
 testSetTypesData ()
 
 testGetTypeDataInvalidArgumentException ()
 
 testGetTypeData ()
 
 testSetTypeDataArrayMerge ()
 
 testNormalizeType ()
 
 testIsTypeSimple ()
 
 testIsTypeAny ()
 
 testIsArrayType ()
 
 testIsValidTypeDeclaration ()
 
 getArrayItemType ()
 
 testTranslateTypeName ()
 
 testTranslateTypeNameInvalidArgumentException ()
 
 testTranslateArrayTypeName ()
 
 testProcessSimpleTypeIntToString ()
 
 testProcessSimpleTypeStringToInt ()
 
 testProcessSimpleTypeMixed ()
 
 testProcessSimpleTypeIntArrayToStringArray ()
 
 testProcessSimpleTypeStringArrayToIntArray ()
 
 testProcessSimpleTypeException ($value, $type)
 
 testProcessSimpleTypeInvalidType ()
 
 testGetParamTypeWithIncorrectAnnotation ()
 
 testGetArrayParamType (string $methodName, string $type)
 
 arrayParamTypeDataProvider ()
 
 testGetParameterDescription (string $methodName, array $descriptions)
 
 methodParamsDataProvider ()
 
 testGetOperationName ()
 
 testGetReturnTypeWithInheritDocBlock ()
 
 testGetReturnTypeWithoutReturnTag ()
 

Static Public Member Functions

static processSimpleTypeExceptionProvider ()
 

Protected Member Functions

 setUp ()
 

Detailed Description

Definition at line 14 of file TypeProcessorTest.php.

Member Function Documentation

◆ arrayParamTypeDataProvider()

arrayParamTypeDataProvider ( )

Get list of methods with expected param types.

Returns
array

Definition at line 277 of file TypeProcessorTest.php.

278  {
279  return [
280  ['method name' => 'addData', 'type' => 'array[]'],
281  ['method name' => 'addObjectList', 'type' => 'TSampleInterface[]']
282  ];
283  }

◆ getArrayItemType()

getArrayItemType ( )

Definition at line 141 of file TypeProcessorTest.php.

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  }

◆ methodParamsDataProvider()

methodParamsDataProvider ( )

Gets list of method names with params and their descriptions.

Returns
array

Definition at line 311 of file TypeProcessorTest.php.

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  }

◆ processSimpleTypeExceptionProvider()

static processSimpleTypeExceptionProvider ( )
static
Returns
array

Definition at line 226 of file TypeProcessorTest.php.

227  {
228  return [
229  "int type, string value" => ['test', 'int'],
230  "float type, string value" => ['test', 'float'],
231  ];
232  }

◆ setUp()

setUp ( )
protected

Set up helper.

Definition at line 24 of file TypeProcessorTest.php.

25  {
26  $this->typeProcessor = new TypeProcessor();
27  }

◆ testGetArrayParamType()

testGetArrayParamType ( string  $methodName,
string  $type 
)

Checks a case for different array param types.

Parameters
string$methodName
string$type@dataProvider arrayParamTypeDataProvider

Definition at line 264 of file TypeProcessorTest.php.

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  }
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ testGetOperationName()

testGetOperationName ( )

Definition at line 319 of file TypeProcessorTest.php.

320  {
321  $this->assertEquals(
322  "resNameMethodName",
323  $this->typeProcessor->getOperationName("resName", "methodName")
324  );
325  }

◆ testGetParameterDescription()

testGetParameterDescription ( string  $methodName,
array  $descriptions 
)

Checks a case when method param has additional description.

Parameters
string$methodName
array$descriptions@dataProvider methodParamsDataProvider

Definition at line 292 of file TypeProcessorTest.php.

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  }
$_option $_optionId $class
Definition: date.phtml:13

◆ testGetParamTypeWithIncorrectAnnotation()

testGetParamTypeWithIncorrectAnnotation ( )

@expectedException \LogicException @expectedExceptionMessageRegExp /

Parameters
annotationis incorrect for the parameter "name" \w+/

Definition at line 249 of file TypeProcessorTest.php.

250  {
251  $class = new ClassReflection(DataObject::class);
252  $methodReflection = $class->getMethod('setName');
253  $paramsReflection = $methodReflection->getParameters();
254  $this->typeProcessor->getParamType($paramsReflection[0]);
255  }
$_option $_optionId $class
Definition: date.phtml:13

◆ testGetReturnTypeWithInheritDocBlock()

testGetReturnTypeWithInheritDocBlock ( )

Checks a case when method has only @inheritdoc annotation.

Definition at line 330 of file TypeProcessorTest.php.

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  }

◆ testGetReturnTypeWithoutReturnTag()

testGetReturnTypeWithoutReturnTag ( )

Checks a case when method and parent interface don't have @return annotation.

@expectedException \InvalidArgumentException @expectedExceptionMessage Method's return type must be specified using

Returns
annotation. See Magento\Framework\Reflection\Test\Unit\Fixture\TSample::getName()

Definition at line 351 of file TypeProcessorTest.php.

352  {
353  $classReflection = new ClassReflection(TSample::class);
354  $methodReflection = $classReflection->getMethod('getName');
355  $this->typeProcessor->getGetterReturnType($methodReflection);
356  }

◆ testGetTypeData()

testGetTypeData ( )

Test retrieval of data type details for the given type name.

Definition at line 66 of file TypeProcessorTest.php.

67  {
68  $this->typeProcessor->setTypeData('typeA', ['dataA']);
69  $this->assertEquals(['dataA'], $this->typeProcessor->getTypeData('typeA'));
70  }

◆ testGetTypeDataInvalidArgumentException()

testGetTypeDataInvalidArgumentException ( )

@expectedException \InvalidArgumentException @expectedExceptionMessage The "NonExistentType" data type isn't declared. Verify the type and try again.

Definition at line 58 of file TypeProcessorTest.php.

59  {
60  $this->typeProcessor->getTypeData('NonExistentType');
61  }

◆ testGetTypesData()

testGetTypesData ( )

Test Retrieving of processed types data.

Definition at line 32 of file TypeProcessorTest.php.

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  }

◆ testIsArrayType()

testIsArrayType ( )

Definition at line 115 of file TypeProcessorTest.php.

116  {
117  $this->assertFalse($this->typeProcessor->isArrayType('string'));
118  $this->assertTrue($this->typeProcessor->isArrayType('string[]'));
119  }

◆ testIsTypeAny()

testIsTypeAny ( )

Definition at line 107 of file TypeProcessorTest.php.

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  }

◆ testIsTypeSimple()

testIsTypeSimple ( )

Definition at line 96 of file TypeProcessorTest.php.

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  }

◆ testIsValidTypeDeclaration()

testIsValidTypeDeclaration ( )

Definition at line 121 of file TypeProcessorTest.php.

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  }

◆ testNormalizeType()

testNormalizeType ( )

Definition at line 87 of file TypeProcessorTest.php.

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  }

◆ testProcessSimpleTypeException()

testProcessSimpleTypeException (   $value,
  $type 
)

@dataProvider processSimpleTypeExceptionProvider

Definition at line 214 of file TypeProcessorTest.php.

215  {
216  $this->expectException(
217  SerializationException::class,
218  'Invalid type for value: "' . $value . '". Expected Type: "' . $type . '"'
219  );
220  $this->typeProcessor->processSimpleAndAnyType($value, $type);
221  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ testProcessSimpleTypeIntArrayToStringArray()

testProcessSimpleTypeIntArrayToStringArray ( )

Definition at line 197 of file TypeProcessorTest.php.

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  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ testProcessSimpleTypeIntToString()

testProcessSimpleTypeIntToString ( )

Definition at line 176 of file TypeProcessorTest.php.

177  {
178  $value = 1;
179  $type = 'string';
180  $this->assertSame('1', $this->typeProcessor->processSimpleAndAnyType($value, $type));
181  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ testProcessSimpleTypeInvalidType()

testProcessSimpleTypeInvalidType ( )

@expectedException \Magento\Framework\Exception\SerializationException @expectedExceptionMessage The "integer" value's type is invalid. The "int[]" type was expected. Verify and try again.

Definition at line 238 of file TypeProcessorTest.php.

239  {
240  $value = 1;
241  $type = 'int[]';
242  $this->typeProcessor->processSimpleAndAnyType($value, $type);
243  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ testProcessSimpleTypeMixed()

testProcessSimpleTypeMixed ( )

Definition at line 190 of file TypeProcessorTest.php.

191  {
192  $value = 1;
193  $type = 'mixed';
194  $this->assertSame(1, $this->typeProcessor->processSimpleAndAnyType($value, $type));
195  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ testProcessSimpleTypeStringArrayToIntArray()

testProcessSimpleTypeStringArrayToIntArray ( )

Definition at line 204 of file TypeProcessorTest.php.

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  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ testProcessSimpleTypeStringToInt()

testProcessSimpleTypeStringToInt ( )

Definition at line 183 of file TypeProcessorTest.php.

184  {
185  $value = '1';
186  $type = 'int';
187  $this->assertSame(1, $this->typeProcessor->processSimpleAndAnyType($value, $type));
188  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ testSetTypeDataArrayMerge()

testSetTypeDataArrayMerge ( )

Test data type details for the same type name set multiple times.

Definition at line 75 of file TypeProcessorTest.php.

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  }

◆ testSetTypesData()

testSetTypesData ( )

Test set of processed types data.

Definition at line 45 of file TypeProcessorTest.php.

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  }

◆ testTranslateArrayTypeName()

testTranslateArrayTypeName ( )

Definition at line 171 of file TypeProcessorTest.php.

172  {
173  $this->assertEquals('ArrayOfComplexType', $this->typeProcessor->translateArrayTypeName('complexType'));
174  }

◆ testTranslateTypeName()

testTranslateTypeName ( )

Definition at line 150 of file TypeProcessorTest.php.

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  }

◆ testTranslateTypeNameInvalidArgumentException()

testTranslateTypeNameInvalidArgumentException ( )

@expectedException \InvalidArgumentException @expectedExceptionMessage The "\Magento\TestModule3\V1\Parameter[]" parameter type is invalid. Verify the parameter and try again.

Definition at line 166 of file TypeProcessorTest.php.

167  {
168  $this->typeProcessor->translateTypeName('\Magento\TestModule3\V1\Parameter[]');
169  }

The documentation for this class was generated from the following file: