26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
63 protected function setUp()
66 $this->mockReflectionService = $this->createMock(\
TYPO3\CMS\
Extbase\Reflection\ReflectionService::class);
67 $this->inject($this->converter,
'reflectionService', $this->mockReflectionService);
69 $this->mockPersistenceManager = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\PersistenceManagerInterface::class);
70 $this->inject($this->converter,
'persistenceManager', $this->mockPersistenceManager);
72 $this->mockObjectManager = $this->createMock(\
TYPO3\CMS\
Extbase\Object\ObjectManagerInterface::class);
73 $this->mockObjectManager->expects($this->any())
75 ->will($this->returnCallback(
function ($className, ...$arguments) {
76 $reflectionClass = new \ReflectionClass($className);
77 if (empty($arguments)) {
78 return $reflectionClass->newInstance();
80 return $reflectionClass->newInstanceArgs($arguments);
82 $this->inject($this->converter,
'objectManager', $this->mockObjectManager);
84 $this->mockContainer = $this->createMock(\
TYPO3\CMS\
Extbase\Object\Container\Container::class);
85 $this->inject($this->converter,
'objectContainer', $this->mockContainer);
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');
108 [
false,
false,
false],
119 $className = PersistentObjectFixture::class;
122 $className = PersistentObjectEntityFixture::class;
123 } elseif ($isValueObject) {
124 $className = PersistentObjectValueObjectFixture::class;
126 $this->assertEquals($expected, $this->converter->canConvertFrom(
'myInputData', $className));
136 '__identity' =>
'someIdentity',
143 $this->assertEquals($expected, $this->converter->getSourceChildPropertiesToBeConverted($source));
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));
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
161 $this->assertEquals(
'TheTypeOfSubObject', $this->converter->getTypeOfChildProperty(
'TheTargetType',
'thePropertyName', $configuration));
169 $this->mockReflectionService->expects($this->never())->method(
'getClassSchema');
170 $this->mockContainer->expects($this->any())->method(
'getImplementationClassName')->will($this->returnValue(
'foo'));
174 $this->assertEquals(
'Foo\Bar', $this->converter->getTypeOfChildProperty(
'foo',
'thePropertyName', $configuration));
183 $object = new \stdClass();
185 $this->mockPersistenceManager->expects($this->any())->method(
'getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
186 $this->assertSame($object, $this->converter->convertFrom($identifier,
'MySpecialType'));
195 $object = new \stdClass();
197 $this->mockPersistenceManager->expects($this->any())->method(
'getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
198 $this->assertSame($object, $this->converter->convertFrom($identifier,
'MySpecialType'));
206 $identifier =
'12345';
207 $object = new \stdClass();
210 '__identity' => $identifier
212 $this->mockPersistenceManager->expects($this->any())->method(
'getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
213 $this->assertSame($object, $this->converter->convertFrom($source,
'MySpecialType'));
221 $this->expectException(InvalidPropertyMappingConfigurationException::class);
222 $this->expectExceptionCode(1297932028);
223 $identifier =
'12345';
224 $object = new \stdClass();
225 $object->someProperty =
'asdf';
228 '__identity' => $identifier,
231 $this->mockPersistenceManager->expects($this->any())->method(
'getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
232 $this->converter->convertFrom($source,
'MySpecialType');
241 $configuration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
242 $configuration->setTypeConverterOptions(\
TYPO3\CMS\
Extbase\Property\TypeConverter\PersistentObjectConverter::class, $typeConverterOptions);
243 return $configuration;
251 public function setupMockQuery($numberOfResults, $howOftenIsGetFirstCalled)
253 $mockClassSchema = $this->getMockBuilder(\
TYPO3\CMS\
Extbase\Reflection\ClassSchema::class)
254 ->setConstructorArgs([\
TYPO3\CMS\
Extbase\Tests\Unit\Property\TypeConverter\Fixtures\Query::class])
256 $mockClassSchema->expects($this->any())->method(
'getIdentityProperties')->will($this->returnValue([
'key1' =>
'someType']));
257 $this->mockReflectionService->expects($this->any())->method(
'getClassSchema')->with(
'SomeType')->will($this->returnValue($mockClassSchema));
259 $mockConstraint = $this->getMockBuilder(\
TYPO3\CMS\
Extbase\Persistence\Generic\Qom\Comparison::class)->disableOriginalConstructor()->getMock();
261 $mockObject = new \stdClass();
262 $mockQuery = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryInterface::class);
263 $mockQueryResult = $this->createMock(\
TYPO3\CMS\
Extbase\Persistence\QueryResultInterface::class);
264 $mockQueryResult->expects($this->any())->method(
'count')->will($this->returnValue($numberOfResults));
265 $mockQueryResult->expects($howOftenIsGetFirstCalled)->method(
'getFirst')->will($this->returnValue($mockObject));
266 $mockQuery->expects($this->any())->method(
'equals')->with(
'key1',
'value1')->will($this->returnValue($mockConstraint));
267 $mockQuery->expects($this->any())->method(
'matching')->with($mockConstraint)->will($this->returnValue($mockQuery));
268 $mockQuery->expects($this->any())->method(
'execute')->will($this->returnValue($mockQueryResult));
270 $this->mockPersistenceManager->expects($this->any())->method(
'createQueryForType')->with(
'SomeType')->will($this->returnValue($mockQuery));
280 $this->expectException(TargetNotFoundException::class);
281 $this->expectExceptionCode(1297933823);
283 $this->mockReflectionService->expects($this->never())->method(
'getClassSchema');
288 $actual = $this->converter->convertFrom($source,
'SomeType');
289 $this->assertNull($actual);
297 $this->expectException(DuplicateObjectException::class);
304 $this->mockPersistenceManager
305 ->expects($this->any())
306 ->method(
'getObjectByIdentifier')
309 $this->converter->convertFrom($source,
'SomeType');
317 $this->expectException(InvalidPropertyMappingConfigurationException::class);
322 $this->converter->convertFrom($source,
'MySpecialType');
333 $convertedChildProperties = [
336 $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters();
337 $expectedObject->property1 =
'bar';
339 $this->mockReflectionService
340 ->expects($this->any())
341 ->method(
'getMethodParameters')
342 ->with(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSetters::class,
'__construct')
343 ->will($this->throwException(
new \ReflectionException(
'testing', 1476107618)));
345 $result = $this->converter->convertFrom($source, \
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSetters::class, $convertedChildProperties, $configuration);
346 $this->assertEquals($expectedObject, $result);
354 $this->expectException(InvalidTargetException::class);
355 $this->expectExceptionCode(1297935345);
359 $object = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters();
360 $convertedChildProperties = [
361 'propertyNotExisting' =>
'bar'
363 $this->mockObjectManager->expects($this->any())->method(
'get')->with(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSetters::class)->will($this->returnValue($object));
364 $this->mockReflectionService->expects($this->any())->method(
'getMethodParameters')->with(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSetters::class,
'__construct')->will($this->returnValue([]));
366 $result = $this->converter->convertFrom($source, \
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSetters::class, $convertedChildProperties, $configuration);
367 $this->assertSame($object, $result);
375 $classSchemaMock = $this->createMock(ClassSchema::class);
377 ->expects($this->any())
378 ->method(
'getMethod')
379 ->with(
'__construct')
382 'property1' => [
'optional' =>
false]
387 ->expects($this->any())
388 ->method(
'hasConstructor')
391 $this->mockReflectionService
392 ->expects($this->any())
393 ->method(
'getClassSchema')
394 ->with(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)
395 ->willReturn($classSchemaMock);
400 $convertedChildProperties = [
401 'property1' =>
'param1',
404 $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor(
'param1');
405 $expectedObject->setProperty2(
'bar');
407 $this->mockContainer->expects($this->any())->method(
'getImplementationClassName')->will($this->returnValue(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
409 $result = $this->converter->convertFrom($source, \
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, $convertedChildProperties, $configuration);
410 $this->assertEquals($expectedObject, $result);
411 $this->assertEquals(
'bar', $expectedObject->getProperty2());
419 $classSchemaMock = $this->createMock(ClassSchema::class);
421 ->expects($this->any())
422 ->method(
'getMethod')
423 ->with(
'__construct')
426 'property1' => [
'optional' =>
true,
'defaultValue' =>
'thisIsTheDefaultValue']
431 ->expects($this->any())
432 ->method(
'hasConstructor')
435 $this->mockReflectionService
436 ->expects($this->any())
437 ->method(
'getClassSchema')
438 ->with(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)
439 ->willReturn($classSchemaMock);
444 $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor(
'thisIsTheDefaultValue');
446 $this->mockContainer->expects($this->any())->method(
'getImplementationClassName')->will($this->returnValue(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
448 $result = $this->converter->convertFrom($source, \
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, [], $configuration);
449 $this->assertEquals($expectedObject, $result);
457 $classSchemaMock = $this->createMock(ClassSchema::class);
459 ->expects($this->any())
460 ->method(
'getMethod')
461 ->with(
'__construct')
464 'property1' => [
'optional' =>
false]
469 ->expects($this->any())
470 ->method(
'hasConstructor')
473 $this->mockReflectionService
474 ->expects($this->any())
475 ->method(
'getClassSchema')
476 ->with(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)
477 ->willReturn($classSchemaMock);
479 $this->expectException(InvalidTargetException::class);
480 $this->expectExceptionCode(1268734872);
484 $object = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor(
'param1');
485 $convertedChildProperties = [
489 $this->mockContainer->expects($this->any())->method(
'getImplementationClassName')->will($this->returnValue(\
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
491 $result = $this->converter->convertFrom($source, \
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, $convertedChildProperties, $configuration);
492 $this->assertSame($object, $result);
501 $result = $this->converter->convertFrom($source, \
TYPO3\CMS\
Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class);
502 $this->assertNull($result);