[TASK] Cleanup StringLengthValidatorTest 77/55277/5
authorAlexander Schnitzler <git@alexanderschnitzler.de>
Fri, 5 Jan 2018 13:10:42 +0000 (14:10 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 8 Jan 2018 19:56:19 +0000 (20:56 +0100)
- Replace eval with anonymous classes
- Avoid mocks if not necessary

Releases: master
Resolves: #83482
Change-Id: I38a7b5c78ffbe6f1347e70154eb72077916892bc
Reviewed-on: https://review.typo3.org/55277
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Reiner Teubner <reiner.teubner@me.com>
Tested-by: Reiner Teubner <reiner.teubner@me.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/extbase/Tests/Unit/Validation/Validator/StringLengthValidatorTest.php

index bba1639..7a90761 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types=1);
 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
 
 /*                                                                        *
@@ -20,7 +21,9 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
  *                                                                        *
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
+
 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
+use TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator;
 
 /**
  * Test case
@@ -28,28 +31,11 @@ use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
 class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
 {
     /**
-     * @var string
-     */
-    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());
     }
 
     /**
@@ -57,7 +43,7 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     public function validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
     {
-        $this->assertFalse($this->validator->validate('')->hasErrors());
+        $this->assertFalse((new StringLengthValidator())->validate('')->hasErrors());
     }
 
     /**
@@ -65,11 +51,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -78,11 +61,12 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -91,11 +75,12 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -104,11 +89,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -117,11 +99,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -130,11 +109,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -143,11 +119,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -156,11 +129,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -169,11 +139,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -182,11 +149,8 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -197,10 +161,11 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
     {
         $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');
     }
@@ -210,12 +175,13 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
     /**
@@ -223,23 +189,15 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
@@ -248,22 +206,18 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     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());
     }
 
     /**
@@ -271,11 +225,9 @@ class StringLengthValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
      */
     public function validateRegardsMultibyteStringsCorrectly()
     {
-        $options = ['minimum' => 0, 'maximum' => 8];
-        $validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['dummy'])
-            ->setConstructorArgs([$options])
-            ->getMock();
-        $this->assertFalse($validator->validate('├╝berlang')->hasErrors());
+        $validator = new StringLengthValidator(['minimum' => 0, 'maximum' => 8]);
+        $result = $validator->validate('├╝berlang');
+
+        $this->assertFalse($result->hasErrors());
     }
 }