[!!!][TASK] Replace ClassInfo with ClassSchema
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Property / TypeConverter / PersistentObjectConverterTest.php
index d4a3c5f..a289057 100644 (file)
@@ -20,7 +20,10 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter;
  *                                                                        *
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
-use TYPO3\CMS\Core\Tests\UnitTestCase;
+use TYPO3\CMS\Extbase\Property\Exception\DuplicateObjectException;
+use TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException;
+use TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException;
+use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
 use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter;
 use TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\PersistentObjectEntityFixture;
 use TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\PersistentObjectFixture;
@@ -29,7 +32,7 @@ use TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\PersistentObjec
 /**
  * Test case
  */
-class PersistentObjectConverterTest extends UnitTestCase
+class PersistentObjectConverterTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
 {
     /**
      * @var \TYPO3\CMS\Extbase\Property\TypeConverterInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -58,32 +61,31 @@ class PersistentObjectConverterTest extends UnitTestCase
 
     /**
      * @throws \InvalidArgumentException
-     * @throws \PHPUnit_Framework_Exception
+     * @throws \PHPUnit\Framework\Exception
      * @throws \RuntimeException
      */
     protected function setUp()
     {
         $this->converter = new PersistentObjectConverter();
-        $this->mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
+        $this->mockReflectionService = $this->createMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
         $this->inject($this->converter, 'reflectionService', $this->mockReflectionService);
 
-        $this->mockPersistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
+        $this->mockPersistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
         $this->inject($this->converter, 'persistenceManager', $this->mockPersistenceManager);
 
-        $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
+        $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
         $this->mockObjectManager->expects($this->any())
             ->method('get')
             ->will($this->returnCallback(function ($className, ...$arguments) {
-                    $reflectionClass = new \ReflectionClass($className);
-                    if (empty($arguments)) {
-                        return $reflectionClass->newInstance();
-                    } else {
-                        return $reflectionClass->newInstanceArgs($arguments);
-                    }
-                }));
+                $reflectionClass = new \ReflectionClass($className);
+                if (empty($arguments)) {
+                    return $reflectionClass->newInstance();
+                }
+                return $reflectionClass->newInstanceArgs($arguments);
+            }));
         $this->inject($this->converter, 'objectManager', $this->mockObjectManager);
 
-        $this->mockContainer = $this->getMock(\TYPO3\CMS\Extbase\Object\Container\Container::class);
+        $this->mockContainer = $this->createMock(\TYPO3\CMS\Extbase\Object\Container\Container::class);
         $this->inject($this->converter, 'objectContainer', $this->mockContainer);
     }
 
@@ -92,9 +94,9 @@ class PersistentObjectConverterTest extends UnitTestCase
      */
     public function checkMetadata()
     {
-        $this->assertEquals(array('integer', 'string', 'array'), $this->converter->getSupportedSourceTypes(), 'Source types do not match');
+        $this->assertEquals(['integer', 'string', 'array'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
         $this->assertEquals('object', $this->converter->getSupportedTargetType(), 'Target type does not match');
-        $this->assertEquals(1, $this->converter->getPriority(), 'Priority does not match');
+        $this->assertEquals(20, $this->converter->getPriority(), 'Priority does not match');
     }
 
     /**
@@ -102,14 +104,14 @@ class PersistentObjectConverterTest extends UnitTestCase
      */
     public function dataProviderForCanConvert()
     {
-        return array(
-            array(true, false, true),
+        return [
+            [true, false, true],
             // is entity => can convert
-            array(false, true, true),
+            [false, true, true],
             // is valueobject => can convert
-            array(false, false, false),
+            [false, false, false],
             // is no entity and no value object => can not convert
-        );
+        ];
     }
 
     /**
@@ -133,15 +135,15 @@ class PersistentObjectConverterTest extends UnitTestCase
      */
     public function getSourceChildPropertiesToBeConvertedReturnsAllPropertiesExceptTheIdentityProperty()
     {
-        $source = array(
+        $source = [
             'k1' => 'v1',
             '__identity' => 'someIdentity',
             'k2' => 'v2'
-        );
-        $expected = array(
+        ];
+        $expected = [
             'k1' => 'v1',
             'k2' => 'v2'
-        );
+        ];
         $this->assertEquals($expected, $this->converter->getSourceChildPropertiesToBeConverted($source));
     }
 
@@ -155,11 +157,11 @@ class PersistentObjectConverterTest extends UnitTestCase
 
         $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue('TheTargetType'));
         $mockSchema->expects($this->any())->method('hasProperty')->with('thePropertyName')->will($this->returnValue(true));
-        $mockSchema->expects($this->any())->method('getProperty')->with('thePropertyName')->will($this->returnValue(array(
+        $mockSchema->expects($this->any())->method('getProperty')->with('thePropertyName')->will($this->returnValue([
             'type' => 'TheTypeOfSubObject',
             'elementType' => null
-        )));
-        $configuration = $this->buildConfiguration(array());
+        ]));
+        $configuration = $this->buildConfiguration([]);
         $this->assertEquals('TheTypeOfSubObject', $this->converter->getTypeOfChildProperty('TheTargetType', 'thePropertyName', $configuration));
     }
 
