TYPO3 CMS  TYPO3_8-7
ObjectConverterTest.php
Go to the documentation of this file.
1 <?php
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  * */
25 
29 class ObjectConverterTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
30 {
34  protected $converter;
35 
40 
44  protected $mockObjectManager;
45 
49  protected $mockContainer;
50 
55  protected function setUp()
56  {
57  $this->mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
58  $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
59  $this->mockContainer = $this->createMock(\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 
70  public function checkMetadata()
71  {
72  $this->assertEquals(['array'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
73  $this->assertEquals('object', $this->converter->getSupportedTargetType(), 'Target type does not match');
74  $this->assertEquals(10, $this->converter->getPriority(), 'Priority does not match');
75  }
76 
80  public function dataProviderForCanConvert()
81  {
82  return [
83  // Is entity => cannot convert
84  [\TYPO3\CMS\Extbase\Tests\Fixture\Entity::class, false],
85  // Is valueobject => cannot convert
86  [\TYPO3\CMS\Extbase\Tests\Fixture\ValueObject::class, false],
87  // Is no entity and no value object => can convert
88  ['stdClass', true]
89  ];
90  }
91 
99  {
100  $this->assertEquals($expected, $this->converter->canConvertFrom('myInputData', $className));
101  }
102 
107  {
108  $this->mockReflectionService->expects($this->any())->method('hasMethod')->with('TheTargetType', 'setThePropertyName')->will($this->returnValue(false));
109  $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with('TheTargetType', '__construct')->will($this->returnValue([
110  'thePropertyName' => [
111  'type' => 'TheTypeOfSubObject',
112  'elementType' => null
113  ]
114  ]));
115  $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue('TheTargetType'));
116 
117  $configuration = new PropertyMappingConfiguration();
118  $configuration->setTypeConverterOptions(\TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter::class, []);
119  $this->assertEquals('TheTypeOfSubObject', $this->converter->getTypeOfChildProperty('TheTargetType', 'thePropertyName', $configuration));
120  }
121 }