[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Validation / Validator / BeforeExtbase14 / StringLengthValidatorTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Validation\Validator\BeforeExtbase14;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 *
20 * This testcase checks the expected behavior for Extbase < 1.4.0, to make sure
21 * we do not break backwards compatibility.
22 */
23 class StringLengthValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * @test
27 */
28 public function stgringLengthValidatorReturnsTrueForAStringShorterThanMaxLengthAndLongerThanMinLength() {
29 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
30 $stringLengthValidator->expects($this->never())->method('addError');
31 $stringLengthValidator->setOptions(array('minimum' => 0, 'maximum' => 50));
32 $stringLengthValidator->isValid('this is a very simple string');
33 }
34
35 /**
36 * @test
37 */
38 public function stringLengthValidatorReturnsFalseForAStringShorterThanThanMinLength() {
39 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
40 $stringLengthValidator->expects($this->once())->method('addError');
41 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
42 $stringLengthValidator->isValid('this is a very short string');
43 }
44
45 /**
46 * @test
47 */
48 public function stringLengthValidatorReturnsFalseForAStringLongerThanThanMaxLength() {
49 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
50 $stringLengthValidator->expects($this->once())->method('addError');
51 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 10));
52 $stringLengthValidator->isValid('this is a very short string');
53 }
54
55 /**
56 * @test
57 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
58 */
59 public function stringLengthValidatorReturnsTrueForAStringLongerThanThanMinLengthAndMaxLengthNotSpecified() {
60 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
61 $stringLengthValidator->setOptions(array('minimum' => 5));
62 $stringLengthValidator->isValid('this is a very short string');
63 }
64
65 /**
66 * @test
67 */
68 public function stringLengthValidatorReturnsTrueForAStringShorterThanThanMaxLengthAndMinLengthNotSpecified() {
69 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
70 $stringLengthValidator->expects($this->never())->method('addError');
71 $stringLengthValidator->setOptions(array('maximum' => 100));
72 }
73
74 /**
75 * @test
76 */
77 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMaxLengthAndMinLengthNotSpecified() {
78 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
79 $stringLengthValidator->expects($this->never())->method('addError');
80 $stringLengthValidator->setOptions(array('maximum' => 10));
81 $stringLengthValidator->isValid('1234567890');
82 }
83
84 /**
85 * @test
86 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
87 */
88 public function stringLengthValidatorReturnsTrueForAStringLengthEqualToMinLengthAndMaxLengthNotSpecified() {
89 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
90 $stringLengthValidator->setOptions(array('minimum' => 10));
91 $stringLengthValidator->isValid('1234567890');
92 }
93
94 /**
95 * @test
96 */
97 public function stringLengthValidatorReturnsTrueIfMinLengthAndMaxLengthAreEqualAndTheGivenStringMatchesThisValue() {
98 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
99 $stringLengthValidator->expects($this->never())->method('addError');
100 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 10));
101 $stringLengthValidator->isValid('1234567890');
102 }
103
104 /**
105 * @test
106 */
107 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMaxLength() {
108 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
109 $stringLengthValidator->expects($this->never())->method('addError');
110 $stringLengthValidator->setOptions(array('minimum' => 1, 'maximum' => 10));
111 $stringLengthValidator->isValid('1234567890');
112 }
113
114 /**
115 * @test
116 */
117 public function stringLengthValidatorReturnsTrueIfTheStringLengthIsEqualToMinLength() {
118 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
119 $stringLengthValidator->expects($this->never())->method('addError');
120 $stringLengthValidator->setOptions(array('minimum' => 10, 'maximum' => 100));
121 $stringLengthValidator->isValid('1234567890');
122 }
123
124 /**
125 * @test
126 * @expectedException \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
127 */
128 public function stringLengthValidatorThrowsAnExceptionIfMinLengthIsGreaterThanMaxLength() {
129 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
130 $stringLengthValidator->setOptions(array('minimum' => 101, 'maximum' => 100));
131 $stringLengthValidator->isValid('1234567890');
132 }
133
134 /**
135 * @test
136 */
137 public function stringLengthValidatorInsertsAnErrorObjectIfValidationFails() {
138 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
139 $stringLengthValidator->expects($this->once())->method('addError');
140 $stringLengthValidator->setOptions(array('minimum' => 50, 'maximum' => 100));
141 $stringLengthValidator->isValid('this is a very short string');
142 }
143
144 /**
145 * @test
146 */
147 public function stringLengthValidatorCanHandleAnObjectWithAToStringMethod() {
148 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError'), array(), '', FALSE);
149 $stringLengthValidator->expects($this->never())->method('addError');
150 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
151 $className = uniqid('TestClass');
152 eval('
153 class ' . $className . ' {
154 public function __toString() {
155 return \'some string\';
156 }
157 }
158 ');
159 $object = new $className();
160 $stringLengthValidator->isValid($object);
161 }
162
163 /**
164 * @test
165 */
166 public function stringLengthValidatorAddsAnErrorIfTheGivenObjectCanNotBeConvertedToAString() {
167 $stringLengthValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\StringLengthValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
168 $stringLengthValidator->expects($this->once())->method('addError');
169 $stringLengthValidator->setOptions(array('minimum' => 5, 'maximum' => 100));
170 $className = uniqid('TestClass');
171 eval('
172 class ' . $className . ' {
173 protected $someProperty;
174 }
175 ');
176 $object = new $className();
177 $stringLengthValidator->isValid($object);
178 }
179 }