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

Releases: master
Resolves: #83483
Change-Id: Id7fca4bbe43d907378031ce7b8f35ed4a364bcc7
Reviewed-on: https://review.typo3.org/55278
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/StringValidatorTest.php

index 1f93da3..2b0b538 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types=1);
 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
 
 /*                                                                        *
@@ -21,31 +22,21 @@ namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator;
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
 
+use TYPO3\CMS\Extbase\Validation\Validator\StringValidator;
+
 /**
  * Testcase for the string length validator
  *
  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  */
-class StringValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\AbstractValidatorTestcase
+class StringValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
 {
     /**
-     * @var string
-     */
-    protected $validatorClassName = \TYPO3\CMS\Extbase\Validation\Validator\StringValidator::class;
-
-    public function setup()
-    {
-        $this->validator = $this->getMockBuilder($this->validatorClassName)
-            ->setMethods(['translateErrorMessage'])
-            ->getMock();
-    }
-
-    /**
      * @test
      */
     public function stringValidatorShouldValidateString()
     {
-        $this->assertFalse($this->validator->validate('Hello World')->hasErrors());
+        $this->assertFalse((new StringValidator())->validate('Hello World')->hasErrors());
     }
 
     /**
@@ -53,7 +44,12 @@ class StringValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\Validation\Valid
      */
     public function stringValidatorShouldReturnErrorIfNumberIsGiven()
     {
-        $this->assertTrue($this->validator->validate(42)->hasErrors());
+        /** @var StringValidator $validator */
+        $validator = $this->getMockBuilder(StringValidator::class)
+            ->setMethods(['translateErrorMessage'])
+            ->getMock();
+
+        $this->assertTrue($validator->validate(42)->hasErrors());
     }
 
     /**
@@ -61,15 +57,19 @@ class StringValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\Validation\Valid
      */
     public function stringValidatorShouldReturnErrorIfObjectWithToStringMethodStringIsGiven()
     {
-        $className = $this->getUniqueId('TestClass');
-        eval('
-                       class ' . $className . ' {
-                               public function __toString() {
-                                       return "ASDF";
-                               }
-                       }
-               ');
-        $object = new $className();
-        $this->assertTrue($this->validator->validate($object)->hasErrors());
+        /** @var StringValidator $validator */
+        $validator = $this->getMockBuilder(StringValidator::class)
+            ->setMethods(['translateErrorMessage'])
+            ->getMock();
+
+        $object = new class() {
+            /** @return string */
+            public function __toString()
+            {
+                return 'ASDF';
+            }
+        };
+
+        $this->assertTrue($validator->validate($object)->hasErrors());
     }
 }