@@ -171,7 +173,7 @@ class PersistentObjectConverterTest extends UnitTestCase
         $this->mockReflectionService->expects($this->never())->method('getClassSchema');
         $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue('foo'));
 
-        $configuration = $this->buildConfiguration(array());
+        $configuration = $this->buildConfiguration([]);
         $configuration->forProperty('thePropertyName')->setTypeConverterOption(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, PersistentObjectConverter::CONFIGURATION_TARGET_TYPE, 'Foo\Bar');
         $this->assertEquals('Foo\Bar', $this->converter->getTypeOfChildProperty('foo', 'thePropertyName', $configuration));
     }
@@ -208,27 +210,28 @@ class PersistentObjectConverterTest extends UnitTestCase
         $identifier = '12345';
         $object = new \stdClass();
 
-        $source = array(
+        $source = [
             '__identity' => $identifier
-        );
+        ];
         $this->mockPersistenceManager->expects($this->any())->method('getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
         $this->assertSame($object, $this->converter->convertFrom($source, 'MySpecialType'));
     }
 
     /**
      * @test
-     * @expectedException \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
      */
     public function convertFromShouldThrowExceptionIfObjectNeedsToBeModifiedButConfigurationIsNotSet()
     {
+        $this->expectException(InvalidPropertyMappingConfigurationException::class);
+        $this->expectExceptionCode(1297932028);
         $identifier = '12345';
         $object = new \stdClass();
         $object->someProperty = 'asdf';
 
-        $source = array(
+        $source = [
             '__identity' => $identifier,
             'foo' => 'bar'
-        );
+        ];
         $this->mockPersistenceManager->expects($this->any())->method('getObjectByIdentifier')->with($identifier)->will($this->returnValue($object));
         $this->converter->convertFrom($source, 'MySpecialType');
     }
@@ -251,15 +254,17 @@ class PersistentObjectConverterTest extends UnitTestCase
      */
     public function setupMockQuery($numberOfResults, $howOftenIsGetFirstCalled)
     {
-        $mockClassSchema = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class, array(), array('Dummy'));
-        $mockClassSchema->expects($this->any())->method('getIdentityProperties')->will($this->returnValue(array('key1' => 'someType')));
+        $mockClassSchema = $this->getMockBuilder(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class)
+            ->setConstructorArgs([\TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\Fixtures\Query::class])
+            ->getMock();
+        $mockClassSchema->expects($this->any())->method('getIdentityProperties')->will($this->returnValue(['key1' => 'someType']));
         $this->mockReflectionService->expects($this->any())->method('getClassSchema')->with('SomeType')->will($this->returnValue($mockClassSchema));
 
         $mockConstraint = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Comparison::class)->disableOriginalConstructor()->getMock();
 
         $mockObject = new \stdClass();
-        $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
-        $mockQueryResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
+        $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
+        $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
         $mockQueryResult->expects($this->any())->method('count')->will($this->returnValue($numberOfResults));
         $mockQueryResult->expects($howOftenIsGetFirstCalled)->method('getFirst')->will($this->returnValue($mockObject));
         $mockQuery->expects($this->any())->method('equals')->with('key1', 'value1')->will($this->returnValue($mockConstraint));
