tests
[m6w6/ascertain] / tests / lib / ascertain / ValidatorTest.php
1 <?php
2
3 namespace ascertain;
4
5 include __DIR__."/../../setup.inc";
6
7 class Test implements Testable
8 {
9 use Validator;
10
11 public $good;
12
13 function __construct($good) {
14 $this->good = $good;
15 }
16
17 function export() {
18 return array_map(function($v) {
19 return $v[(int)$this->good];
20 }, array(
21 "any" => [0,1],
22 "boolean" => ["nay", true],
23 "containing" => ["im 1", "im 2"],
24 "containing2" => [[1], [1,2]],
25 "email" => ["@nirvana", "mike@php.net"],
26 "float" => ["foo", 123.123],
27 "integer" => [123.1, 123],
28 "ip" => ["543.234.123.000", "123.234.98.0"],
29 "len" => ["foo","foobar"],
30 "matching" => ["foo","foo"],
31 "nothing" => [0, ""],
32 "numeric" => ["123foo123", "123.123"],
33 "printable" => ["\r\n", "easy test"],
34 "scalar" => [null, 1],
35 "url" => ["this-::is a h#rd one", "http://because/probably?everything=valid#here"],
36 ));
37 }
38 }
39
40 class ValidatorTest extends \PHPUnit_Framework_TestCase {
41
42 /**
43 * @var \ascertain\Test
44 */
45 protected $good;
46
47 /**
48 * @var \ascertain\Test
49 */
50 protected $bad;
51
52 protected function setUp() {
53 $this->good = new Test(true);
54 $this->bad = new Test(false);
55 }
56
57 public function testTestNothing() {
58 $good = $this->good->assert(false);
59 $this->assertInstanceOf("\\ascertain\\Assert", $good);
60 $good->that("nothing")->isNothing("error");
61 $this->assertEquals(0, $good->hasErrors());
62 $good->that("nothing")->isNotNothing("error");
63 $this->assertEquals(1, $good->hasErrors());
64 $this->assertSame(array("nothing"=>array("error")), $good->getErrors());
65 $good->resetErrors();
66 $this->assertSame(array(), $good->getErrors());
67
68 $bad = $this->bad->assert(false);
69 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
70 $bad->that("nothing")->isNotNothing("error");
71 $this->assertEquals(0, $bad->hasErrors());
72 $bad->that("nothing")->isNothing("error");
73 $this->assertEquals(1, $bad->hasErrors());
74 $this->assertSame(array("nothing"=>array("error")), $bad->getErrors());
75 $bad->resetErrors();
76 $this->assertSame(array(), $bad->getErrors());
77 }
78
79 public function testTestNumeric() {
80 $good = $this->good->assert(false);
81 $this->assertInstanceOf("\\ascertain\\Assert", $good);
82 $good->that("numeric")->isNumeric("error");
83 $this->assertEquals(0, $good->hasErrors());
84 $good->that("numeric")->isNotNumeric("error");
85 $this->assertEquals(1, $good->hasErrors());
86
87 $bad = $this->bad->assert(false);
88 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
89 $bad->that("numeric")->isNotNumeric("error");
90 $this->assertEquals(0, $bad->hasErrors());
91 $bad->that("numeric")->isNumeric("error");
92 $this->assertEquals(1, $bad->hasErrors());
93 }
94
95 public function testTestScalar() {
96 $good = $this->good->assert(false);
97 $this->assertInstanceOf("\\ascertain\\Assert", $good);
98 $good->that("scalar")->isScalar("error");
99 $this->assertEquals(0, $good->hasErrors());
100 $good->that("scalar")->isNotScalar("error");
101 $this->assertEquals(1, $good->hasErrors());
102
103 $bad = $this->bad->assert(false);
104 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
105 $bad->that("scalar")->isNotScalar("error");
106 $this->assertEquals(0, $bad->hasErrors());
107 $bad->that("scalar")->isScalar("error");
108 $this->assertEquals(1, $bad->hasErrors());
109 }
110
111 public function testTestPrintable() {
112 $good = $this->good->assert(false);
113 $this->assertInstanceOf("\\ascertain\\Assert", $good);
114 $good->that("printable")->isPrintable("error");
115 $this->assertEquals(0, $good->hasErrors());
116 $good->that("printable")->isNotPrintable("error");
117 $this->assertEquals(1, $good->hasErrors());
118
119 $bad = $this->bad->assert(false);
120 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
121 $bad->that("printable")->isNotPrintable("error");
122 $this->assertEquals(0, $bad->hasErrors());
123 $bad->that("printable")->isPrintable("error");
124 $this->assertEquals(1, $bad->hasErrors());
125 }
126
127 public function testTestLen() {
128 $good = $this->good->assert(false);
129 $this->assertInstanceOf("\\ascertain\\Assert", $good);
130 $good->that("len")->isLen(4, "error");
131 $this->assertEquals(0, $good->hasErrors());
132 $good->that("len")->isNotLen(4, "error");
133 $this->assertEquals(1, $good->hasErrors());
134
135 $bad = $this->bad->assert(false);
136 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
137 $bad->that("len")->isNotLen(4, "error");
138 $this->assertEquals(0, $bad->hasErrors());
139 $bad->that("len")->isLen(4, "error");
140 $this->assertEquals(1, $bad->hasErrors());
141 }
142
143 public function testTestInteger() {
144 $good = $this->good->assert(false);
145 $this->assertInstanceOf("\\ascertain\\Assert", $good);
146 $good->that("integer")->isInteger("error");
147 $this->assertEquals(0, $good->hasErrors());
148 $good->that("integer")->isNotInteger("error");
149 $this->assertEquals(1, $good->hasErrors());
150
151 $bad = $this->bad->assert(false);
152 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
153 $bad->that("integer")->isNotInteger("error");
154 $this->assertEquals(0, $bad->hasErrors());
155 $bad->that("integer")->isInteger("error");
156 $this->assertEquals(1, $bad->hasErrors());
157 }
158
159 public function testTestBoolean() {
160 $good = $this->good->assert(false);
161 $this->assertInstanceOf("\\ascertain\\Assert", $good);
162 $good->that("boolean")->isBoolean("error");
163 $this->assertEquals(0, $good->hasErrors());
164 $good->that("boolean")->isNotBoolean("error");
165 $this->assertEquals(1, $good->hasErrors());
166
167 $bad = $this->bad->assert(false);
168 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
169 $bad->that("boolean")->isNotBoolean("error");
170 $this->assertEquals(0, $bad->hasErrors());
171 $bad->that("boolean")->isBoolean("error");
172 $this->assertEquals(1, $bad->hasErrors());
173 }
174
175 public function testTestFloat() {
176 $good = $this->good->assert(false);
177 $this->assertInstanceOf("\\ascertain\\Assert", $good);
178 $good->that("float")->isFloat("error");
179 $this->assertEquals(0, $good->hasErrors());
180 $good->that("float")->isNotFloat("error");
181 $this->assertEquals(1, $good->hasErrors());
182
183 $bad = $this->bad->assert(false);
184 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
185 $bad->that("float")->isNotFloat("error");
186 $this->assertEquals(0, $bad->hasErrors());
187 $bad->that("float")->isFloat("error");
188 $this->assertEquals(1, $bad->hasErrors());
189 }
190
191 public function testTestUrl() {
192 $good = $this->good->assert(false);
193 $this->assertInstanceOf("\\ascertain\\Assert", $good);
194 $good->that("url")->isUrl("error");
195 $this->assertEquals(0, $good->hasErrors());
196 $good->that("url")->isNotUrl("error");
197 $this->assertEquals(1, $good->hasErrors());
198
199 $bad = $this->bad->assert(false);
200 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
201 $bad->that("url")->isNotUrl("error");
202 $this->assertEquals(0, $bad->hasErrors());
203 $bad->that("url")->isUrl("error");
204 $this->assertEquals(1, $bad->hasErrors());
205 }
206
207 public function testTestEmail() {
208 $good = $this->good->assert(false);
209 $this->assertInstanceOf("\\ascertain\\Assert", $good);
210 $good->that("email")->isEmail("error");
211 $this->assertEquals(0, $good->hasErrors());
212 $good->that("email")->isNotEmail("error");
213 $this->assertEquals(1, $good->hasErrors());
214
215 $bad = $this->bad->assert(false);
216 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
217 $bad->that("email")->isNotEmail("error");
218 $this->assertEquals(0, $bad->hasErrors());
219 $bad->that("email")->isEmail("error");
220 $this->assertEquals(1, $bad->hasErrors());
221 }
222
223 public function testTestIp() {
224 $good = $this->good->assert(false);
225 $this->assertInstanceOf("\\ascertain\\Assert", $good);
226 $good->that("ip")->isIp("error");
227 $this->assertEquals(0, $good->hasErrors());
228 $good->that("ip")->isNotIp("error");
229 $this->assertEquals(1, $good->hasErrors());
230
231 $bad = $this->bad->assert(false);
232 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
233 $bad->that("ip")->isNotIp("error");
234 $this->assertEquals(0, $bad->hasErrors());
235 $bad->that("ip")->isIp("error");
236 $this->assertEquals(1, $bad->hasErrors());
237 }
238
239 public function testTestContaining() {
240 $good = $this->good->assert(false);
241 $this->assertInstanceOf("\\ascertain\\Assert", $good);
242 $good->that("containing")->isContaining(2, "error");
243 $this->assertEquals(0, $good->hasErrors());
244 $good->that("containing")->isNotContaining(2, "error");
245 $this->assertEquals(1, $good->hasErrors());
246
247 $bad = $this->bad->assert(false);
248 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
249 $bad->that("containing")->isNotContaining(2, "error");
250 $this->assertEquals(0, $bad->hasErrors());
251 $bad->that("containing")->isContaining(2, "error");
252 $this->assertEquals(1, $bad->hasErrors());
253 }
254
255 public function testTestContaining2() {
256 $good = $this->good->assert(false);
257 $this->assertInstanceOf("\\ascertain\\Assert", $good);
258 $good->that("containing2")->isContaining(1, "error");
259 $this->assertEquals(0, $good->hasErrors());
260 $good->that("containing2")->isNotContaining(1, "error");
261 $this->assertEquals(1, $good->hasErrors());
262
263 $bad = $this->bad->assert(false);
264 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
265 $bad->that("containing2")->isNotContaining(1, "error");
266 $this->assertEquals(0, $bad->hasErrors());
267 $bad->that("containing2")->isContaining(1, "error");
268 $this->assertEquals(1, $bad->hasErrors());
269 }
270
271 public function testTestMatching() {
272 $good = $this->good->assert(false);
273 $this->assertInstanceOf("\\ascertain\\Assert", $good);
274 $good->that("matching")->isMatching("/^\w+\$/", "error");
275 $this->assertEquals(0, $good->hasErrors());
276 $good->that("matching")->isNotMatching("/^\w+\$/", "error");
277 $this->assertEquals(1, $good->hasErrors());
278
279 $bad = $this->bad->assert(false);
280 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
281 $bad->that("matching")->isNotMatching("/^\$/", "error");
282 $this->assertEquals(0, $bad->hasErrors());
283 $bad->that("matching")->isMatching("/^\$/", "error");
284 $this->assertEquals(1, $bad->hasErrors());
285 }
286
287 public function testTestAny() {
288 $good = $this->good->assert(false);
289 $this->assertInstanceOf("\\ascertain\\Assert", $good);
290 $good->that("any")->isAny(array(1,2), "error");
291 $this->assertEquals(0, $good->hasErrors());
292 $good->that("any")->isNotAny(array(1,2), "error");
293 $this->assertEquals(1, $good->hasErrors());
294
295 $bad = $this->bad->assert(false);
296 $this->assertInstanceOf("\\ascertain\\Assert", $bad);
297 $bad->that("any")->isNotAny(array(1,2), "error");
298 $this->assertEquals(0, $bad->hasErrors());
299 $bad->that("any")->isAny(array(1,2), "error");
300 $this->assertEquals(1, $bad->hasErrors());
301 }
302 }