[FEATURE] Backport Object Type Converter from Flow
[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
24 /**
25 * Testcase for the ObjectConverter
26 *
27 * @covers \TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter<extended>
28 */
29 class ObjectConverterTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter
33 */
34 protected $converter;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
38 */
39 protected $mockReflectionService;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
43 */
44 protected $mockObjectManager;
45
46 public function setUp() {
47 $this->mockReflectionService = $this->getMock('TYPO3\CMS\Extbase\Reflection\ReflectionService');
48 $this->mockObjectManager = $this->getMock('TYPO3\CMS\Extbase\Object\ObjectManagerInterface');
49
50 $this->converter = new \TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter();
51 $this->inject($this->converter, 'reflectionService', $this->mockReflectionService);
52 $this->inject($this->converter, 'objectManager', $this->mockObjectManager);
53 }
54
55 /**
56 * @test
57 */
58 public function checkMetadata() {
59 $this->assertEquals(array('array'), $this->converter->getSupportedSourceTypes(), 'Source types do not match');
60 $this->assertEquals('object', $this->converter->getSupportedTargetType(), 'Target type does not match');
61 $this->assertEquals(0, $this->converter->getPriority(), 'Priority does not match');
62 }
63
64 public function dataProviderForCanConvert() {
65 return array(
66 // Is entity => cannot convert
67 array('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\Entity', FALSE),
68 // Is valueobject => cannot convert
69 array('TYPO3\\CMS\\Extbase\\Tests\\Fixture\\ValueObject', FALSE),
70 // Is no entity and no value object => can convert
71 array('stdClass', TRUE)
72 );
73 }
74
75 /**
76 * @test
77 * @dataProvider dataProviderForCanConvert
78 */
79 public function canConvertFromReturnsTrueIfClassIsTaggedWithEntityOrValueObject($className, $expected) {
80 $this->assertEquals($expected, $this->converter->canConvertFrom('myInputData', $className));
81 }
82
83 /**
84 * @test
85 */
86 public function getTypeOfChildPropertyShouldUseReflectionServiceToDetermineType() {
87 $this->mockReflectionService->expects($this->any())->method('hasMethod')->with('TheTargetType', 'setThePropertyName')->will($this->returnValue(FALSE));
88 $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with('TheTargetType', '__construct')->will($this->returnValue(array(
89 'thePropertyName' => array(
90 'type' => 'TheTypeOfSubObject',
91 'elementType' => NULL
92 )
93 )));
94 $configuration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
95 $configuration->setTypeConverterOptions('TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter', array());
96 $this->assertEquals('TheTypeOfSubObject', $this->converter->getTypeOfChildProperty('TheTargetType', 'thePropertyName', $configuration));
97 }
98
99 }
100 ?>