@@ -273,44 +278,51 @@ class PersistentObjectConverterTest extends UnitTestCase
 
     /**
      * @test
-     * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
      */
     public function convertFromShouldReturnExceptionIfNoMatchingObjectWasFound()
     {
+        $this->expectException(TargetNotFoundException::class);
+        $this->expectExceptionCode(1297933823);
         $this->setupMockQuery(0, $this->never());
         $this->mockReflectionService->expects($this->never())->method('getClassSchema');
 
-        $source = array(
+        $source = [
             '__identity' => 123
-        );
+        ];
         $actual = $this->converter->convertFrom($source, 'SomeType');
         $this->assertNull($actual);
     }
 
     /**
      * @test
-     * @expectedException \TYPO3\CMS\Extbase\Property\Exception\DuplicateObjectException
      */
     public function convertFromShouldThrowExceptionIfMoreThanOneObjectWasFound()
     {
+        $this->expectException(DuplicateObjectException::class);
+        // @TODO expectExceptionCode is 0
         $this->setupMockQuery(2, $this->never());
 
-        $source = array(
+        $source = [
             '__identity' => 666
-        );
-        $this->mockPersistenceManager->expects($this->any())->method('getObjectByIdentifier')->with(666)->will($this->throwException(new \TYPO3\CMS\Extbase\Property\Exception\DuplicateObjectException));
+        ];
+        $this->mockPersistenceManager
+            ->expects($this->any())
+            ->method('getObjectByIdentifier')
+            ->with(666)
+            ->will($this->throwException(new DuplicateObjectException('testing', 1476107580)));
         $this->converter->convertFrom($source, 'SomeType');
     }
 
     /**
      * @test
-     * @expectedException \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
      */
     public function convertFromShouldThrowExceptionIfObjectNeedsToBeCreatedButConfigurationIsNotSet()
     {
-        $source = array(
+        $this->expectException(InvalidPropertyMappingConfigurationException::class);
+        // @TODO expectExceptionCode is 0
+        $source = [
             'foo' => 'bar'
-        );
+        ];
         $this->converter->convertFrom($source, 'MySpecialType');
     }
 
@@ -319,38 +331,42 @@ class PersistentObjectConverterTest extends UnitTestCase
      */
     public function convertFromShouldCreateObject()
     {
-        $source = array(
+        $source = [
             'propertyX' => 'bar'
-        );
-        $convertedChildProperties = array(
+        ];
+        $convertedChildProperties = [
             'property1' => 'bar'
-        );
+        ];
         $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters();
         $expectedObject->property1 = 'bar';
 
-        $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, '__construct')->will($this->throwException(new \ReflectionException()));
-        $this->mockObjectManager->expects($this->any())->method('getClassNameByObjectName')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class)->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class));
-        $configuration = $this->buildConfiguration(array(PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true));
+        $this->mockReflectionService
+            ->expects($this->any())
+            ->method('getMethodParameters')
+            ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, '__construct')
+            ->will($this->throwException(new \ReflectionException('testing', 1476107618)));
+        $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
         $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, $convertedChildProperties, $configuration);
         $this->assertEquals($expectedObject, $result);
     }
 
     /**
      * @test
-     * @expectedException \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
      */
     public function convertFromShouldThrowExceptionIfPropertyOnTargetObjectCouldNotBeSet()
     {
-        $source = array(
+        $this->expectException(InvalidTargetException::class);
+        $this->expectExceptionCode(1297935345);
+        $source = [
             'propertyX' => 'bar'
-        );
+        ];
         $object = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters();
-        $convertedChildProperties = array(
+        $convertedChildProperties = [
             'propertyNotExisting' => 'bar'
-        );
+        ];
         $this->mockObjectManager->expects($this->any())->method('get')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class)->will($this->returnValue($object));
-        $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, '__construct')->will($this->returnValue(array()));
-        $configuration = $this->buildConfiguration(array(PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true));
+        $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, '__construct')->will($this->returnValue([]));
+        $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
         $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSetters::class, $convertedChildProperties, $configuration);
         $this->assertSame($object, $result);
     }
