[TASK] Streamline phpdoc annotations in EXT:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / StringLengthValidatorTest.php
index 776636a..873d009 100644 (file)
@@ -1,52 +1,35 @@
 <?php
+declare(strict_types = 1);
 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
 
-/*                                                                        *
- * This script belongs to the Extbase framework.                          *
- *                                                                        *
- * It is free software; you can redistribute it and/or modify it under    *
- * the terms of the GNU Lesser General Public License as published by the *
- * Free Software Foundation, either version 3 of the License, or (at your *
- * option) any later version.                                             *
- *                                                                        *
- * This script is distributed in the hope that it will be useful, but     *
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
- * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
- * General Public License for more details.                               *
- *                                                                        *
- * You should have received a copy of the GNU Lesser General Public       *
- * License along with the script.                                         *
- * If not, see http://www.gnu.org/licenses/lgpl.html                      *
- *                                                                        *
- * The TYPO3 project - inspiring people to share!                         *
- *                                                                        */
+/*
+ * 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\Validation\Exception\InvalidValidationOptionsException;
+use TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 /**
  * Test case
  */
-class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
+class StringLengthValidatorTest extends UnitTestCase
 {
-    protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator::class;
-
-    public function setup()
-    {
-        $this->validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['translateErrorMessage'])
-            ->getMock();
-    }
-
-    /**
-     * @var \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
-     */
-    protected $validator;
-
     /**
      * @test
      */
     public function validateReturnsNoErrorIfTheGivenValueIsNull()
     {
-        $this->assertFalse($this->validator->validate(null)->hasErrors());
+        $this->assertFalse((new StringLengthValidator())->validate(null)->hasErrors());
     }
 
     /**
@@ -54,7 +37,7 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
     {
-        $this->assertFalse($this->validator->validate('')->hasErrors());
+        $this->assertFalse((new StringLengthValidator())->validate('')->hasErrors());
     }
 
     /**
@@ -62,11 +45,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
     {
-        $options = ['minimum' => 0, 'maximum' => 50];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 50]);
+
         $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
     }
 
@@ -75,11 +55,12 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
     {
-        $options = ['minimum' => 50, 'maximum' => 100];
-        $validator = $this->getMockBuilder($this->validatorClassName)
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
             ->setMethods(['translateErrorMessage'])
-            ->setConstructorArgs([$options])
+            ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
             ->getMock();
+
         $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -88,11 +69,12 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
     {
-        $options = ['minimum' => 5, 'maximum' => 10];
-        $validator = $this->getMockBuilder($this->validatorClassName)
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
             ->setMethods(['translateErrorMessage'])
-            ->setConstructorArgs([$options])
+            ->setConstructorArgs([['minimum' => 5, 'maximum' => 10]])
             ->getMock();
+
         $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -101,11 +83,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
     {
-        $options = ['minimum' => 5];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['minimum' => 5]);
+
         $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -114,11 +93,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
     {
-        $options = ['maximum' => 100];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['maximum' => 100]);
+
         $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -127,11 +103,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
     {
-        $options = ['maximum' => 10];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['maximum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -140,11 +113,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
     {
-        $options = ['minimum' => 10];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['minimum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -153,11 +123,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
     {
-        $options = ['minimum' => 10, 'maximum' => 10];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -166,11 +133,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
     {
-        $options = ['minimum' => 1, 'maximum' => 10];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['minimum' => 1, 'maximum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -179,11 +143,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
     {
-        $options = ['minimum' => 10, 'maximum' => 100];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 100]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -194,10 +155,11 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
     {
         $this->expectException(InvalidValidationOptionsException::class);
         $this->expectExceptionCode(1238107096);
-        $options = ['minimum' => 101, 'maximum' => 100];
-        $validator = $this->getMockBuilder($this->validatorClassName)
+
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
             ->setMethods(['addError', 'translateErrorMessage'])
-            ->setConstructorArgs([$options])
+            ->setConstructorArgs([['minimum' => 101, 'maximum' => 100]])
             ->getMock();
         $validator->validate('1234567890');
     }
@@ -207,12 +169,13 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
     {
-        $options = ['minimum' => 50, 'maximum' => 100];
-        $validator = $this->getMockBuilder($this->validatorClassName)
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
             ->setMethods(['translateErrorMessage'])
-            ->setConstructorArgs([$options])
+            ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
             ->getMock();
-        $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
+
+        $this->assertCount(1, $validator->validate('this is a very short string')->getErrors());
     }
 
     /**
@@ -220,23 +183,15 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
     {
-        $options = ['minimum' => 5, 'maximum' => 100];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['addError', 'translateErrorMessage'])
-            ->setConstructorArgs([$options])
-            ->getMock();
-
-        $className = $this->getUniqueId('TestClass');
+        $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
+        $object = new class() {
+            /** @return string */
+            public function __toString()
+            {
+                return 'some string';
+            }
+        };
 
-        eval('
-                       class ' . $className . ' {
-                               public function __toString() {
-                                       return \'some string\';
-                               }
-                       }
-               ');
-
-        $object = new $className();
         $this->assertFalse($validator->validate($object)->hasErrors());
     }
 
@@ -245,22 +200,18 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
     {
-        $options = ['minimum' => 5, 'maximum' => 100];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['translateErrorMessage'])
-            ->setConstructorArgs([$options])
-            ->getMock();
+        $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
+        $object = new class() {
+        };
 
-        $className = $this->getUniqueId('TestClass');
+        $result = $validator->validate($object);
 
-        eval('
-                       class ' . $className . ' {
-                               protected $someProperty;
-                       }
-               ');
+        $this->assertTrue($result->hasErrors());
+        $this->assertCount(1, $result->getErrors());
 
-        $object = new $className();
-        $this->assertTrue($validator->validate($object)->hasErrors());
+        /** @var \TYPO3\CMS\Extbase\Validation\Error $error */
+        $error = current($result->getErrors());
+        $this->assertSame(1238110957, $error->getCode());
     }
 
     /**
@@ -268,8 +219,9 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Components\TestingFramework\C
      */
     public function validateRegardsMultibyteStringsCorrectly()
     {
-        //             $this->validatorOptions(array('maximum' => 8));
-//             $this->assertFalse($this->validator->validate('├╝berlang')->hasErrors());
-        $this->markTestSkipped('Validator needs to be adjusted regarding multibyte char lengths.');
+        $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 8]);
+        $result = $validator->validate('├╝berlang');
+
+        $this->assertFalse($result->hasErrors());
     }
 }