‪TYPO3CMS  11.5
RegularExpressionValidatorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪RegularExpressionValidatorTest extends UnitTestCase
28 {
33  {
34  $options = ['regularExpression' => '/^simple[0-9]expression$/'];
35  ‪$validator = $this->getMockBuilder(RegularExpressionValidator::class)
36  ->onlyMethods(['getErrorMessage'])
37  ->setConstructorArgs([$options])
38  ->getMock();
39  self::assertFalse(‪$validator->validate('simple1expression')->hasErrors());
40  self::assertTrue(‪$validator->validate('simple1expressions')->hasErrors());
41  }
42 
47  {
48  $options = ['regularExpression' => '/^simple[0-9]expression$/'];
49  ‪$validator = $this->getMockBuilder(RegularExpressionValidator::class)
50  ->onlyMethods(['getErrorMessage'])
51  ->setConstructorArgs([$options])
52  ->getMock();
53  ‪$errors = ‪$validator->validate('some subject that will not match')->getErrors();
54  // we only test for the error code, after the translation Method for message is mocked anyway
55  self::assertEquals([new ‪Error('', 1221565130)], ‪$errors);
56  }
57 
61  public function ‪customErrorMessageIsRespected(): void
62  {
63  $options = [
64  'regularExpression' => '/^simple[0-9]expression$/',
65  'errorMessage' => 'custom message',
66  ];
68  $result = ‪$validator->validate('some subject that will not match');
69  self::assertTrue($result->hasErrors());
70  ‪$errors = $result->getErrors();
71  self::assertEquals([new ‪Error('custom message', 1221565130)], ‪$errors);
72  }
73 
78  {
79  $options = [
80  'regularExpression' => '/^simple[0-9]expression$/',
81  ];
82  ‪$validator = $this->getMockBuilder(RegularExpressionValidator::class)
83  ->onlyMethods(['translateErrorMessage'])
84  ->setConstructorArgs([$options])
85  ->getMock();
86  ‪$validator->expects(self::once())->method('translateErrorMessage')
87  ->willReturn('default message');
88  $result = ‪$validator->validate('some subject that will not match');
89  self::assertTrue($result->hasErrors());
90  ‪$errors = $result->getErrors();
91  self::assertEquals([new ‪Error('default message', 1221565130)], ‪$errors);
92  }
93 
97  public function ‪customErrorMessageIsTranslated(): void
98  {
99  $options = [
100  'regularExpression' => '/^simple[0-9]expression$/',
101  'errorMessage' => 'LLL:demo/Resources/',
102  ];
103  ‪$validator = $this->getMockBuilder(RegularExpressionValidator::class)
104  ->onlyMethods(['translateErrorMessage'])
105  ->setConstructorArgs([$options])
106  ->getMock();
107  ‪$validator->expects(self::once())->method('translateErrorMessage')
108  ->willReturn('custom translated message');
109  $result = ‪$validator->validate('some subject that will not match');
110  self::assertTrue($result->hasErrors());
111  ‪$errors = $result->getErrors();
112  self::assertEquals([new ‪Error('custom translated message', 1221565130)], ‪$errors);
113  }
114 }
‪TYPO3\CMS\Extbase\Validation\Validator\RegularExpressionValidator
Definition: RegularExpressionValidator.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\RegularExpressionValidatorTest\customErrorMessageIsRespected
‪customErrorMessageIsRespected()
Definition: RegularExpressionValidatorTest.php:61
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\RegularExpressionValidatorTest\regularExpressionValidatorMatchesABasicExpressionCorrectly
‪regularExpressionValidatorMatchesABasicExpressionCorrectly()
Definition: RegularExpressionValidatorTest.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\RegularExpressionValidatorTest\getErrorMessageReturnsDefaultLabelIfNoCustomIsDefined
‪getErrorMessageReturnsDefaultLabelIfNoCustomIsDefined()
Definition: RegularExpressionValidatorTest.php:77
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Validation\Error
Definition: Error.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\RegularExpressionValidatorTest\regularExpressionValidatorCreatesTheCorrectErrorIfTheExpressionDidNotMatch
‪regularExpressionValidatorCreatesTheCorrectErrorIfTheExpressionDidNotMatch()
Definition: RegularExpressionValidatorTest.php:46
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\RegularExpressionValidatorTest
Definition: RegularExpressionValidatorTest.php:28
‪$errors
‪$errors
Definition: annotationChecker.php:123
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\RegularExpressionValidatorTest\customErrorMessageIsTranslated
‪customErrorMessageIsTranslated()
Definition: RegularExpressionValidatorTest.php:97
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:18