[FEATURE] Add several testcases for validators
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Validation / BetweenValidatorTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Andreas Lappe <a.lappe@kuehlhaus.com>, kuehlhaus AG
8 *
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Test case for class \TYPO3\CMS\Form\Validation\BetweenValidator.
30 *
31 * @author Andreas Lappe <a.lappe@kuehlhaus.com>
32 * @package TYPO3
33 * @subpackage form
34 */
35 class BetweenValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
36 /**
37 * @var \TYPO3\CMS\Form\Tests\Unit\Validation\Helper
38 */
39 protected $helper;
40
41 /**
42 * @var \TYPO3\CMS\Form\Validation\BetweenValidator
43 */
44 protected $fixture;
45
46 public function setUp() {
47 $this->helper = new \TYPO3\CMS\Form\Tests\Unit\Validation\Helper();
48 $this->fixture = new \TYPO3\CMS\Form\Validation\BetweenValidator(array());
49 }
50
51 public function tearDown() {
52 unset($this->helper, $this->fixture);
53 }
54
55 public function validNonInclusiveDataProvider() {
56 return array(
57 '3 < 5 < 7' => array(array(3, 5, 7)),
58 '0 < 10 < 20' => array(array(0, 10, 20)),
59 '-10 < 0 < 10' => array(array(-10, 0, 10)),
60 '-20 < -10 < 0' => array(array(-20, -10, 0)),
61 '1 < 2 < 3' => array(array(1, 2, 3)),
62 '1 < 1.01 < 1.1' => array(array(1, 1.01, 1.1)),
63 );
64 }
65
66 public function invalidNonInclusiveDataProvider() {
67 return array(
68 '1 < 1 < 2' => array(array(1, 1, 2)),
69 '1 < 2 < 2' => array(array(1, 2, 2)),
70 '1.1 < 1.1 < 1.2' => array(array(1.1, 1.1, 1.2)),
71 '1.1 < 1.2 < 1.2' => array(array(1.1, 1.2, 1.2)),
72 '-10.1234 < -10.12340 < 10' => array(array(-10.1234, -10.12340, 10)),
73 '100 < 0 < -100' => array(array(100, 0, -100))
74 );
75 }
76
77 public function validInclusiveDataProvider() {
78 return array(
79 '1 ≤ 1 ≤ 1' => array(array(1,1,1)),
80 '-10.1234 ≤ -10.12340 ≤ 10' => array(array(-10.1234, -10.12340, 10)),
81 '-10.1234 ≤ -10 ≤ 10' => array(array(-10.1234, -10.12340, 10)),
82 );
83 }
84
85 public function invalidInclusiveDataProvider() {
86 return array(
87 '-10.1234 ≤ -10.12345 ≤ 10' => array(array(-10.1234, -10.12345, 10)),
88 '100 ≤ 0 ≤ -100' => array(array(100, 0, -100))
89 );
90 }
91
92 /**
93 * @test
94 * @dataProvider validNonInclusiveDataProvider
95 */
96 public function isValidWithValidInputAndWithoutInclusiveReturnsTrue($input) {
97 $this->fixture->setMinimum($input[0]);
98 $this->fixture->setMaximum($input[2]);
99 $this->fixture->setFieldName('numericValue');
100 $requestHandlerMock = $this->helper->getRequestHandler(array(
101 'numericValue' => $input[1]
102 ));
103 $this->fixture->injectRequestHandler($requestHandlerMock);
104
105 $this->assertTrue(
106 $this->fixture->isValid()
107 );
108 }
109
110 /**
111 * @test
112 * @dataProvider validInclusiveDataProvider
113 */
114 public function isValidWithValidInputAndWithInclusiveReturnsTrue($input) {
115 $this->fixture->setMinimum($input[0]);
116 $this->fixture->setMaximum($input[2]);
117 $this->fixture->setFieldName('numericValue');
118 $this->fixture->setInclusive(TRUE);
119 $requestHandlerMock = $this->helper->getRequestHandler(array(
120 'numericValue' => $input[1]
121 ));
122 $this->fixture->injectRequestHandler($requestHandlerMock);
123
124 $this->assertTrue(
125 $this->fixture->isValid()
126 );
127 }
128
129 /**
130 * @test
131 * @dataProvider invalidNonInclusiveDataProvider
132 */
133 public function isValidWithInvalidInputAndWithoutInclusiveReturnsFalse($input) {
134 $this->fixture->setMinimum($input[0]);
135 $this->fixture->setMaximum($input[2]);
136 $this->fixture->setFieldName('numericValue');
137 $requestHandlerMock = $this->helper->getRequestHandler(array(
138 'numericValue' => $input[1]
139 ));
140 $this->fixture->injectRequestHandler($requestHandlerMock);
141
142 $this->assertFalse(
143 $this->fixture->isValid()
144 );
145 }
146
147 /**
148 * @test
149 * @dataProvider invalidInclusiveDataProvider
150 */
151 public function isValidWithInvalidInputAndWithInclusiveReturnsFalse($input) {
152 $this->fixture->setMinimum($input[0]);
153 $this->fixture->setMaximum($input[2]);
154 $this->fixture->setFieldName('numericValue');
155 $this->fixture->setInclusive(TRUE);
156 $requestHandlerMock = $this->helper->getRequestHandler(array(
157 'numericValue' => $input[1]
158 ));
159 $this->fixture->injectRequestHandler($requestHandlerMock);
160
161 $this->assertFalse(
162 $this->fixture->isValid()
163 );
164 }
165 }
166 ?>