@@ -360,13 +376,13 @@ class PersistentObjectConverterTest extends UnitTestCase
      */
     public function convertFromShouldCreateObjectWhenThereAreConstructorParameters()
     {
-        $source = array(
+        $source = [
             'propertyX' => 'bar'
-        );
-        $convertedChildProperties = array(
+        ];
+        $convertedChildProperties = [
             'property1' => 'param1',
             'property2' => 'bar'
-        );
+        ];
         $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor('param1');
         $expectedObject->setProperty2('bar');
 
@@ -374,17 +390,16 @@ class PersistentObjectConverterTest extends UnitTestCase
                 ->expects($this->any())
                 ->method('getMethodParameters')
                 ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')
-                ->will($this->returnValue(array(
-                    'property1' => array('optional' => false)
-                )));
+                ->will($this->returnValue([
+                    'property1' => ['optional' => false]
+                ]));
         $this->mockReflectionService
                 ->expects($this->any())
                 ->method('hasMethod')
                 ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')
                 ->will($this->returnValue(true));
-        $this->mockObjectManager->expects($this->any())->method('getClassNameByObjectName')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
         $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
-        $configuration = $this->buildConfiguration(array(PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true));
+        $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
         $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, $convertedChildProperties, $configuration);
         $this->assertEquals($expectedObject, $result);
         $this->assertEquals('bar', $expectedObject->getProperty2());
@@ -395,51 +410,50 @@ class PersistentObjectConverterTest extends UnitTestCase
      */
     public function convertFromShouldCreateObjectWhenThereAreOptionalConstructorParameters()
     {
-        $source = array(
+        $source = [
             'propertyX' => 'bar'
-        );
+        ];
         $expectedObject = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor('thisIsTheDefaultValue');
 
-        $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')->will($this->returnValue(array(
-            'property1' => array('optional' => true, 'defaultValue' => 'thisIsTheDefaultValue')
-        )));
+        $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')->will($this->returnValue([
+            'property1' => ['optional' => true, 'defaultValue' => 'thisIsTheDefaultValue']
+        ]));
         $this->mockReflectionService
                 ->expects($this->any())
                 ->method('hasMethod')
                 ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')
                 ->will($this->returnValue(true));
-        $this->mockObjectManager->expects($this->any())->method('getClassNameByObjectName')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
         $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
-        $configuration = $this->buildConfiguration(array(PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true));
-        $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, array(), $configuration);
+        $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
+        $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, [], $configuration);
         $this->assertEquals($expectedObject, $result);
     }
 
     /**
      * @test
-     * @expectedException \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
      */
     public function convertFromShouldThrowExceptionIfRequiredConstructorParameterWasNotFound()
     {
-        $source = array(
+        $this->expectException(InvalidTargetException::class);
+        $this->expectExceptionCode(1268734872);
+        $source = [
             'propertyX' => 'bar'
-        );
+        ];
         $object = new \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor('param1');
-        $convertedChildProperties = array(
+        $convertedChildProperties = [
             'property2' => 'bar'
-        );
+        ];
 
-        $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')->will($this->returnValue(array(
-            'property1' => array('optional' => false)
-        )));
+        $this->mockReflectionService->expects($this->any())->method('getMethodParameters')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')->will($this->returnValue([
+            'property1' => ['optional' => false]
+        ]));
         $this->mockReflectionService
                 ->expects($this->any())
                 ->method('hasMethod')
                 ->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, '__construct')
                 ->will($this->returnValue(true));
-        $this->mockObjectManager->expects($this->any())->method('getClassNameByObjectName')->with(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class)->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
         $this->mockContainer->expects($this->any())->method('getImplementationClassName')->will($this->returnValue(\TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class));
-        $configuration = $this->buildConfiguration(array(PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true));
+        $configuration = $this->buildConfiguration([PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true]);
         $result = $this->converter->convertFrom($source, \TYPO3\CMS\Extbase\Tests\Fixture\ClassWithSettersAndConstructor::class, $convertedChildProperties, $configuration);
         $this->assertSame($object, $result);
     }