Mercurial > hg > isophonics-drupal-site
comparison vendor/webmozart/assert/tests/AssertTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the webmozart/assert package. | |
5 * | |
6 * (c) Bernhard Schussek <bschussek@gmail.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Webmozart\Assert\Tests; | |
13 | |
14 use ArrayIterator; | |
15 use Exception; | |
16 use Error; | |
17 use LogicException; | |
18 use PHPUnit_Framework_TestCase; | |
19 use RuntimeException; | |
20 use stdClass; | |
21 use Webmozart\Assert\Assert; | |
22 | |
23 /** | |
24 * @since 1.0 | |
25 * | |
26 * @author Bernhard Schussek <bschussek@gmail.com> | |
27 */ | |
28 class AssertTest extends PHPUnit_Framework_TestCase | |
29 { | |
30 private static $resource; | |
31 | |
32 public static function getResource() | |
33 { | |
34 if (!static::$resource) { | |
35 static::$resource = fopen(__FILE__, 'r'); | |
36 } | |
37 | |
38 return static::$resource; | |
39 } | |
40 | |
41 public static function tearDownAfterClass() | |
42 { | |
43 @fclose(self::$resource); | |
44 } | |
45 | |
46 public function getTests() | |
47 { | |
48 $resource = self::getResource(); | |
49 | |
50 return array( | |
51 array('string', array('value'), true), | |
52 array('string', array(''), true), | |
53 array('string', array(1234), false), | |
54 array('stringNotEmpty', array('value'), true), | |
55 array('stringNotEmpty', array(''), false), | |
56 array('stringNotEmpty', array(1234), false), | |
57 array('integer', array(123), true), | |
58 array('integer', array('123'), false), | |
59 array('integer', array(1.0), false), | |
60 array('integer', array(1.23), false), | |
61 array('integerish', array(1.0), true), | |
62 array('integerish', array(1.23), false), | |
63 array('integerish', array(123), true), | |
64 array('integerish', array('123'), true), | |
65 array('float', array(1.0), true), | |
66 array('float', array(1.23), true), | |
67 array('float', array(123), false), | |
68 array('float', array('123'), false), | |
69 array('numeric', array(1.0), true), | |
70 array('numeric', array(1.23), true), | |
71 array('numeric', array(123), true), | |
72 array('numeric', array('123'), true), | |
73 array('numeric', array('foo'), false), | |
74 array('boolean', array(true), true), | |
75 array('boolean', array(false), true), | |
76 array('boolean', array(1), false), | |
77 array('boolean', array('1'), false), | |
78 array('scalar', array('1'), true), | |
79 array('scalar', array(123), true), | |
80 array('scalar', array(true), true), | |
81 array('scalar', array(null), false), | |
82 array('scalar', array(array()), false), | |
83 array('scalar', array(new stdClass()), false), | |
84 array('object', array(new stdClass()), true), | |
85 array('object', array(new RuntimeException()), true), | |
86 array('object', array(null), false), | |
87 array('object', array(true), false), | |
88 array('object', array(1), false), | |
89 array('object', array(array()), false), | |
90 array('resource', array($resource), true), | |
91 array('resource', array($resource, 'stream'), true), | |
92 array('resource', array($resource, 'other'), false), | |
93 array('resource', array(1), false), | |
94 array('isCallable', array('strlen'), true), | |
95 array('isCallable', array(array($this, 'getTests')), true), | |
96 array('isCallable', array(function () {}), true), | |
97 array('isCallable', array(1234), false), | |
98 array('isCallable', array('foobar'), false), | |
99 array('isArray', array(array()), true), | |
100 array('isArray', array(array(1, 2, 3)), true), | |
101 array('isArray', array(new ArrayIterator(array())), false), | |
102 array('isArray', array(123), false), | |
103 array('isArray', array(new stdClass()), false), | |
104 array('isTraversable', array(array()), true), | |
105 array('isTraversable', array(array(1, 2, 3)), true), | |
106 array('isTraversable', array(new ArrayIterator(array())), true), | |
107 array('isTraversable', array(123), false), | |
108 array('isTraversable', array(new stdClass()), false), | |
109 array('isInstanceOf', array(new stdClass(), 'stdClass'), true), | |
110 array('isInstanceOf', array(new Exception(), 'stdClass'), false), | |
111 array('isInstanceOf', array(123, 'stdClass'), false), | |
112 array('isInstanceOf', array(array(), 'stdClass'), false), | |
113 array('notInstanceOf', array(new stdClass(), 'stdClass'), false), | |
114 array('notInstanceOf', array(new Exception(), 'stdClass'), true), | |
115 array('notInstanceOf', array(123, 'stdClass'), true), | |
116 array('notInstanceOf', array(array(), 'stdClass'), true), | |
117 array('true', array(true), true), | |
118 array('true', array(false), false), | |
119 array('true', array(1), false), | |
120 array('true', array(null), false), | |
121 array('false', array(false), true), | |
122 array('false', array(true), false), | |
123 array('false', array(1), false), | |
124 array('false', array(0), false), | |
125 array('false', array(null), false), | |
126 array('null', array(null), true), | |
127 array('null', array(false), false), | |
128 array('null', array(0), false), | |
129 array('notNull', array(false), true), | |
130 array('notNull', array(0), true), | |
131 array('notNull', array(null), false), | |
132 array('isEmpty', array(null), true), | |
133 array('isEmpty', array(false), true), | |
134 array('isEmpty', array(0), true), | |
135 array('isEmpty', array(''), true), | |
136 array('isEmpty', array(1), false), | |
137 array('isEmpty', array('a'), false), | |
138 array('notEmpty', array(1), true), | |
139 array('notEmpty', array('a'), true), | |
140 array('notEmpty', array(null), false), | |
141 array('notEmpty', array(false), false), | |
142 array('notEmpty', array(0), false), | |
143 array('notEmpty', array(''), false), | |
144 array('eq', array(1, 1), true), | |
145 array('eq', array(1, '1'), true), | |
146 array('eq', array(1, true), true), | |
147 array('eq', array(1, 0), false), | |
148 array('notEq', array(1, 0), true), | |
149 array('notEq', array(1, 1), false), | |
150 array('notEq', array(1, '1'), false), | |
151 array('notEq', array(1, true), false), | |
152 array('same', array(1, 1), true), | |
153 array('same', array(1, '1'), false), | |
154 array('same', array(1, true), false), | |
155 array('same', array(1, 0), false), | |
156 array('notSame', array(1, 0), true), | |
157 array('notSame', array(1, 1), false), | |
158 array('notSame', array(1, '1'), true), | |
159 array('notSame', array(1, true), true), | |
160 array('greaterThan', array(1, 0), true), | |
161 array('greaterThan', array(0, 0), false), | |
162 array('greaterThanEq', array(2, 1), true), | |
163 array('greaterThanEq', array(1, 1), true), | |
164 array('greaterThanEq', array(0, 1), false), | |
165 array('lessThan', array(0, 1), true), | |
166 array('lessThan', array(1, 1), false), | |
167 array('lessThanEq', array(0, 1), true), | |
168 array('lessThanEq', array(1, 1), true), | |
169 array('lessThanEq', array(2, 1), false), | |
170 array('range', array(1, 1, 2), true), | |
171 array('range', array(2, 1, 2), true), | |
172 array('range', array(0, 1, 2), false), | |
173 array('range', array(3, 1, 2), false), | |
174 array('oneOf', array(1, array(1, 2, 3)), true), | |
175 array('oneOf', array(1, array('1', '2', '3')), false), | |
176 array('contains', array('abcd', 'ab'), true), | |
177 array('contains', array('abcd', 'bc'), true), | |
178 array('contains', array('abcd', 'cd'), true), | |
179 array('contains', array('abcd', 'de'), false), | |
180 array('contains', array('', 'de'), false), | |
181 array('startsWith', array('abcd', 'ab'), true), | |
182 array('startsWith', array('abcd', 'bc'), false), | |
183 array('startsWith', array('', 'bc'), false), | |
184 array('startsWithLetter', array('abcd'), true), | |
185 array('startsWithLetter', array('1abcd'), false), | |
186 array('startsWithLetter', array(''), false), | |
187 array('endsWith', array('abcd', 'cd'), true), | |
188 array('endsWith', array('abcd', 'bc'), false), | |
189 array('endsWith', array('', 'bc'), false), | |
190 array('regex', array('abcd', '~^ab~'), true), | |
191 array('regex', array('abcd', '~^bc~'), false), | |
192 array('regex', array('', '~^bc~'), false), | |
193 array('alpha', array('abcd'), true), | |
194 array('alpha', array('ab1cd'), false), | |
195 array('alpha', array(''), false), | |
196 array('digits', array('1234'), true), | |
197 array('digits', array('12a34'), false), | |
198 array('digits', array(''), false), | |
199 array('alnum', array('ab12'), true), | |
200 array('alnum', array('ab12$'), false), | |
201 array('alnum', array(''), false), | |
202 array('lower', array('abcd'), true), | |
203 array('lower', array('abCd'), false), | |
204 array('lower', array('ab_d'), false), | |
205 array('lower', array(''), false), | |
206 array('upper', array('ABCD'), true), | |
207 array('upper', array('ABcD'), false), | |
208 array('upper', array('AB_D'), false), | |
209 array('upper', array(''), false), | |
210 array('length', array('abcd', 4), true), | |
211 array('length', array('abc', 4), false), | |
212 array('length', array('abcde', 4), false), | |
213 array('length', array('äbcd', 4), true, true), | |
214 array('length', array('äbc', 4), false, true), | |
215 array('length', array('äbcde', 4), false, true), | |
216 array('minLength', array('abcd', 4), true), | |
217 array('minLength', array('abcde', 4), true), | |
218 array('minLength', array('abc', 4), false), | |
219 array('minLength', array('äbcd', 4), true, true), | |
220 array('minLength', array('äbcde', 4), true, true), | |
221 array('minLength', array('äbc', 4), false, true), | |
222 array('maxLength', array('abcd', 4), true), | |
223 array('maxLength', array('abc', 4), true), | |
224 array('maxLength', array('abcde', 4), false), | |
225 array('maxLength', array('äbcd', 4), true, true), | |
226 array('maxLength', array('äbc', 4), true, true), | |
227 array('maxLength', array('äbcde', 4), false, true), | |
228 array('lengthBetween', array('abcd', 3, 5), true), | |
229 array('lengthBetween', array('abc', 3, 5), true), | |
230 array('lengthBetween', array('abcde', 3, 5), true), | |
231 array('lengthBetween', array('ab', 3, 5), false), | |
232 array('lengthBetween', array('abcdef', 3, 5), false), | |
233 array('lengthBetween', array('äbcd', 3, 5), true, true), | |
234 array('lengthBetween', array('äbc', 3, 5), true, true), | |
235 array('lengthBetween', array('äbcde', 3, 5), true, true), | |
236 array('lengthBetween', array('äb', 3, 5), false, true), | |
237 array('lengthBetween', array('äbcdef', 3, 5), false, true), | |
238 array('fileExists', array(__FILE__), true), | |
239 array('fileExists', array(__DIR__), true), | |
240 array('fileExists', array(__DIR__.'/foobar'), false), | |
241 array('file', array(__FILE__), true), | |
242 array('file', array(__DIR__), false), | |
243 array('file', array(__DIR__.'/foobar'), false), | |
244 array('directory', array(__DIR__), true), | |
245 array('directory', array(__FILE__), false), | |
246 array('directory', array(__DIR__.'/foobar'), false), | |
247 // no tests for readable()/writable() for now | |
248 array('classExists', array(__CLASS__), true), | |
249 array('classExists', array(__NAMESPACE__.'\Foobar'), false), | |
250 array('subclassOf', array(__CLASS__, 'PHPUnit_Framework_TestCase'), true), | |
251 array('subclassOf', array(__CLASS__, 'stdClass'), false), | |
252 array('implementsInterface', array('ArrayIterator', 'Traversable'), true), | |
253 array('implementsInterface', array(__CLASS__, 'Traversable'), false), | |
254 array('propertyExists', array((object) array('property' => 0), 'property'), true), | |
255 array('propertyExists', array((object) array('property' => null), 'property'), true), | |
256 array('propertyExists', array((object) array('property' => null), 'foo'), false), | |
257 array('propertyNotExists', array((object) array('property' => 0), 'property'), false), | |
258 array('propertyNotExists', array((object) array('property' => null), 'property'), false), | |
259 array('propertyNotExists', array((object) array('property' => null), 'foo'), true), | |
260 array('methodExists', array('RuntimeException', 'getMessage'), true), | |
261 array('methodExists', array(new RuntimeException(), 'getMessage'), true), | |
262 array('methodExists', array('stdClass', 'getMessage'), false), | |
263 array('methodExists', array(new stdClass(), 'getMessage'), false), | |
264 array('methodExists', array(null, 'getMessage'), false), | |
265 array('methodExists', array(true, 'getMessage'), false), | |
266 array('methodExists', array(1, 'getMessage'), false), | |
267 array('methodNotExists', array('RuntimeException', 'getMessage'), false), | |
268 array('methodNotExists', array(new RuntimeException(), 'getMessage'), false), | |
269 array('methodNotExists', array('stdClass', 'getMessage'), true), | |
270 array('methodNotExists', array(new stdClass(), 'getMessage'), true), | |
271 array('methodNotExists', array(null, 'getMessage'), true), | |
272 array('methodNotExists', array(true, 'getMessage'), true), | |
273 array('methodNotExists', array(1, 'getMessage'), true), | |
274 array('keyExists', array(array('key' => 0), 'key'), true), | |
275 array('keyExists', array(array('key' => null), 'key'), true), | |
276 array('keyExists', array(array('key' => null), 'foo'), false), | |
277 array('keyNotExists', array(array('key' => 0), 'key'), false), | |
278 array('keyNotExists', array(array('key' => null), 'key'), false), | |
279 array('keyNotExists', array(array('key' => null), 'foo'), true), | |
280 array('count', array(array(0, 1, 2), 3), true), | |
281 array('count', array(array(0, 1, 2), 2), false), | |
282 array('uuid', array('00000000-0000-0000-0000-000000000000'), true), | |
283 array('uuid', array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'), true), | |
284 array('uuid', array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), true), | |
285 array('uuid', array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'), true), | |
286 array('uuid', array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'), true), | |
287 array('uuid', array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'), true), | |
288 array('uuid', array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'), true), | |
289 array('uuid', array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'), false), | |
290 array('uuid', array('af6f8cb0c57d11e19b210800200c9a66'), false), | |
291 array('uuid', array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'), false), | |
292 array('uuid', array('af6f8cb-c57d-11e1-9b21-0800200c9a66'), false), | |
293 array('uuid', array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'), false), | |
294 array('throws', array(function() { throw new LogicException('test'); }, 'LogicException'), true), | |
295 array('throws', array(function() { throw new LogicException('test'); }, 'IllogicException'), false), | |
296 array('throws', array(function() { throw new Exception('test'); }), true), | |
297 array('throws', array(function() { trigger_error('test'); }, 'Throwable'), true, false, 70000), | |
298 array('throws', array(function() { trigger_error('test'); }, 'Unthrowable'), false, false, 70000), | |
299 array('throws', array(function() { throw new Error(); }, 'Throwable'), true, true, 70000), | |
300 ); | |
301 } | |
302 | |
303 public function getMethods() | |
304 { | |
305 $methods = array(); | |
306 | |
307 foreach ($this->getTests() as $params) { | |
308 $methods[$params[0]] = array($params[0]); | |
309 } | |
310 | |
311 return array_values($methods); | |
312 } | |
313 | |
314 /** | |
315 * @dataProvider getTests | |
316 */ | |
317 public function testAssert($method, $args, $success, $multibyte = false, $minVersion = null) | |
318 { | |
319 if ($minVersion && PHP_VERSION_ID < $minVersion) { | |
320 $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); | |
321 | |
322 return; | |
323 } | |
324 if ($multibyte && !function_exists('mb_strlen')) { | |
325 $this->markTestSkipped('The function mb_strlen() is not available'); | |
326 | |
327 return; | |
328 } | |
329 | |
330 if (!$success) { | |
331 $this->setExpectedException('\InvalidArgumentException'); | |
332 } | |
333 | |
334 call_user_func_array(array('Webmozart\Assert\Assert', $method), $args); | |
335 } | |
336 | |
337 /** | |
338 * @dataProvider getTests | |
339 */ | |
340 public function testNullOr($method, $args, $success, $multibyte = false, $minVersion = null) | |
341 { | |
342 if ($minVersion && PHP_VERSION_ID < $minVersion) { | |
343 $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); | |
344 | |
345 return; | |
346 } | |
347 if ($multibyte && !function_exists('mb_strlen')) { | |
348 $this->markTestSkipped('The function mb_strlen() is not available'); | |
349 | |
350 return; | |
351 } | |
352 | |
353 if (!$success && null !== reset($args)) { | |
354 $this->setExpectedException('\InvalidArgumentException'); | |
355 } | |
356 | |
357 call_user_func_array(array('Webmozart\Assert\Assert', 'nullOr'.ucfirst($method)), $args); | |
358 } | |
359 | |
360 /** | |
361 * @dataProvider getMethods | |
362 */ | |
363 public function testNullOrAcceptsNull($method) | |
364 { | |
365 call_user_func(array('Webmozart\Assert\Assert', 'nullOr'.ucfirst($method)), null); | |
366 } | |
367 | |
368 /** | |
369 * @dataProvider getTests | |
370 */ | |
371 public function testAllArray($method, $args, $success, $multibyte = false, $minVersion = null) | |
372 { | |
373 if ($minVersion && PHP_VERSION_ID < $minVersion) { | |
374 $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); | |
375 | |
376 return; | |
377 } | |
378 if ($multibyte && !function_exists('mb_strlen')) { | |
379 $this->markTestSkipped('The function mb_strlen() is not available'); | |
380 | |
381 return; | |
382 } | |
383 | |
384 if (!$success) { | |
385 $this->setExpectedException('\InvalidArgumentException'); | |
386 } | |
387 | |
388 $arg = array_shift($args); | |
389 array_unshift($args, array($arg)); | |
390 | |
391 call_user_func_array(array('Webmozart\Assert\Assert', 'all'.ucfirst($method)), $args); | |
392 } | |
393 | |
394 /** | |
395 * @dataProvider getTests | |
396 */ | |
397 public function testAllTraversable($method, $args, $success, $multibyte = false, $minVersion = null) | |
398 { | |
399 if ($minVersion && PHP_VERSION_ID < $minVersion) { | |
400 $this->markTestSkipped(sprintf('This test requires php %s or upper.', $minVersion)); | |
401 | |
402 return; | |
403 } | |
404 if ($multibyte && !function_exists('mb_strlen')) { | |
405 $this->markTestSkipped('The function mb_strlen() is not available'); | |
406 | |
407 return; | |
408 } | |
409 | |
410 if (!$success) { | |
411 $this->setExpectedException('\InvalidArgumentException'); | |
412 } | |
413 | |
414 $arg = array_shift($args); | |
415 array_unshift($args, new ArrayIterator(array($arg))); | |
416 | |
417 call_user_func_array(array('Webmozart\Assert\Assert', 'all'.ucfirst($method)), $args); | |
418 } | |
419 | |
420 public function getStringConversions() | |
421 { | |
422 return array( | |
423 array('integer', array('foobar'), 'Expected an integer. Got: string'), | |
424 array('string', array(1), 'Expected a string. Got: integer'), | |
425 array('string', array(true), 'Expected a string. Got: boolean'), | |
426 array('string', array(null), 'Expected a string. Got: NULL'), | |
427 array('string', array(array()), 'Expected a string. Got: array'), | |
428 array('string', array(new stdClass()), 'Expected a string. Got: stdClass'), | |
429 array('string', array(self::getResource()), 'Expected a string. Got: resource'), | |
430 | |
431 array('eq', array('1', '2'), 'Expected a value equal to "2". Got: "1"'), | |
432 array('eq', array(1, 2), 'Expected a value equal to 2. Got: 1'), | |
433 array('eq', array(true, false), 'Expected a value equal to false. Got: true'), | |
434 array('eq', array(true, null), 'Expected a value equal to null. Got: true'), | |
435 array('eq', array(null, true), 'Expected a value equal to true. Got: null'), | |
436 array('eq', array(array(1), array(2)), 'Expected a value equal to array. Got: array'), | |
437 array('eq', array(new ArrayIterator(array()), new stdClass()), 'Expected a value equal to stdClass. Got: ArrayIterator'), | |
438 array('eq', array(1, self::getResource()), 'Expected a value equal to resource. Got: 1'), | |
439 ); | |
440 } | |
441 | |
442 /** | |
443 * @dataProvider getStringConversions | |
444 */ | |
445 public function testConvertValuesToStrings($method, $args, $exceptionMessage) | |
446 { | |
447 $this->setExpectedException('\InvalidArgumentException', $exceptionMessage); | |
448 | |
449 call_user_func_array(array('Webmozart\Assert\Assert', $method), $args); | |
450 } | |
451 } |