[TASK] Streamline phpdoc annotations in EXT:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Property / TypeConverter / ObjectConverterTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration;
18 use TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter;
19 use TYPO3\CMS\Extbase\Reflection\ClassSchema;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class ObjectConverterTest extends UnitTestCase
26 {
27 /**
28 * @var ObjectConverter
29 */
30 protected $converter;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService|\PHPUnit_Framework_MockObject_MockObject
34 */
35 protected $mockReflectionService;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
39 */
40 protected $mockObjectManager;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Object\Container\Container|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $mockContainer;
46
47 /**
48 * @throws \InvalidArgumentException
49 * @throws \RuntimeException
50 */
51 protected function setUp()
52 {
53 $this->mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
54 $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
55 $this->mockContainer = $this->createMock(\TYPO3\CMS\Extbase\Object\Container\Container::class);
56
57 $this->converter = new ObjectConverter();
58 $this->inject($this->converter, 'reflectionService', $this->mockReflectionService);
59 $this->inject($this->converter, 'objectManager', $this->mockObjectManager);
60 $this->inject($this->converter, 'objectContainer', $this->mockContainer);
61 }
62
63 /**
64 * @test
65 */
66 public function checkMetadata()
67 {
68 $this->assertEquals(['array'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
69 $this->assertEquals('object', $this->converter->getSupportedTargetType(), 'Target type does not match');
70 $this->assertEquals(10, $this->converter->getPriority(), 'Priority does not match');
71 }
72
73 /**
74 * @return array
75 */
76 public function dataProviderForCanConvert()
77 {
78 return [
79 // Is entity => cannot convert
80 [\TYPO3\CMS\Extbase\Tests\Fixture\Entity::class, false],
81 // Is valueobject => cannot convert
82 [\TYPO3\CMS\Extbase\Tests\Fixture\ValueObject::class, false],
83 // Is no entity and no value object => can convert
84 ['stdClass', true]
85 ];
86 }
87
88 /**
89 * @test
90 * @dataProvider dataProviderForCanConvert
91 * @param $className
92 * @param $expected
93 */
94 public function canConvertFromReturnsTrueIfClassIsTaggedWithEntityOrValueObject($className, $expected)
95 {
96 $this->assertEquals($expected, $this->converter->canConvertFrom('myInputData', $className));
97 }
98
99 /**
100 * @test
101 */
102 public function getTypeOfChildPropertyShouldUseReflectionServiceToDetermineType(): void
103 {
104 $classSchemaMock = $this->createMock(ClassSchema::class);
105 $classSchemaMock->expects($this->any())->method('getMethod')->with('__construct')->willReturn([
106 'params' => [
107 'thePropertyName' => [
108 'type' => 'TheTypeOfSubObject',
109 'elementType' => null
110 ]
111 ]
112 ]);
113
114 $this->mockReflectionService
115 ->expects($this->any())
116 ->method('getClassSchema')
117 ->with('TheTargetType')
118 ->willReturn($classSchemaMock);
119
120 $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue('TheTargetType'));
121
122 $configuration = new PropertyMappingConfiguration();
123 $configuration->setTypeConverterOptions(\TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter::class, []);
124 $this->assertEquals('TheTypeOfSubObject', $this->converter->getTypeOfChildProperty('TheTargetType', 'thePropertyName', $configuration));
125 }
126 }