‪TYPO3CMS  9.5
StringLengthValidatorTest.php
Go to the documentation of this file.
1 <?php
2 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 
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
25 class ‪StringLengthValidatorTest extends UnitTestCase
26 {
31  {
32  $this->assertFalse((new ‪StringLengthValidator())->validate(null)->hasErrors());
33  }
34 
39  {
40  $this->assertFalse((new ‪StringLengthValidator())->validate('')->hasErrors());
41  }
42 
47  {
48  ‪$validator = new ‪StringLengthValidator(['minimum' => 0, 'maximum' => 50]);
49 
50  $this->assertFalse(‪$validator->validate('this is a very simple string')->hasErrors());
51  }
52 
57  {
59  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
60  ->setMethods(['translateErrorMessage'])
61  ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
62  ->getMock();
63 
64  $this->assertTrue(‪$validator->validate('this is a very short string')->hasErrors());
65  }
66 
71  {
73  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
74  ->setMethods(['translateErrorMessage'])
75  ->setConstructorArgs([['minimum' => 5, 'maximum' => 10]])
76  ->getMock();
77 
78  $this->assertTrue(‪$validator->validate('this is a very short string')->hasErrors());
79  }
80 
85  {
86  ‪$validator = new ‪StringLengthValidator(['minimum' => 5]);
87 
88  $this->assertFalse(‪$validator->validate('this is a very short string')->hasErrors());
89  }
90 
95  {
96  ‪$validator = new ‪StringLengthValidator(['maximum' => 100]);
97 
98  $this->assertFalse(‪$validator->validate('this is a very short string')->hasErrors());
99  }
100 
105  {
106  ‪$validator = new ‪StringLengthValidator(['maximum' => 10]);
107 
108  $this->assertFalse(‪$validator->validate('1234567890')->hasErrors());
109  }
110 
115  {
116  ‪$validator = new ‪StringLengthValidator(['minimum' => 10]);
117 
118  $this->assertFalse(‪$validator->validate('1234567890')->hasErrors());
119  }
120 
125  {
126  ‪$validator = new ‪StringLengthValidator(['minimum' => 10, 'maximum' => 10]);
127 
128  $this->assertFalse(‪$validator->validate('1234567890')->hasErrors());
129  }
130 
135  {
136  ‪$validator = new ‪StringLengthValidator(['minimum' => 1, 'maximum' => 10]);
137 
138  $this->assertFalse(‪$validator->validate('1234567890')->hasErrors());
139  }
140 
145  {
146  ‪$validator = new ‪StringLengthValidator(['minimum' => 10, 'maximum' => 100]);
147 
148  $this->assertFalse(‪$validator->validate('1234567890')->hasErrors());
149  }
150 
155  {
156  $this->expectException(InvalidValidationOptionsException::class);
157  $this->expectExceptionCode(1238107096);
158 
160  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
161  ->setMethods(['addError', 'translateErrorMessage'])
162  ->setConstructorArgs([['minimum' => 101, 'maximum' => 100]])
163  ->getMock();
164  ‪$validator->validate('1234567890');
165  }
166 
171  {
173  ‪$validator = $this->getMockBuilder(StringLengthValidator::class)
174  ->setMethods(['translateErrorMessage'])
175  ->setConstructorArgs([['minimum' => 50, 'maximum' => 100]])
176  ->getMock();
177 
178  $this->assertCount(1, ‪$validator->validate('this is a very short string')->getErrors());
179  }
180 
185  {
186  ‪$validator = new ‪StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
187  $object = new class() {
189  public function __toString()
190  {
191  return 'some string';
192  }
193  };
194 
195  $this->assertFalse(‪$validator->validate($object)->hasErrors());
196  }
197 
202  {
203  ‪$validator = new ‪StringLengthValidator(['minimum' => 5, 'maximum' => 100]);
204  $object = new class() {
205  };
206 
207  $result = ‪$validator->validate($object);
208 
209  $this->assertTrue($result->hasErrors());
210  $this->assertCount(1, $result->getErrors());
211 
213  $error = current($result->getErrors());
214  $this->assertSame(1238110957, $error->getCode());
215  }
216 
221  {
222  ‪$validator = new ‪StringLengthValidator(['minimum' => 0, 'maximum' => 8]);
223  $result = ‪$validator->validate('überlang');
224 
225  $this->assertFalse($result->hasErrors());
226  }
227 }
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength
‪stringLengthValidatorReturnsNoErrorsfTheStringLengthIsEqualToMaxLength()
Definition: StringLengthValidatorTest.php:134
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength
‪stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength()
Definition: StringLengthValidatorTest.php:154
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue
‪stringLengthValidatorReturnsNoErrorIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue()
Definition: StringLengthValidatorTest.php:124
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength
‪stringLengthValidatorReturnsErrorForAStringShorterThanThanMinLength()
Definition: StringLengthValidatorTest.php:56
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorInsertsAnErrorObjectIfValidationFails
‪stringLengthValidatorInsertsAnErrorObjectIfValidationFails()
Definition: StringLengthValidatorTest.php:170
‪TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator
Definition: StringLengthValidator.php:21
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorsForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified()
Definition: StringLengthValidatorTest.php:84
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorsForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified()
Definition: StringLengthValidatorTest.php:94
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateReturnsNoErrorIfTheGivenValueIsNull
‪validateReturnsNoErrorIfTheGivenValueIsNull()
Definition: StringLengthValidatorTest.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength
‪stringLengthValidatorReturnsNoErrorIfTheStringLengthIsEqualToMinLength()
Definition: StringLengthValidatorTest.php:144
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength
‪stringLengthValidatorReturnsErrorsForAStringLongerThanThanMaxLength()
Definition: StringLengthValidatorTest.php:70
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString
‪validateReturnsErrorsIfTheGivenObjectCanNotBeConvertedToAString()
Definition: StringLengthValidatorTest.php:201
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorCanHandleAnObjectWithAToStringMethod
‪stringLengthValidatorCanHandleAnObjectWithAToStringMethod()
Definition: StringLengthValidatorTest.php:184
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength
‪stringLengthValidatorReturnsNoErrorForAStringShorterThanMaxLengthAndLongerThanMinLength()
Definition: StringLengthValidatorTest.php:46
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateReturnsNoErrorIfTheGivenValueIsAnEmptyString
‪validateReturnsNoErrorIfTheGivenValueIsAnEmptyString()
Definition: StringLengthValidatorTest.php:38
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified()
Definition: StringLengthValidatorTest.php:114
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\validateRegardsMultibyteStringsCorrectly
‪validateRegardsMultibyteStringsCorrectly()
Definition: StringLengthValidatorTest.php:220
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator
Definition: AbstractCompositeValidatorTest.php:2
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest\stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified
‪stringLengthValidatorReturnsNoErrorsForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified()
Definition: StringLengthValidatorTest.php:104
‪TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\StringLengthValidatorTest
Definition: StringLengthValidatorTest.php:26
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:21