[TASK] Use name-resolution instead of strings where possible: 3
[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 script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 use TYPO3\CMS\Core\Tests\UnitTestCase;
24 use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration;
25 use TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter;
26
27 /**
28 * Test case
29 */
30 class ObjectConverterTest extends UnitTestCase {
31
32 /**
33 * @var ObjectConverter
34 */
35 protected $converter;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService|\PHPUnit_Framework_MockObject_MockObject
39 */
40 protected $mockReflectionService;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $mockObjectManager;
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Object\Container\Container|\PHPUnit_Framework_MockObject_MockObject
49 */
50 protected $mockContainer;
51
52 /**
53 * @throws \InvalidArgumentException
54 * @throws \RuntimeException
55 */
56 public function setUp() {
57 $this->mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
58 $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
59 $this->mockContainer = $this->getMock(\TYPO3\CMS\Extbase\Object\Container\Container::class);
60
61 $this->converter = new ObjectConverter();
62 $this->inject($this->converter, 'reflectionService', $this->mockReflectionService);
63 $this->inject($this->converter, 'objectManager', $this->mockObjectManager);
64 $this->inject($this->converter, 'objectContainer', $this->mockContainer);
65 }
66
67 /**
68 * @test
69 */
70 public function checkMetadata() {
71 $this->assertEquals(array('array'), $this->converter->getSupportedSourceTypes(), 'Source types do not match');
72 $this->assertEquals('object', $this->converter->getSupportedTargetType(), 'Target type does not match');
73 $this->assertEquals(0, $this->converter->getPriority(), 'Priority does not match');
74 }
75
76 /**
77 * @return array
78 */
79 public function dataProviderForCanConvert() {
80 return array(
81 // Is entity => cannot convert
82 array(\TYPO3\CMS\Extbase\Tests\Fixture\Entity::class, FALSE),
83 // Is valueobject => cannot convert
84 array(\TYPO3\CMS\Extbase\Tests\Fixture\ValueObject::class, FALSE),
85 // Is no entity and no value object => can convert
86 array('stdClass', TRUE)
87 );
88 }
89
90 /**
91 * @test
92 * @dataProvider dataProviderForCanConvert
93 * @param $className
94 * @param $expected
95 */
96 public function canConvertFromReturnsTrueIfClassIsTaggedWithEntityOrValueObject($className, $expected) {
97 $this->assertEquals($expected, $this->converter->canConvertFrom('myInputData', $className));
98 }
99
100 /**
101 * @test
102 */
103 public function getTypeOfChildPropertyShouldUseReflectionServiceToDetermineType() {
104 $this->mockReflectionService->expects($this->any())->method('hasMethod')->with('TheTargetType', 'setThePropertyName')->will($this->returnValue(FALSE));
105 $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with('TheTargetType', '__construct')->will($this->returnValue(array(
106 'thePropertyName' => array(
107 'type' => 'TheTypeOfSubObject',
108 'elementType' => NULL
109 )
110 )));
111 $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue('TheTargetType'));
112
113 $configuration = new PropertyMappingConfiguration();
114 $configuration->setTypeConverterOptions(\TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter::class, array());
115 $this->assertEquals('TheTypeOfSubObject', $this->converter->getTypeOfChildProperty('TheTargetType', 'thePropertyName', $configuration));
116 }
117
118 }