[TASK] Separately extract @validate annotations 38/54838/3
authorAlexander Schnitzler <git@alexanderschnitzler.de>
Tue, 28 Nov 2017 18:01:10 +0000 (19:01 +0100)
committerBenni Mack <benni@typo3.org>
Tue, 28 Nov 2017 22:07:51 +0000 (23:07 +0100)
Separately exctracting the @validate annotations
makes resolving these annotations more testable.

Also this allows to deprecate the usage of @validate
in favor of a doctrine annotation.

Releases: master
Resolves: #83143
Change-Id: I4dfd93b58e08cd5c458abebee36e82e9ce03ad47
Reviewed-on: https://review.typo3.org/54838
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/extbase/Classes/Reflection/ClassSchema.php
typo3/sysext/extbase/Tests/Unit/Reflection/ClassSchemaTest.php
typo3/sysext/extbase/Tests/Unit/Reflection/Fixture/DummyClassWithValidateAnnotation.php [new file with mode: 0644]

index 7b86160..e0683f2 100644 (file)
@@ -179,6 +179,11 @@ class ClassSchema
             $this->properties[$propertyName]['annotations']['type'] = null;
             $this->properties[$propertyName]['annotations']['cascade'] = null;
             $this->properties[$propertyName]['annotations']['dependency'] = null;
+            $this->properties[$propertyName]['annotations']['validators'] = [];
+
+            if ($docCommentParser->isTaggedWith('validate')) {
+                $this->properties[$propertyName]['annotations']['validators'] = $docCommentParser->getTagValues('validate');
+            }
 
             if ($annotationReader->getPropertyAnnotation($reflectionProperty, Lazy::class) instanceof Lazy) {
                 $this->properties[$propertyName]['annotations']['lazy'] = true;
@@ -301,9 +306,13 @@ class ClassSchema
             $this->methods[$methodName]['abstract']     = $reflectionMethod->isAbstract();
             $this->methods[$methodName]['params']       = [];
             $this->methods[$methodName]['tags']         = [];
+            $this->methods[$methodName]['annotations']  = [];
 
             $docCommentParser = new DocCommentParser(true);
             $docCommentParser->parseDocComment($reflectionMethod->getDocComment());
+
+            $this->methods[$methodName]['annotations']['validators'] = [];
+
             foreach ($docCommentParser->getTagsValues() as $tag => $values) {
                 if ($tag === 'ignorevalidation') {
                     trigger_error(
@@ -311,6 +320,9 @@ class ClassSchema
                         E_USER_DEPRECATED
                     );
                 }
+                if ($tag === 'validate') {
+                    $this->methods[$methodName]['annotations']['validators'] = $values;
+                }
                 $this->methods[$methodName]['tags'][$tag] = array_map(function ($value) {
                     return ltrim($value, '$');
                 }, $values);
index db68e41..7d03fab 100644 (file)
@@ -347,4 +347,37 @@ class ClassSchemaTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         static::assertArrayNotHasKey('author', $tags);
         static::assertArrayNotHasKey('version', $tags);
     }
+
+    /**
+     * @test
+     */
+    public function classSchemaDetectsValidateAnnotation()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithValidateAnnotation::class);
+
+        static::assertSame(
+            [],
+            $classSchema->getProperty('propertyWithoutValidateAnnotations')['annotations']['validators']
+        );
+        static::assertSame(
+            [
+                'NotEmpty',
+                'Empty (Foo=Bar)'
+            ],
+            $classSchema->getProperty('propertyWithValidateAnnotations')['annotations']['validators']
+        );
+
+        static::assertSame(
+            [],
+            $classSchema->getMethod('methodWithoutValidateAnnotations')['annotations']['validators']
+        );
+
+        static::assertSame(
+            [
+                '$fooParam FooValidator (FooValidatorOptionKey=FooValidatorOptionValue)',
+                '$fooParam BarValidator'
+            ],
+            $classSchema->getMethod('methodWithValidateAnnotations')['annotations']['validators']
+        );
+    }
 }
diff --git a/typo3/sysext/extbase/Tests/Unit/Reflection/Fixture/DummyClassWithValidateAnnotation.php b/typo3/sysext/extbase/Tests/Unit/Reflection/Fixture/DummyClassWithValidateAnnotation.php
new file mode 100644 (file)
index 0000000..51314b1
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
+
+/**
+ * Fixture class with @validate annotations
+ */
+class DummyClassWithValidateAnnotation extends AbstractEntity
+{
+    protected $propertyWithoutValidateAnnotations;
+
+    /**
+     * @validate NotEmpty
+     * @validate Empty (Foo=Bar)
+     */
+    protected $propertyWithValidateAnnotations;
+
+    public function methodWithoutValidateAnnotations()
+    {
+    }
+
+    /**
+     * @param $fooParam
+     * @validate $fooParam FooValidator (FooValidatorOptionKey=FooValidatorOptionValue)
+     * @validate $fooParam BarValidator
+     */
+    public function methodWithValidateAnnotations($fooParam)
+    {
+    }
+}