[!!!][TASK] Remove deprecated Extbase-related code (Part 2)
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Property / TypeConverter / PersistentObjectConverterTest.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\Exception\DuplicateObjectException;
18 use TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException;
19 use TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException;
20 use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
21 use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter;
22 use TYPO3\CMS\Extbase\Reflection\ClassSchema;
23 use TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\PersistentObjectEntityFixture;
24 use TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\PersistentObjectFixture;
25 use TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\PersistentObjectValueObjectFixture;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28 /**
29 * Test case
30 */
31 class PersistentObjectConverterTest extends UnitTestCase
32 {
33 /**
34 * @var \TYPO3\CMS\Extbase\Property\TypeConverterInterface|\PHPUnit_Framework_MockObject_MockObject
35 */
36 protected $converter;
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService|\PHPUnit_Framework_MockObject_MockObject
40 */
41 protected $mockReflectionService;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface|\PHPUnit_Framework_MockObject_MockObject
45 */
46 protected $mockPersistenceManager;
47
48 /**
49 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
50 */
51 protected $mockObjectManager;
52
53 /**
54 * @var \TYPO3\CMS\Extbase\Object\Container\Container|\PHPUnit_Framework_MockObject_MockObject
55 */
56 protected $mockContainer;
57
58 /**
59 * @throws \InvalidArgumentException
60 * @throws \PHPUnit\Framework\Exception
61 * @throws \RuntimeException
62 */
63 protected function setUp()
64 {
65 $this->converter = new PersistentObjectConverter();
66 $this->mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
67 $this->inject($this->converter, 'reflectionService', $this->mockReflectionService);
68
69 $this->mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
70 $this->inject($this->converter, 'persistenceManager', $this->mockPersistenceManager);
71
72 $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
73 $this->mockObjectManager->expects($this->any())
74 ->method('get')
75 ->will($this->returnCallback(function ($className, ...$arguments) {
76 $reflectionClass = new \ReflectionClass($className);
77 if (empty($arguments)) {
78 return $reflectionClass->newInstance();
79 }
80 return $reflectionClass->newInstanceArgs($arguments);
81 }));
82 $this->inject($this->converter, 'objectManager', $this->mockObjectManager);
83
84 $this->mockContainer = $this->createMock(\TYPO3\CMS\Extbase\Object\Container\Container::class);
85 $this->inject($this->converter, 'objectContainer', $this->mockContainer);
86 }
87
88 /**
89 * @test
90 */
91 public function checkMetadata()
92 {
93 $this->assertEquals(['integer', 'string', 'array'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
94 $this->assertEquals('object', $this->converter->getSupportedTargetType(), 'Target type does not match');
95 $this->assertEquals(20, $this->converter->getPriority(), 'Priority does not match');
96 }
97
98 /**
99 * @return array
100 */
101 public function dataProviderForCanConvert()
102 {
103 return [
104 [true, false, true],
105 // is entity => can convert
106 [false, true, true],
107 // is valueobject => can convert
108 [false, false, false],
109 // is no entity and no value object => can not convert
110 ];
111 }
112
113 /**
114 * @test
115 * @dataProvider dataProviderForCanConvert
116 */
117 public function canConvertFromReturnsTrueIfClassIsTaggedWithEntityOrValueObject($isEntity, $isValueObject, $expected)
118 {
119 $className = PersistentObjectFixture::class;
120
121 if ($isEntity) {
122 $className = PersistentObjectEntityFixture::class;
123 } elseif ($isValueObject) {
124 $className = PersistentObjectValueObjectFixture::class;
125 }
126 $this->assertEquals($expected, $this->converter->canConvertFrom('myInputData', $className));
127 }
128
129 /**
130 * @test
131 */
132 public function getSourceChildPropertiesToBeConvertedReturnsAllPropertiesExceptTheIdentityProperty()
133 {
134 $source = [
135 'k1' => 'v1',
136 '__identity' => 'someIdentity',
137 'k2' => 'v2'
138 ];
139 $expected = [
140 'k1' => 'v1',
141 'k2' => 'v2'
142 ];
143 $this->assertEquals($expected, $this->converter->getSourceChildPropertiesToBeConverted($source));
144 }
145
146 /**
147 * @test
148 */
149 public function getTypeOfChildPropertyShouldUseReflectionServiceToDetermineType()
150 {
151 $mockSchema = $this->getMockBuilder(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class)->disableOriginalConstructor()->getMock();
152 $this->mockReflectionService->expects($this->any())->method('getClassSchema')->with('TheTargetType')->will($this->returnValue($mockSchema));
153
154 $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue('TheTargetType'));
155 $mockSchema->expects($this->any())->method('hasProperty')->with('thePropertyName')->will($this->returnValue(true));
156 $mockSchema->expects($this->any())->method('getProperty')->with('thePropertyName')->will($this->returnValue([
157 'type' => 'TheTypeOfSubObject',
158 'elementType' => null
159 ]));
160 $configuration = $this->buildConfiguration([]);
161 $this->assertEquals('TheTypeOfSubObject', $this->converter->getTypeOfChildProperty('TheTargetType', 'thePropertyName', $configuration));
162 }
163
164 /**
165 * @test
166 */
167 public function getTypeOfChildPropertyShouldUseConfiguredTypeIfItWasSet()
168 {
169 $this->mockReflectionService->expects($this->never())->method('getClassSchema');
170 $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue('foo'));
171
172 $configuration = $this->buildConfiguration([]);
173 $configuration->forProperty('thePropertyName')->setTypeConverterOption(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_TARGET_TYPE, 'Foo\Bar');
174 $this->assertEquals('Foo\Bar', $this->converter->getTypeOfChildProperty('foo', 'thePropertyName', $configuration));
175 }
176
177 /**
178 * @test
179 */
180 public function convertFromShouldFetchObjectFromPersistenceIfUuidStringIsGiven()
181 {
182 $identifier = '17';
183 $object = new \stdClass();
184
185 $this->mockPersistenceManager->expects($this->any())->method('getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
186 $this->assertSame($object, $this->converter->convertFrom($identifier, 'MySpecialType'));
187 }
188
189 /**
190 * @test
191 */
192 public function convertFromShouldFetchObjectFromPersistenceIfuidStringIsGiven()
193 {
194 $identifier = '17';
195 $object = new \stdClass();
196
197 $this->mockPersistenceManager->expects($this->any())->method('getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
198 $this->assertSame($object, $this->converter->convertFrom($identifier, 'MySpecialType'));
199 }
200
201 /**
202 * @test
203 */
204 public function convertFromShouldFetchObjectFromPersistenceIfOnlyIdentityArrayGiven()
205 {
206 $identifier = '12345';
207 $object = new \stdClass();
208
209 $source = [
210 '__identity' => $identifier
211 ];
212 $this->mockPersistenceManager->expects($this->any())->method('getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
213 $this->assertSame($object, $this->converter->convertFrom($source, 'MySpecialType'));
214 }
215
216 /**
217 * @test
218 */
219 public function convertFromShouldThrowExceptionIfObjectNeedsToBeModifiedButConfigurationIsNotSet()
220 {
221 $this->expectException(InvalidPropertyMappingConfigurationException::class);
222 $this->expectExceptionCode(1297932028);
223 $identifier = '12345';
224 $object = new \stdClass();
225 $object->someProperty = 'asdf';
226
227 $source = [
228 '__identity' => $identifier,
229 'foo' => 'bar'
230 ];
231 $this->mockPersistenceManager->expects($this->any())->method('getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
232 $this->converter->convertFrom($source, 'MySpecialType');
233 }
234
235 /**
236 * @param array $typeConverterOptions
237 * @return \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
238 */
239 protected function buildConfiguration($typeConverterOptions)
240 {
241 $configuration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
242 $configuration->setTypeConverterOptions(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, $typeConverterOptions);
243 return $configuration;
244 }
245
246 /**
247 * @param int $numberOfResults
248 * @param Matcher $howOftenIsGetFirstCalled
249 * @return \stdClass
250 */
251 public function setupMockQuery($numberOfResults, $howOftenIsGetFirstCalled)
252 {
253 $mockClassSchema = $this->getMockBuilder(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class)
254 ->setConstructorArgs([\TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\Query::class])
255 ->getMock();
256 $this->mockReflectionService->expects($this->any())->method('getClassSchema')->with('SomeType')->will($this->returnValue($mockClassSchema));
257
258 $mockConstraint = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Comparison::class)->disableOriginalConstructor()->getMock();
259
260 $mockObject = new \stdClass();
261 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
262 $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
263 $mockQueryResult->expects($this->any())->method('count')->will($this->returnValue($numberOfResults));
264 $mockQueryResult->expects($howOftenIsGetFirstCalled)->method('getFirst')->will($this->returnValue($mockObject));
265 $mockQuery->expects($this->any())->method('equals')->with('key1', 'value1')->will($this->returnValue($mockConstraint));
266 $mockQuery->expects($this->any())->method('matching')->with($mockConstraint)->will($this->returnValue($mockQuery));
267 $mockQuery->expects($this->any())->method('execute')->will($this->returnValue($mockQueryResult));
268
269 $this->mockPersistenceManager->expects($this->any())->method('createQueryForType')->with('SomeType')->will($this->returnValue($mockQuery));
270
271 return $mockObject;
272 }
273
274 /**
275 * @test
276 */
277 public function convertFromShouldReturnExceptionIfNoMatchingObjectWasFound()
278 {
279 $this->expectException(TargetNotFoundException::class);
280 $this->expectExceptionCode(1297933823);
281 $this->setupMockQuery(0, $this->never());
282 $this->mockReflectionService->expects($this->never())->method('getClassSchema');
283
284 $source = [
285 '__identity' => 123
286 ];
287 $actual = $this->converter->convertFrom($source, 'SomeType');
288 $this->assertNull($actual);
289 }
290
291 /**
292 * @test
293 */
294 public function convertFromShouldThrowExceptionIfMoreThanOneObjectWasFound()
295 {
296 $this->expectException(DuplicateObjectException::class);
297 // @TODO expectExceptionCode is 0
298 $this->setupMockQuery(2, $this->never());
299
300 $source = [
301 '__identity' => 666
302 ];
303 $this->mockPersistenceManager
304 ->expects($this->any())
305 ->method('getObjectByIdentifier')
306 ->with(666)
307 ->will($this->throwException(new DuplicateObjectException('testing', 1476107580)));
308 $this->converter->convertFrom($source, 'SomeType');
309 }
310
311 /**
312 * @test
313 */
314 public function convertFromShouldThrowExceptionIfObjectNeedsToBeCreatedButConfigurationIsNotSet()
315 {
316 $this->expectException(InvalidPropertyMappingConfigurationException::class);
317 // @TODO expectExceptionCode is 0
318 $source = [
319 'foo' => 'bar'
320 ];
321 $this->converter->convertFrom($source, 'MySpecialType');
322 }
323
324 /**
325 * @test
326 */
327 public function convertFromShouldCreateObject()
328 {
329 $source = [
330 'propertyX' => 'bar'
331 ];
332 $convertedChildProperties = [
333 'property1' => 'bar'
334 ];
335 $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters();
336 $expectedObject->property1 = 'bar';
337
338 $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
339 $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, $convertedChildProperties, $configuration);
340 $this->assertEquals($expectedObject, $result);
341 }
342
343 /**
344 * @test
345 */
346 public function convertFromShouldThrowExceptionIfPropertyOnTargetObjectCouldNotBeSet()
347 {
348 $this->expectException(InvalidTargetException::class);
349 $this->expectExceptionCode(1297935345);
350 $source = [
351 'propertyX' => 'bar'
352 ];
353 $object = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters();
354 $convertedChildProperties = [
355 'propertyNotExisting' => 'bar'
356 ];
357 $this->mockObjectManager->expects($this->any())->method('get')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class)->will($this->returnValue($object));
358 $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
359 $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, $convertedChildProperties, $configuration);
360 $this->assertSame($object, $result);
361 }
362
363 /**
364 * @test
365 */
366 public function convertFromShouldCreateObjectWhenThereAreConstructorParameters(): void
367 {
368 $classSchemaMock = $this->createMock(ClassSchema::class);
369 $classSchemaMock
370 ->expects($this->any())
371 ->method('getMethod')
372 ->with('__construct')
373 ->willReturn([
374 'params' => [
375 'property1' => ['optional' => false]
376 ]
377 ]);
378
379 $classSchemaMock
380 ->expects($this->any())
381 ->method('hasConstructor')
382 ->willReturn(true);
383
384 $this->mockReflectionService
385 ->expects($this->any())
386 ->method('getClassSchema')
387 ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)
388 ->willReturn($classSchemaMock);
389
390 $source = [
391 'propertyX' => 'bar'
392 ];
393 $convertedChildProperties = [
394 'property1' => 'param1',
395 'property2' => 'bar'
396 ];
397 $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor('param1');
398 $expectedObject->setProperty2('bar');
399
400 $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
401 $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
402 $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, $convertedChildProperties, $configuration);
403 $this->assertEquals($expectedObject, $result);
404 $this->assertEquals('bar', $expectedObject->getProperty2());
405 }
406
407 /**
408 * @test
409 */
410 public function convertFromShouldCreateObjectWhenThereAreOptionalConstructorParameters()
411 {
412 $classSchemaMock = $this->createMock(ClassSchema::class);
413 $classSchemaMock
414 ->expects($this->any())
415 ->method('getMethod')
416 ->with('__construct')
417 ->willReturn([
418 'params' => [
419 'property1' => ['optional' => true, 'defaultValue' => 'thisIsTheDefaultValue']
420 ]
421 ]);
422
423 $classSchemaMock
424 ->expects($this->any())
425 ->method('hasConstructor')
426 ->willReturn(true);
427
428 $this->mockReflectionService
429 ->expects($this->any())
430 ->method('getClassSchema')
431 ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)
432 ->willReturn($classSchemaMock);
433
434 $source = [
435 'propertyX' => 'bar'
436 ];
437 $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor('thisIsTheDefaultValue');
438
439 $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
440 $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
441 $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, [], $configuration);
442 $this->assertEquals($expectedObject, $result);
443 }
444
445 /**
446 * @test
447 */
448 public function convertFromShouldThrowExceptionIfRequiredConstructorParameterWasNotFound(): void
449 {
450 $classSchemaMock = $this->createMock(ClassSchema::class);
451 $classSchemaMock
452 ->expects($this->any())
453 ->method('getMethod')
454 ->with('__construct')
455 ->willReturn([
456 'params' => [
457 'property1' => ['optional' => false]
458 ]
459 ]);
460
461 $classSchemaMock
462 ->expects($this->any())
463 ->method('hasConstructor')
464 ->willReturn(true);
465
466 $this->mockReflectionService
467 ->expects($this->any())
468 ->method('getClassSchema')
469 ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)
470 ->willReturn($classSchemaMock);
471
472 $this->expectException(InvalidTargetException::class);
473 $this->expectExceptionCode(1268734872);
474 $source = [
475 'propertyX' => 'bar'
476 ];
477 $object = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor('param1');
478 $convertedChildProperties = [
479 'property2' => 'bar'
480 ];
481
482 $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
483 $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
484 $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, $convertedChildProperties, $configuration);
485 $this->assertSame($object, $result);
486 }
487
488 /**
489 * @test
490 */
491 public function convertFromShouldReturnNullForEmptyString()
492 {
493 $source = '';
494 $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class);
495 $this->assertNull($result);
496 }
497 }