[TASK] Streamline phpdoc annotations in EXT:extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / StringLengthValidatorTest.php
index 3c19c00..873d009 100644 (file)
@@ -1,50 +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\Core\Tests\UnitTestCase
+class StringLengthValidatorTest extends UnitTestCase
 {
-    protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator::class;
-
-    public function setup()
-    {
-        $this->validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'));
-    }
-
-    /**
-     * @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());
     }
 
     /**
@@ -52,7 +37,7 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
     {
-        $this->assertFalse($this->validator->validate('')->hasErrors());
+        $this->assertFalse((new StringLengthValidator())->validate('')->hasErrors());
     }
 
     /**
@@ -60,8 +45,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
     {
-        $options = array('minimum' => 0, 'maximum' => 50);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 50]);
+
         $this->assertFalse($validator->validate('this is a very simple string')->hasErrors());
     }
 
@@ -70,8 +55,12 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
     {
-        $options = array('minimum' => 50, 'maximum' => 100);
-        $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
+            ->setMethods(['translateErrorMessage'])
+            ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
+            ->getMock();
+
         $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -80,8 +69,12 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
     {
-        $options = array('minimum' => 5, 'maximum' => 10);
-        $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
+            ->setMethods(['translateErrorMessage'])
+            ->setConstructorArgs([['minimum' => 5, 'maximum' => 10]])
+            ->getMock();
+
         $this->assertTrue($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -90,8 +83,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
     {
-        $options = array('minimum' => 5);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['minimum' => 5]);
+
         $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -100,8 +93,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
     {
-        $options = array('maximum' => 100);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['maximum' => 100]);
+
         $this->assertFalse($validator->validate('this is a very short string')->hasErrors());
     }
 
@@ -110,8 +103,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
     {
-        $options = array('maximum' => 10);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['maximum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -120,8 +113,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
     {
-        $options = array('minimum' => 10);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['minimum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -130,8 +123,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
     {
-        $options = array('minimum' => 10, 'maximum' => 10);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -140,8 +133,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
     {
-        $options = array('minimum' => 1, 'maximum' => 10);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['minimum' => 1, 'maximum' => 10]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -150,8 +143,8 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
     {
-        $options = array('minimum' => 10, 'maximum' => 100);
-        $validator = $this->getMock($this->validatorClassName, array('dummy'), array($options));
+        $validator = new StringLengthValidator(['minimum' => 10, 'maximum' => 100]);
+
         $this->assertFalse($validator->validate('1234567890')->hasErrors());
     }
 
@@ -162,8 +155,12 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         $this->expectException(InvalidValidationOptionsException::class);
         $this->expectExceptionCode(1238107096);
-        $options = array('minimum' => 101, 'maximum' => 100);
-        $validator = $this->getMock($this->validatorClassName, array('addError', 'translateErrorMessage'), array($options));
+
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
+            ->setMethods(['addError', 'translateErrorMessage'])
+            ->setConstructorArgs([['minimum' => 101, 'maximum' => 100]])
+            ->getMock();
         $validator->validate('1234567890');
     }
 
@@ -172,9 +169,13 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
     {
-        $options = array('minimum' => 50, 'maximum' => 100);
-        $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
-        $this->assertEquals(1, count($validator->validate('this is a very short string')->getErrors()));
+        /** @var StringLengthValidator $validator */
+        $validator = $this->getMockBuilder(StringLengthValidator::class)
+            ->setMethods(['translateErrorMessage'])
+            ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
+            ->getMock();
+
+        $this->assertCount(1, $validator->validate('this is a very short string')->getErrors());
     }
 
     /**
@@ -182,20 +183,15 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
     {
-        $options = array('minimum' => 5, 'maximum' => 100);
-        $validator = $this->getMock($this->validatorClassName, array('addError', 'translateErrorMessage'), array($options));
+        $validator = new StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
+        $object = new class() {
+            /** @return string */
+            public function __toString()
+            {
+                return 'some string';
+            }
+        };
 
-        $className = $this->getUniqueId('TestClass');
-
-        eval('
-                       class ' . $className . ' {
-                               public function __toString() {
-                                       return \'some string\';
-                               }
-                       }
-               ');
-
-        $object = new $className();
         $this->assertFalse($validator->validate($object)->hasErrors());
     }
 
@@ -204,19 +200,18 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
     {
-        $options = array('minimum' => 5, 'maximum' => 100);
-        $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
+        $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());
     }
 
     /**
@@ -224,8 +219,9 @@ class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     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());
     }
 }