[!!!][TASK] Replace ClassInfo with ClassSchema
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Reflection / ClassSchemaTest.php
index eb93ceb..7a2f97c 100644 (file)
@@ -14,6 +14,11 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Reflection;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
+use TYPO3\CMS\Extbase\Reflection\ClassSchema;
+use TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyModel;
+
 /**
  * Test case
  */
@@ -24,49 +29,173 @@ class ClassSchemaTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
      */
     public function classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNamespacedClasses()
     {
-        $className = $this->getUniqueId('BazFixture');
-        eval('
-                       namespace Foo\\Bar\\Domain\\Model;
-                       class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {}
-               ');
-        eval('
-                       namespace Foo\\Bar\\Domain\\Repository;
-                       class ' . $className . 'Repository {}
-               ');
-
-        /** @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
-        $objectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
-        $mockClassSchema = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class, ['dummy'], ['Foo\\Bar\\Domain\\Model\\' . $className]);
-        $objectManager->expects($this->once())->method('get')->will($this->returnValue($mockClassSchema));
-
         /** @var \TYPO3\CMS\Extbase\Reflection\ReflectionService $service */
-        $service = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, ['dummy']);
-        $service->_set('objectManager', $objectManager);
-        $classSchema = $service->getClassSchema('Foo\\Bar\\Domain\\Model\\' . $className);
+        $service = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
+        $classSchema = $service->getClassSchema(DummyModel::class);
         $this->assertTrue($classSchema->isAggregateRoot());
     }
 
-    /**
-     * @test
-     */
-    public function classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNotNamespacedClasses()
-    {
-        $className = $this->getUniqueId('BazFixture');
-        eval('
-                       class Foo_Bar_Domain_Model_' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' {}
-               ');
-        eval('
-                       class Foo_Bar_Domain_Repository_' . $className . 'Repository {}
-               ');
-
-        /** @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
-        $objectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
-        $mockClassSchema = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class, ['dummy'], ['Foo_Bar_Domain_Model_' . $className]);
-        $objectManager->expects($this->once())->method('get')->will($this->returnValue($mockClassSchema));
-
-        $service = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, ['dummy']);
-        $service->_set('objectManager', $objectManager);
-        $classSchema = $service->getClassSchema('Foo_Bar_Domain_Model_' . $className);
-        $this->assertTrue($classSchema->isAggregateRoot());
+    public function testClassSchemaHasConstructor()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArguments::class);
+        static::assertTrue($classSchema->hasConstructor());
+    }
+
+    public function testClassSchemaDetectsConstructorArguments()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArguments::class);
+        static::assertTrue($classSchema->hasConstructor());
+
+        $methodDefinition = $classSchema->getMethod('__construct');
+        static::assertArrayHasKey('foo', $methodDefinition['params']);
+        static::assertArrayHasKey('bar', $methodDefinition['params']);
+    }
+
+    public function testClassSchemaDetectsConstructorArgumentsWithDependencies()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArgumentsWithDependencies::class);
+        static::assertTrue($classSchema->hasConstructor());
+
+        $methodDefinition = $classSchema->getMethod('__construct');
+        static::assertArrayHasKey('foo', $methodDefinition['params']);
+        static::assertSame(Fixture\DummyClassWithGettersAndSetters::class, $methodDefinition['params']['foo']['dependency']);
+    }
+
+    public function testClassSchemaDetectsMethodVisibility()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
+
+        $methodDefinition = $classSchema->getMethod('publicMethod');
+        static::assertTrue($methodDefinition['public']);
+        static::assertFalse($methodDefinition['protected']);
+        static::assertFalse($methodDefinition['private']);
+
+        $methodDefinition = $classSchema->getMethod('protectedMethod');
+        static::assertFalse($methodDefinition['public']);
+        static::assertTrue($methodDefinition['protected']);
+        static::assertFalse($methodDefinition['private']);
+
+        $methodDefinition = $classSchema->getMethod('privateMethod');
+        static::assertFalse($methodDefinition['public']);
+        static::assertFalse($methodDefinition['protected']);
+        static::assertTrue($methodDefinition['private']);
+    }
+
+    public function testClassSchemaDetectsInjectMethods()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
+
+        $methodDefinition = $classSchema->getMethod('injectSettings');
+        static::assertFalse($methodDefinition['injectMethod']);
+
+        $methodDefinition = $classSchema->getMethod('injectMethodWithoutParam');
+        static::assertFalse($methodDefinition['injectMethod']);
+
+        $methodDefinition = $classSchema->getMethod('injectMethodThatIsProtected');
+        static::assertFalse($methodDefinition['injectMethod']);
+
+        $methodDefinition = $classSchema->getMethod('injectFoo');
+        static::assertTrue($methodDefinition['injectMethod']);
+    }
+
+    public function testClassSchemaDetectsStaticMethods()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
+
+        $methodDefinition = $classSchema->getMethod('staticMethod');
+        static::assertTrue($methodDefinition['static']);
+    }
+
+    public function testClassSchemaDetectsMandatoryParams()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
+
+        $methodDefinition = $classSchema->getMethod('methodWithMandatoryParam');
+        static::assertFalse($methodDefinition['params']['param']['optional']);
+    }
+
+    public function testClassSchemaDetectsNullableParams()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
+
+        $methodDefinition = $classSchema->getMethod('methodWithNullableParam');
+        static::assertTrue($methodDefinition['params']['param']['nullable']);
+    }
+
+    public function testClassSchemaDetectsDefaultValueParams()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
+
+        $methodDefinition = $classSchema->getMethod('methodWithDefaultValueParam');
+        static::assertSame('foo', $methodDefinition['params']['param']['default']);
+    }
+
+    public function testClassSchemaDetectsParamTypeFromTypeHint()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
+
+        $methodDefinition = $classSchema->getMethod('methodWithTypeHintedParam');
+        static::assertSame('string', $methodDefinition['params']['param']['type']);
+    }
+
+    public function testClassSchemaDetectsPropertyVisibility()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
+
+        $propertyDefinition = $classSchema->getProperty('publicProperty');
+        static::assertTrue($propertyDefinition['public']);
+        static::assertFalse($propertyDefinition['protected']);
+        static::assertFalse($propertyDefinition['private']);
+
+        $propertyDefinition = $classSchema->getProperty('protectedProperty');
+        static::assertFalse($propertyDefinition['public']);
+        static::assertTrue($propertyDefinition['protected']);
+        static::assertFalse($propertyDefinition['private']);
+
+        $propertyDefinition = $classSchema->getProperty('privateProperty');
+        static::assertFalse($propertyDefinition['public']);
+        static::assertFalse($propertyDefinition['protected']);
+        static::assertTrue($propertyDefinition['private']);
+    }
+
+    public function testClassSchemaDetectsInjectProperty()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
+
+        $propertyDefinition = $classSchema->getProperty('propertyWithInjectAnnotation');
+        static::assertTrue($propertyDefinition['annotations']['inject']);
+    }
+
+    public function testClassSchemaDetectsTransientProperty()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
+
+        $propertyDefinition = $classSchema->getProperty('propertyWithTransientAnnotation');
+        static::assertTrue($propertyDefinition['annotations']['transient']);
+    }
+
+    public function testClassSchemaDetectsCascadeProperty()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
+
+        $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotation');
+        static::assertSame('remove', $propertyDefinition['annotations']['cascade']);
+    }
+
+    public function testClassSchemaDetectsCascadePropertyOnlyWithVarAnnotation()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
+
+        $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotationWithoutVarAnnotation');
+        static::assertNull($propertyDefinition['annotations']['cascade']);
+    }
+
+    public function testClassSchemaDetectsTypeAndElementType()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
+
+        $propertyDefinition = $classSchema->getProperty('propertyWithObjectStorageAnnotation');
+        static::assertSame(ObjectStorage::class, $propertyDefinition['type']);
+        static::assertSame(Fixture\DummyClassWithAllTypesOfProperties::class, $propertyDefinition['elementType']);
     }
 }