‪TYPO3CMS  10.4
StringLengthValidatorTest.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 ‪StringLengthValidatorTest extends UnitTestCase
28 {
33  {
34  self::assertFalse((new ‪StringLengthValidator())->validate(null)->hasErrors());
35  }
36 
41  {
42  self::assertFalse((new ‪StringLengthValidator())->validate('')->hasErrors());
43  }
44 
49  {
50  ‪$validator = new ‪StringLengthValidator(['minimum' => 0, 'maximum' => 50]);
51 
52  self::assertFalse(‪$validator->validate('this is a very simple string')->hasErrors());
53  }
54 
59  {
61  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
62  ->setMethods(['translateErrorMessage'])
63  ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
64  ->getMock();
65 
66  self::assertTrue(‪$validator->validate('this is a very short string')->hasErrors());
67  }
68 
73  {
75  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
76  ->setMethods(['translateErrorMessage'])
77  ->setConstructorArgs([['minimum' => 5, 'maximum' => 10]])
78  ->getMock();
79 
80  self::assertTrue(‪$validator->validate('this is a very short string')->hasErrors());
81  }
82 
87  {
88  ‪$validator = new ‪StringLengthValidator(['minimum' => 5]);
89 
90  self::assertFalse(‪$validator->validate('this is a very short string')->hasErrors());
91  }
92 
97  {
98  ‪$validator = new ‪StringLengthValidator(['maximum' => 100]);
99 
100  self::assertFalse(‪$validator->validate('this is a very short string')->hasErrors());
101  }
102 
107  {
108  ‪$validator = new ‪StringLengthValidator(['maximum' => 10]);
109 
110  self::assertFalse(‪$validator->validate('1234567890')->hasErrors());
111  }
112 
117  {
118  ‪$validator = new ‪StringLengthValidator(['minimum' => 10]);
119 
120  self::assertFalse(‪$validator->validate('1234567890')->hasErrors());
121  }
122 
127  {
128  ‪$validator = new ‪StringLengthValidator(['minimum' => 10, 'maximum' => 10]);
129 
130  self::assertFalse(‪$validator->validate('1234567890')->hasErrors());
131  }
132 
137  {
138  ‪$validator = new ‪StringLengthValidator(['minimum' => 1, 'maximum' => 10]);
139 
140  self::assertFalse(‪$validator->validate('1234567890')->hasErrors());
141  }
142 
147  {
148  ‪$validator = new ‪StringLengthValidator(['minimum' => 10, 'maximum' => 100]);
149 
150  self::assertFalse(‪$validator->validate('1234567890')->hasErrors());
151  }
152 
157  {
158  $this->expectException(InvalidValidationOptionsException::class);
159  $this->expectExceptionCode(1238107096);
160 
162  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
163  ->setMethods(['addError', 'translateErrorMessage'])
164  ->setConstructorArgs([['minimum' => 101, 'maximum' => 100]])
165  ->getMock();
166  ‪$validator->validate('1234567890');
167  }
168 
173  {
175  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
176  ->setMethods(['translateErrorMessage'])
177  ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
178  ->getMock();
179 
180  self::assertCount(1, ‪$validator->validate('this is a very short string')->getErrors());
181  }
182 
187  {
188  ‪$validator = new ‪StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
189  $object = new class() {
191  public function __toString()
192  {
193  return 'some string';
194  }
195  };
196 
197  self::assertFalse(‪$validator->validate($object)->hasErrors());
198  }
199 
204  {
205  ‪$validator = new ‪StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
206  $object = new class() {
207  };
208 
209  $result = ‪$validator->validate($object);
210 
211  self::assertTrue($result->hasErrors());
212  self::assertCount(1, $result->getErrors());
213 
215  $error = current($result->getErrors());
216  self::assertSame(1238110957, $error->getCode());
217  }
218 
223  {
224  ‪$validator = new ‪StringLengthValidator(['minimum' => 0, 'maximum' => 8]);
225  $result = ‪$validator->validate('überlang');
226 
227  self::assertFalse($result->hasErrors());
228  }
229 }
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength
‪stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength()
Definition: StringLengthValidatorTest.php:156
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue
‪stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
Definition: StringLengthValidatorTest.php:126
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength
‪stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
Definition: StringLengthValidatorTest.php:58
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorInsertsAnErrorObjectIfValidationFails
‪stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
Definition: StringLengthValidatorTest.php:172
‪TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
Definition: StringLengthValidator.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
Definition: StringLengthValidatorTest.php:86
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
Definition: StringLengthValidatorTest.php:96
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateReturnsNoErrorIfTheGivenValueIsNull
‪validateReturnsNoErrorIfTheGivenValueIsNull()
Definition: StringLengthValidatorTest.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength
‪stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
Definition: StringLengthValidatorTest.php:146
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsIfTheStringLengthIsEqualToMaxLength
‪stringLengthValidatorReturnsNoErrorsIfTheStringLengthIsEqualToMaxLength()
Definition: StringLengthValidatorTest.php:136
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength
‪stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
Definition: StringLengthValidatorTest.php:72
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString
‪validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
Definition: StringLengthValidatorTest.php:203
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorCanHandleAnObjectWithAToStringMethod
‪stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
Definition: StringLengthValidatorTest.php:186
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength
‪stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
Definition: StringLengthValidatorTest.php:48
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateReturnsNoErrorIfTheGivenValueIsAnEmptyString
‪validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
Definition: StringLengthValidatorTest.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
Definition: StringLengthValidatorTest.php:116
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateRegardsMultibyteStringsCorrectly
‪validateRegardsMultibyteStringsCorrectly()
Definition: StringLengthValidatorTest.php:222
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:16
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
Definition: StringLengthValidatorTest.php:106
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest
Definition: StringLengthValidatorTest.php:28
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:26