Completed
Pull Request — master (#169)
by Woody
03:57 queued 01:26
created

AssertTest   F

Complexity

Total Complexity 156

Size/Duplication

Total Lines 1208
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 5

Importance

Changes 40
Bugs 2 Features 22
Metric Value
c 40
b 2
f 22
dl 0
loc 1208
rs 0.6314
wmc 156
cbo 5

154 Methods

Rating   Name   Duplication   Size   Complexity  
A dataInvalidFloat() 0 11 1
A testInvalidFloat() 0 5 1
A testValidFloat() 0 6 1
A testInvalidScalar() 0 5 1
A testValidScalar() 0 7 1
A testEmpty() 0 7 1
A dataInvalidEmpty() 0 10 1
A testInvalidEmpty() 0 5 1
A testString() 0 5 1
A testInvalidString() 0 5 1
A dataInvalidString() 0 11 1
A testInvalidEndsWith() 0 5 1
A testInvalidEndsWithDueToWrongEncoding() 0 5 1
A testValidEndsWith() 0 7 1
A dataInvalidInteger() 0 12 1
A dataInvalidIntegerish() 0 10 1
A testInvalidInteger() 0 5 1
A testValidInteger() 0 5 1
A testValidIntegerish() 0 5 1
A testInvalidIntegerish() 0 5 1
A testValidBoolean() 0 5 1
A testInvalidBoolean() 0 5 1
A dataInvalidNotEmpty() 0 10 1
A testInvalidNotEmpty() 0 5 1
A testNotEmpty() 0 7 1
A testNotNull() 0 8 1
A testInvalidNotNull() 0 5 1
A testInvalidRegex() 0 5 1
A testInvalidRegexValueNotString() 0 5 1
A testInvalidMinLength() 0 5 1
A testInvalidMaxLength() 0 5 1
A testInvalidBetweenLengthMin() 0 5 1
A testInvalidBetweenLengthMax() 0 5 1
A testInvalidStartsWith() 0 5 1
A testInvalidContains() 0 5 1
A testInvalidChoice() 0 5 1
A testValidChoice() 0 4 1
A testValidNumeric() 0 6 1
A testInvalidKeyExists() 0 5 1
A testValidKeyExists() 0 4 1
A testInvalidKeyNotExists() 0 5 1
A testValidKeyNotExists() 0 4 1
A dataInvalidNotBlank() 0 13 1
A testValidNotBlank() 0 4 1
A testInvalidNotInstanceOf() 0 5 1
A testValidNotIsInstanceOf() 0 4 1
A testInvalidInstanceOf() 0 5 1
A testValidInstanceOf() 0 4 1
A testInvalidSubclassOf() 0 5 1
A testValidSubclassOf() 0 4 1
A testInvalidRange() 0 6 1
A testValidRange() 0 7 1
A testInvalidEmail() 0 5 1
A testValidEmail() 0 4 1
A testInvalidUrl() 0 6 1
A dataInvalidUrl() 0 11 1
A testValidUrl() 0 4 1
A dataValidUrl() 0 11 1
A testInvalidDigit() 0 5 1
A testValidDigit() 0 6 1
A testNotEq() 0 7 1
A testNotSame() 0 7 1
A testValidContains() 0 5 1
A testInvalidInArray() 0 5 1
A testValidInArray() 0 4 1
A testInvalidNumeric() 0 5 1
A dataInvalidArray() 0 12 1
A testInvalidArray() 0 5 1
A testValidArray() 0 6 1
A testValidMinLength() 0 8 1
A testValidMaxLength() 0 7 1
A testValidBetweenLength() 0 5 1
A testValidStartsWith() 0 7 1
A testInvalidNotBlank() 0 5 1
A testValidAlnum() 0 7 1
A testInvalidAlnum() 0 5 1
A testValidTrue() 0 4 1
A testInvalidTrue() 0 5 1
A testValidFalse() 0 4 1
A testInvalidFalse() 0 5 1
A testInvalidClass() 0 5 1
A testValidClass() 0 4 1
A testSame() 0 8 1
A testEq() 0 8 1
A testNotInArray() 0 8 1
A testMin() 0 9 1
A testMax() 0 9 1
A testNullOr() 0 5 1
A testNullOrWithNoValueThrows() 0 5 1
A testLength() 0 5 1
A dataLengthUtf8Characters() 0 7 1
A testLenghtUtf8Characters() 0 4 1
A testLengthFailed() 0 5 1
A testFile() 0 4 1
A testFileWithEmptyFilename() 0 5 1
A testFileDoesNotExists() 0 5 1
A testDirectory() 0 7 1
A testReadable() 0 7 1
A testWriteable() 0 7 1
A testFailedNullOrMethodCall() 0 4 1
A testIsJsonString() 0 4 1
A isJsonStringDataprovider() 0 9 1
A testIsJsonStringExpectingException() 0 5 1
A isJsonStringInvalidStringDataprovider() 0 7 1
A testValidUuids() 0 4 1
A testInvalidUuids() 0 5 1
A providesValidUuids() 0 11 1
A providesInvalidUuids() 0 10 1
A testValidNotEmptyKey() 0 4 1
A testInvalidNotEmptyKey() 0 5 1
A invalidNotEmptyKeyDataprovider() 0 7 1
A testAllWithSimpleAssertion() 0 4 1
A testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() 0 5 1
A testAllWithComplexAssertion() 0 4 1
A testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() 0 6 1
A testAllWithNoValueThrows() 0 5 1
A testValidCount() 0 5 1
A dataInvalidCount() 0 7 1
A testInvalidCount() 0 5 1
A testChoicesNotEmpty() 0 7 1
A testChoicesNotEmptyExpectingException() 0 8 1
A invalidChoicesProvider() 0 8 1
A testIsObject() 0 4 1
A testIsObjectExpectingException() 0 5 1
A testMethodExists() 0 4 1
A testMethodExistsFailure() 0 5 1
A it_passes_values_and_constraints_to_exception() 0 11 2
A testLessThan() 0 7 1
A invalidLessProvider() 0 11 1
A testLessThanThrowsException() 0 5 1
A testLessOrEqualThan() 0 10 1
A invalidLessOrEqualProvider() 0 8 1
A testLessOrEqualThanThrowsException() 0 5 1
A testGreaterThan() 0 7 1
A invalidGreaterProvider() 0 11 1
A testValidDate() 0 4 1
A validDateProvider() 0 9 1
A testGreaterThanThrowsException() 0 5 1
A testGreaterOrEqualThan() 0 10 1
A invalidGreaterOrEqualProvider() 0 8 1
A testGreaterOrEqualThanThrowsException() 0 5 1
A testInvalidDate() 0 5 1
A invalidDateProvider() 0 8 1
A testInvalidTraversable() 0 5 1
A testInvalidArrayAccessible() 0 5 1
A testInvalidCallable() 0 5 1
A testValidCallable() 0 8 1
A testInvalidStartsWithDueToWrongEncoding() 0 5 1
A testLengthFailedForWrongEncoding() 0 5 1
A testLengthValidForGivenEncoding() 0 4 1
A testImplementsInterface() 0 13 1
A testImplementsInterfaceWithClassObject() 0 15 1
A testInvalidSatisfy() 0 7 1
A testValidSatisfy() 0 14 2

How to fix   Complexity   

Complex Class

Complex classes like AssertTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AssertTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Assert\Tests;
3
4
use Assert\Assertion;
5
use Assert\AssertionFailedException;
6
7
class AssertTest extends \PHPUnit_Framework_TestCase
8
{
9
    public static function dataInvalidFloat()
10
    {
11
        return array(
12
            array(1),
13
            array(false),
14
            array("test"),
15
            array(null),
16
            array("1.23"),
17
            array("10"),
18
        );
19
    }
20
21
    /**
22
     * @dataProvider dataInvalidFloat
23
     */
24
    public function testInvalidFloat($nonFloat)
25
    {
26
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FLOAT);
27
        Assertion::float($nonFloat);
28
    }
29
30
    public function testValidFloat()
31
    {
32
        Assertion::float(1.0);
33
        Assertion::float(0.1);
34
        Assertion::float(-1.1);
35
    }
36
37
    public static function dataInvalidInteger()
38
    {
39
        return array(
40
            array(1.23),
41
            array(false),
42
            array("test"),
43
            array(null),
44
            array("1.23"),
45
            array("10"),
46
            array(new \DateTime()),
47
        );
48
    }
49
50
    /**
51
     * @dataProvider dataInvalidInteger
52
     */
53
    public function testInvalidInteger($nonInteger)
54
    {
55
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGER);
56
        Assertion::integer($nonInteger);
57
    }
58
59
    public function testValidInteger()
60
    {
61
        Assertion::integer(10);
62
        Assertion::integer(0);
63
    }
64
65
    public function testValidIntegerish()
66
    {
67
        Assertion::integerish(10);
68
        Assertion::integerish("10");
69
    }
70
71
    public static function dataInvalidIntegerish()
72
    {
73
        return array(
74
            array(1.23),
75
            array(false),
76
            array("test"),
77
            array(null),
78
            array("1.23"),
79
        );
80
    }
81
82
    /**
83
     * @dataProvider dataInvalidIntegerish
84
     */
85
    public function testInvalidIntegerish($nonInteger)
86
    {
87
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGERISH);
88
        Assertion::integerish($nonInteger);
89
    }
90
91
    public function testValidBoolean()
92
    {
93
        Assertion::boolean(true);
94
        Assertion::boolean(false);
95
    }
96
97
    public function testInvalidBoolean()
98
    {
99
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BOOLEAN);
100
        Assertion::boolean(1);
101
    }
102
103
    public function testInvalidScalar()
104
    {
105
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SCALAR);
106
        Assertion::scalar(new \stdClass);
107
    }
108
109
    public function testValidScalar()
110
    {
111
        Assertion::scalar("foo");
112
        Assertion::scalar(52);
113
        Assertion::scalar(12.34);
114
        Assertion::scalar(false);
115
    }
116
117
    public static function dataInvalidNotEmpty()
118
    {
119
        return array(
120
            array(""),
121
            array(false),
122
            array(0),
123
            array(null),
124
            array( array() ),
125
        );
126
    }
127
128
    /**
129
     * @dataProvider dataInvalidNotEmpty
130
     */
131
    public function testInvalidNotEmpty($value)
132
    {
133
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_EMPTY);
134
        Assertion::notEmpty($value);
135
    }
136
137
    public function testNotEmpty()
138
    {
139
        Assertion::notEmpty("test");
140
        Assertion::notEmpty(1);
141
        Assertion::notEmpty(true);
142
        Assertion::notEmpty( array("foo") );
143
    }
144
145
    public function testEmpty()
146
    {
147
        Assertion::noContent("");
148
        Assertion::noContent(0);
149
        Assertion::noContent(false);
150
        Assertion::noContent( array() );
151
    }
152
153
    public static function dataInvalidEmpty()
154
    {
155
        return array(
156
            array("foo"),
157
            array(true),
158
            array(12),
159
            array( array('foo') ),
160
            array( new \stdClass() ),
161
        );
162
    }
163
164
    /**
165
     * @dataProvider dataInvalidEmpty
166
     */
167
    public function testInvalidEmpty($value)
168
    {
169
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_EMPTY);
170
        Assertion::noContent($value);
171
    }
172
173
    public function testNotNull()
174
    {
175
        Assertion::notNull("1");
176
        Assertion::notNull(1);
177
        Assertion::notNull(0);
178
        Assertion::notNull(array());
179
        Assertion::notNull(false);
180
    }
181
182
    public function testInvalidNotNull()
183
    {
184
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NULL);
185
        Assertion::notNull(null);
186
    }
187
188
    public function testString()
189
    {
190
        Assertion::string("test-string");
191
        Assertion::string("");
192
    }
193
194
    /**
195
     * @dataProvider dataInvalidString
196
     */
197
    public function testInvalidString($invalidString)
198
    {
199
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING);
200
        Assertion::string($invalidString);
201
    }
202
203
    public static function dataInvalidString()
204
    {
205
        return array(
206
            array(1.23),
207
            array(false),
208
            array(new \ArrayObject),
209
            array(null),
210
            array(10),
211
            array(true),
212
        );
213
    }
214
215
    public function testInvalidRegex()
216
    {
217
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_REGEX);
218
        Assertion::regex("foo", "(bar)");
219
    }
220
221
    public function testInvalidRegexValueNotString()
222
    {
223
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING);
224
        Assertion::regex(array("foo"), "(bar)");
225
    }
226
227
    public function testInvalidMinLength()
228
    {
229
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH);
230
        Assertion::minLength("foo", 4);
231
    }
232
233
    public function testValidMinLength()
234
    {
235
        Assertion::minLength("foo", 3);
236
        Assertion::minLength("foo", 1);
237
        Assertion::minLength("foo", 0);
238
        Assertion::minLength("", 0);
239
        Assertion::minLength("址址", 2);
240
    }
241
242
    public function testInvalidMaxLength()
243
    {
244
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH);
245
        Assertion::maxLength("foo", 2);
246
    }
247
248
    public function testValidMaxLength()
249
    {
250
        Assertion::maxLength("foo", 10);
251
        Assertion::maxLength("foo", 3);
252
        Assertion::maxLength("", 0);
253
        Assertion::maxLength("址址", 2);
254
    }
255
256
    public function testInvalidBetweenLengthMin()
257
    {
258
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH);
259
        Assertion::betweenLength("foo", 4, 100);
260
    }
261
262
    public function testInvalidBetweenLengthMax()
263
    {
264
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH);
265
        Assertion::betweenLength("foo", 0, 2);
266
    }
267
268
    public function testValidBetweenLength()
269
    {
270
        Assertion::betweenLength("foo", 0, 3);
271
        Assertion::betweenLength("址址", 2, 2);
272
    }
273
274
    public function testInvalidStartsWith()
275
    {
276
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START);
277
        Assertion::startsWith("foo", "bar");
278
    }
279
280
    public function testInvalidStartsWithDueToWrongEncoding()
281
    {
282
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START);
283
        Assertion::startsWith("址", "址址", null, null, 'ASCII');
284
    }
285
286
    public function testValidStartsWith()
287
    {
288
        Assertion::startsWith("foo", "foo");
289
        Assertion::startsWith("foo", "fo");
290
        Assertion::startsWith("foo", "f");
291
        Assertion::startsWith("址foo", "址");
292
    }
293
294
    public function testInvalidEndsWith()
295
    {
296
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END);
297
        Assertion::endsWith("foo", "bar");
298
    }
299
300
    public function testInvalidEndsWithDueToWrongEncoding()
301
    {
302
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END);
303
        Assertion::endsWith("址", "址址", null, null, 'ASCII');
304
    }
305
306
    public function testValidEndsWith()
307
    {
308
        Assertion::endsWith("foo", "foo");
309
        Assertion::endsWith("sonderbar", "bar");
310
        Assertion::endsWith("opp", "p");
311
        Assertion::endsWith("foo址", "址");
312
    }
313
314
    public function testInvalidContains()
315
    {
316
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_CONTAINS);
317
        Assertion::contains("foo", "bar");
318
    }
319
320
    public function testValidContains()
321
    {
322
        Assertion::contains("foo", "foo");
323
        Assertion::contains("foo", "oo");
324
    }
325
326
    public function testInvalidChoice()
327
    {
328
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE);
329
        Assertion::choice("foo", array("bar", "baz"));
330
    }
331
332
    public function testValidChoice()
333
    {
334
        Assertion::choice("foo", array("foo"));
335
    }
336
337
    public function testInvalidInArray()
338
    {
339
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE);
340
        Assertion::inArray("bar", array("baz"));
341
    }
342
343
    public function testValidInArray()
344
    {
345
        Assertion::inArray("foo", array("foo"));
346
    }
347
348
    public function testInvalidNumeric()
349
    {
350
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NUMERIC);
351
        Assertion::numeric("foo");
352
    }
353
354
    public function testValidNumeric()
355
    {
356
        Assertion::numeric("1");
357
        Assertion::numeric(1);
358
        Assertion::numeric(1.23);
359
    }
360
361
    public static function dataInvalidArray()
362
    {
363
        return array(
364
            array(null),
365
            array(false),
366
            array("test"),
367
            array(1),
368
            array(1.23),
369
            array(new \StdClass),
370
            array(fopen('php://memory', 'r')),
371
        );
372
    }
373
374
    /**
375
     * @dataProvider dataInvalidArray
376
     */
377
    public function testInvalidArray($value)
378
    {
379
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ARRAY);
380
        Assertion::isArray($value);
381
    }
382
383
    public function testValidArray()
384
    {
385
        Assertion::isArray(array());
386
        Assertion::isArray(array(1,2,3));
387
        Assertion::isArray(array(array(),array()));
388
    }
389
390
    public function testInvalidKeyExists()
391
    {
392
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_EXISTS);
393
        Assertion::keyExists(array("foo" => "bar"), "baz");
394
    }
395
396
    public function testValidKeyExists()
397
    {
398
        Assertion::keyExists(array("foo" => "bar"), "foo");
399
    }
400
401
    public function testInvalidKeyNotExists()
402
    {
403
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_NOT_EXISTS);
404
        Assertion::keyNotExists(array("foo" => "bar"), "foo");
405
    }
406
407
    public function testValidKeyNotExists()
408
    {
409
        Assertion::keyNotExists(array("foo" => "bar"), "baz");
410
    }
411
412
    public static function dataInvalidNotBlank()
413
    {
414
        return array(
415
            array(""),
416
            array(" "),
417
            array("\t"),
418
            array("\n"),
419
            array("\r"),
420
            array(false),
421
            array(null),
422
            array( array() ),
423
        );
424
    }
425
426
    /**
427
     * @dataProvider dataInvalidNotBlank
428
     */
429
    public function testInvalidNotBlank($notBlank)
430
    {
431
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_BLANK);
432
        Assertion::notBlank($notBlank);
433
    }
434
435
    public function testValidNotBlank()
436
    {
437
        Assertion::notBlank("foo");
438
    }
439
440
    public function testInvalidNotInstanceOf()
441
    {
442
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_INSTANCE_OF);
443
        Assertion::notIsInstanceOf(new \stdClass, 'stdClass');
444
    }
445
446
    public function testValidNotIsInstanceOf()
447
    {
448
        Assertion::notIsInstanceOf(new \stdClass, 'PDO');
449
    }
450
451
    public function testInvalidInstanceOf()
452
    {
453
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INSTANCE_OF);
454
        Assertion::isInstanceOf(new \stdClass, 'PDO');
455
    }
456
457
    public function testValidInstanceOf()
458
    {
459
        Assertion::isInstanceOf(new \stdClass, 'stdClass');
460
    }
461
462
    public function testInvalidSubclassOf()
463
    {
464
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SUBCLASS_OF);
465
        Assertion::subclassOf(new \stdClass, 'PDO');
466
    }
467
468
    public function testValidSubclassOf()
469
    {
470
        Assertion::subclassOf(new ChildStdClass, 'stdClass');
471
    }
472
473
    public function testInvalidRange()
474
    {
475
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_RANGE);
476
        Assertion::range(1, 2, 3);
477
        Assertion::range(1.5, 2, 3);
478
    }
479
480
    public function testValidRange()
481
    {
482
        Assertion::range(1, 1, 2);
483
        Assertion::range(2, 1, 2);
484
        Assertion::range(2, 0, 100);
485
        Assertion::range(2.5, 2.25, 2.75);
486
    }
487
488
    public function testInvalidEmail()
489
    {
490
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EMAIL);
491
        Assertion::email("foo");
492
    }
493
494
    public function testValidEmail()
495
    {
496
        Assertion::email("[email protected]");
497
    }
498
499
    /**
500
     * @dataProvider dataInvalidUrl
501
     */
502
    public function testInvalidUrl($url)
503
    {
504
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_URL);
505
506
        Assertion::url($url);
507
    }
508
509
    public static function dataInvalidUrl()
510
    {
511
        return array(
512
            'null value' => array(""),
513
            'empty string' => array(" "),
514
            'no scheme' => array("url.de"),
515
            'unsupported scheme' => array("git://url.de"),
516
            'Http with query (no / between tld und ?)' => array("http://example.org?do=something"),
517
            'Http with query and port (no / between port und ?)' => array("http://example.org:8080?do=something"),
518
        );
519
    }
520
521
    /**
522
     * @dataProvider dataValidUrl
523
     */
524
    public function testValidUrl($url)
525
    {
526
        Assertion::url($url);
527
    }
528
529
    public static function dataValidUrl()
530
    {
531
        return array(
532
            'straight with Http' => array("http://example.org"),
533
            'Http with path' => array("http://example.org/do/something"),
534
            'Http with query' => array("http://example.org/index.php?do=something"),
535
            'Http with port' => array("http://example.org:8080"),
536
            'Http with all possibilities' => array("http://example.org:8080/do/something/index.php?do=something"),
537
            'straight with Https' => array("https://example.org"),
538
        );
539
    }
540
541
    public function testInvalidDigit()
542
    {
543
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DIGIT);
544
        Assertion::digit(-1);
545
    }
546
547
    public function testValidDigit()
548
    {
549
        Assertion::digit(1);
550
        Assertion::digit(0);
551
        Assertion::digit("0");
552
    }
553
554
    public function testValidAlnum()
555
    {
556
        Assertion::alnum("a");
557
        Assertion::alnum("a1");
558
        Assertion::alnum("aasdf1234");
559
        Assertion::alnum("a1b2c3");
560
    }
561
562
    public function testInvalidAlnum()
563
    {
564
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_ALNUM);
565
        Assertion::alnum("1a");
566
    }
567
568
    public function testValidTrue()
569
    {
570
        Assertion::true(1 == 1);
571
    }
572
573
    public function testInvalidTrue()
574
    {
575
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE);
576
        Assertion::true(false);
577
    }
578
579
    public function testValidFalse()
580
    {
581
        Assertion::false(1 == 0);
582
    }
583
584
    public function testInvalidFalse()
585
    {
586
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FALSE);
587
        Assertion::false(true);
588
    }
589
590
    public function testInvalidClass()
591
    {
592
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CLASS);
593
        Assertion::classExists("Foo");
594
    }
595
596
    public function testValidClass()
597
    {
598
        Assertion::classExists("\\Exception");
599
    }
600
601
    public function testSame()
602
    {
603
        Assertion::same(1,1);
604
        Assertion::same("foo","foo");
605
        Assertion::same($obj = new \stdClass(), $obj);
606
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SAME);
607
        Assertion::same(new \stdClass(), new \stdClass());
608
    }
609
610
    public function testEq()
611
    {
612
        Assertion::eq(1,"1");
613
        Assertion::eq("foo",true);
614
        Assertion::eq($obj = new \stdClass(), $obj);
615
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_EQ);
616
        Assertion::eq("2", 1);
617
    }
618
619
    public function testNotEq()
620
    {
621
        Assertion::NotEq("1", false);
622
        Assertion::NotEq(new \stdClass(), array());
623
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_EQ);
624
        Assertion::NotEq("1", 1);
625
    }
626
627
    public function testNotSame()
628
    {
629
        Assertion::notSame("1", 2);
630
        Assertion::notSame(new \stdClass(), array());
631
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_SAME);
632
        Assertion::notSame(1, 1);
633
    }
634
635
    public function testNotInArray()
636
    {
637
        Assertion::notInArray(6, range(1, 5));
638
639
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_VALUE_IN_ARRAY);
640
        Assertion::notInArray(1, range(1, 5));
641
        Assertion::notInArray(range('a', 'c'), range('a', 'd'));
642
    }
643
644
    public function testMin()
645
    {
646
        Assertion::min(1, 1);
647
        Assertion::min(2, 1);
648
        Assertion::min(2.5, 1);
649
650
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN);
651
        Assertion::min(0, 1);
652
    }
653
654
    public function testMax()
655
    {
656
        Assertion::max(1, 1);
657
        Assertion::max(0.5, 1);
658
        Assertion::max(0, 1);
659
660
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX);
661
        Assertion::max(2, 1);
662
    }
663
664
    public function testNullOr()
665
    {
666
        Assertion::nullOrMax(null, 1);
667
        Assertion::nullOrMax(null, 2);
668
    }
669
670
    public function testNullOrWithNoValueThrows()
671
    {
672
        $this->setExpectedException('BadMethodCallException');
673
        Assertion::nullOrMax();
0 ignored issues
show
Bug introduced by
The call to nullOrMax() misses some required arguments starting with $value.
Loading history...
674
    }
675
676
    public function testLength()
677
    {
678
        Assertion::length("asdf", 4);
679
        Assertion::length("", 0);
680
    }
681
682
    public static function dataLengthUtf8Characters()
683
    {
684
        return array(
685
            array("址", 1),
686
            array("ل", 1),
687
        );
688
    }
689
690
    /**
691
     * @dataProvider dataLengthUtf8Characters
692
     */
693
    public function testLenghtUtf8Characters($value, $expected)
694
    {
695
        Assertion::length($value, $expected);
696
    }
697
698
    public function testLengthFailed()
699
    {
700
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH);
701
        Assertion::length("asdf", 3);
702
    }
703
704
    public function testLengthFailedForWrongEncoding()
705
    {
706
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LENGTH);
707
        Assertion::length("址", 1, null, null, 'ASCII');
708
    }
709
710
    public function testLengthValidForGivenEncoding()
711
    {
712
        Assertion::length("址", 1, null, null, 'utf8');
713
    }
714
715
    public function testFile()
716
    {
717
        Assertion::file(__FILE__);
718
    }
719
720
    public function testFileWithEmptyFilename()
721
    {
722
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_EMPTY);
723
        Assertion::file("");
724
    }
725
726
    public function testFileDoesNotExists()
727
    {
728
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FILE);
729
        Assertion::file(__DIR__ . '/does-not-exists');
730
    }
731
732
    public function testDirectory()
733
    {
734
        Assertion::directory(__DIR__);
735
736
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DIRECTORY);
737
        Assertion::directory(__DIR__ . '/does-not-exist');
738
    }
739
740
    public function testReadable()
741
    {
742
        Assertion::readable(__FILE__);
743
744
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_READABLE);
745
        Assertion::readable(__DIR__ . '/does-not-exist');
746
    }
747
748
    public function testWriteable()
749
    {
750
        Assertion::writeable(sys_get_temp_dir());
751
752
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_WRITEABLE);
753
        Assertion::writeable(__DIR__ . '/does-not-exist');
754
    }
755
756
    /**
757
     * @expectedException \BadMethodCallException
758
     * @expectedExceptionMessage No assertion
759
     */
760
    public function testFailedNullOrMethodCall()
761
    {
762
        Assertion::NullOrAssertionDoesNotExist();
763
    }
764
765
    public function testImplementsInterface()
766
    {
767
        Assertion::implementsInterface(
768
            '\ArrayIterator',
769
            '\Traversable'
770
        );
771
772
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED);
773
        Assertion::implementsInterface(
774
            '\Exception',
775
            '\Traversable'
776
        );
777
    }
778
779
    public function testImplementsInterfaceWithClassObject()
780
    {
781
        $class = new \ArrayObject();
782
783
        Assertion::implementsInterface(
784
            $class,
785
            '\Traversable'
786
        );
787
788
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INTERFACE_NOT_IMPLEMENTED);
789
        Assertion::implementsInterface(
790
            $class,
791
            '\SplObserver'
792
        );
793
    }
794
795
    /**
796
     * @dataProvider isJsonStringDataprovider
797
     */
798
    public function testIsJsonString($content)
799
    {
800
        Assertion::isJsonString($content);
801
    }
802
803
    public static function isJsonStringDataprovider()
804
    {
805
        return array(
806
            '»null« value' => array(json_encode(null)),
807
            '»false« value' => array(json_encode(false)),
808
            'array value' => array('["false"]'),
809
            'object value' => array('{"tux":"false"}'),
810
        );
811
    }
812
813
    /**
814
     * @dataProvider isJsonStringInvalidStringDataprovider
815
     */
816
    public function testIsJsonStringExpectingException($invalidString)
817
    {
818
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_JSON_STRING);
819
        Assertion::isJsonString($invalidString);
820
    }
821
822
    public static function isJsonStringInvalidStringDataprovider()
823
    {
824
        return array(
825
            'no json string' => array('invalid json encoded string'),
826
            'error in json string' => array('{invalid json encoded string}'),
827
        );
828
    }
829
830
    /**
831
     * @dataProvider providesValidUuids
832
     */
833
    public function testValidUuids($uuid)
834
    {
835
        Assertion::uuid($uuid);
836
    }
837
838
    /**
839
     * @dataProvider providesInvalidUuids
840
     */
841
    public function testInvalidUuids($uuid)
842
    {
843
        $this->setExpectedException('Assert\InvalidArgumentException');
844
        Assertion::uuid($uuid);
845
    }
846
847
    static public function providesValidUuids()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
848
    {
849
        return array(
850
            array('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'),
851
            array('ff6f8cb0-c57d-21e1-9b21-0800200c9a66'),
852
            array('ff6f8cb0-c57d-31e1-9b21-0800200c9a66'),
853
            array('ff6f8cb0-c57d-41e1-9b21-0800200c9a66'),
854
            array('ff6f8cb0-c57d-51e1-9b21-0800200c9a66'),
855
            array('FF6F8CB0-C57D-11E1-9B21-0800200C9A66'),
856
        );
857
    }
858
859
    static public function providesInvalidUuids()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
860
    {
861
        return array(
862
            array('zf6f8cb0-c57d-11e1-9b21-0800200c9a66'),
863
            array('af6f8cb0c57d11e19b210800200c9a66'),
864
            array('ff6f8cb0-c57da-51e1-9b21-0800200c9a66'),
865
            array('af6f8cb-c57d-11e1-9b21-0800200c9a66'),
866
            array('3f6f8cb0-c57d-11e1-9b21-0800200c9a6'),
867
        );
868
    }
869
870
    public function testValidNotEmptyKey()
871
    {
872
        Assertion::notEmptyKey(array('keyExists' => 'notEmpty'), 'keyExists');
873
    }
874
875
    /**
876
     * @dataProvider invalidNotEmptyKeyDataprovider
877
     */
878
    public function testInvalidNotEmptyKey($invalidArray, $key)
879
    {
880
        $this->setExpectedException('Assert\InvalidArgumentException');
881
        Assertion::notEmptyKey($invalidArray, $key);
882
    }
883
884
    public static function invalidNotEmptyKeyDataprovider()
885
    {
886
        return array(
887
            'empty'          => array(array('keyExists' => ''), 'keyExists'),
888
            'key not exists' => array(array('key' => 'notEmpty'), 'keyNotExists')
889
        );
890
    }
891
892
    public function testAllWithSimpleAssertion()
893
    {
894
        Assertion::allTrue(array(true, true));
895
    }
896
897
    public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion()
898
    {
899
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_TRUE);
900
        Assertion::allTrue(array(true, false));
901
    }
902
903
    public function testAllWithComplexAssertion()
904
    {
905
        Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'stdClass');
906
    }
907
908
    public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion()
909
    {
910
        $this->setExpectedException('Assert\AssertionFailedException', 'Assertion failed', Assertion::INVALID_INSTANCE_OF);
911
912
        Assertion::allIsInstanceOf(array(new \stdClass, new \stdClass), 'PDO', 'Assertion failed', 'foos');
913
    }
914
915
    public function testAllWithNoValueThrows()
916
    {
917
        $this->setExpectedException('BadMethodCallException');
918
        Assertion::allTrue();
0 ignored issues
show
Bug introduced by
The call to allTrue() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
919
    }
920
921
    public function testValidCount()
922
    {
923
        Assertion::count(array('Hi'), 1);
924
        Assertion::count(new OneCountable(), 1);
925
    }
926
927
    public static function dataInvalidCount()
928
    {
929
        return array(
930
            array(array('Hi', 'There'), 3),
931
            array(new OneCountable(), 2),
932
        );
933
    }
934
935
    /**
936
     * @dataProvider dataInvalidCount
937
     */
938
    public function testInvalidCount($countable, $count)
939
    {
940
        $this->setExpectedException('Assert\AssertionFailedException', 'List does not contain exactly "'.$count.'" elements.', Assertion::INVALID_COUNT);
941
        Assertion::count($countable, $count);
942
    }
943
944
    public function testChoicesNotEmpty()
945
    {
946
        Assertion::choicesNotEmpty(
947
            array('tux' => 'linux', 'Gnu' => 'dolphin'),
948
            array('tux')
949
        );
950
    }
951
952
    /**
953
     * @dataProvider invalidChoicesProvider
954
     */
955
    public function testChoicesNotEmptyExpectingException($values, $choices, $exceptionCode)
956
    {
957
        $this->setExpectedException('Assert\AssertionFailedException', null, $exceptionCode);
958
        Assertion::choicesNotEmpty(
959
            $values,
960
            $choices
961
        );
962
    }
963
964
    public function invalidChoicesProvider()
965
    {
966
        return array(
967
            'empty values' => array(array(), array('tux'), Assertion::VALUE_EMPTY),
968
            'empty recodes in $values' => array(array('tux' => ''), array('tux'), Assertion::VALUE_EMPTY),
969
            'choice not found in values' => array(array('tux' => ''), array('invalidChoice'), Assertion::INVALID_KEY_ISSET),
970
        );
971
    }
972
973
    public function testIsObject()
974
    {
975
        Assertion::isObject(new \StdClass);
976
    }
977
978
    public function testIsObjectExpectingException()
979
    {
980
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_OBJECT);
981
        Assertion::isObject('notAnObject');
982
    }
983
984
    public function testMethodExists()
985
    {
986
        Assertion::methodExists('methodExists', new Assertion());
987
    }
988
989
    public function testMethodExistsFailure()
990
    {
991
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_METHOD);
992
        Assertion::methodExists('methodNotExists', new Assertion());
993
    }
994
995
    /**
996
     * @test
997
     */
998
    public function it_passes_values_and_constraints_to_exception()
0 ignored issues
show
Coding Style introduced by
Method name "AssertTest::it_passes_values_and_constraints_to_exception" is not in camel caps format
Loading history...
999
    {
1000
        try {
1001
            Assertion::range(0, 10, 20);
1002
1003
            $this->fail('Exception expected');
1004
        } catch (AssertionFailedException $e) {
1005
            $this->assertEquals(0, $e->getValue());
1006
            $this->assertEquals(array('min' => 10, 'max' => 20), $e->getConstraints());
1007
        }
1008
    }
1009
1010
    public function testLessThan()
1011
    {
1012
        Assertion::lessThan(1, 2);
1013
        Assertion::lessThan('aaa', 'bbb');
1014
        Assertion::lessThan('aaa', 'aaaa');
1015
        Assertion::lessThan(new \DateTime('today'), new \DateTime('tomorrow'));
1016
    }
1017
1018
    public function invalidLessProvider()
1019
    {
1020
        return array(
1021
            array(2, 1),
1022
            array(2, 2),
1023
            array('aaa', 'aaa'),
1024
            array('aaaa', 'aaa'),
1025
            array(new \DateTime('today'), new \DateTime('yesterday')),
1026
            array(new \DateTime('today'), new \DateTime('today')),
1027
        );
1028
    }
1029
1030
    /**
1031
     * @dataProvider invalidLessProvider
1032
     */
1033
    public function testLessThanThrowsException($value, $limit)
1034
    {
1035
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LESS);
1036
        Assertion::lessThan($value, $limit);
1037
    }
1038
1039
    public function testLessOrEqualThan()
1040
    {
1041
        Assertion::lessOrEqualThan(1, 2);
1042
        Assertion::lessOrEqualThan(1, 1);
1043
        Assertion::lessOrEqualThan('aaa', 'bbb');
1044
        Assertion::lessOrEqualThan('aaa', 'aaaa');
1045
        Assertion::lessOrEqualThan('aaa', 'aaa');
1046
        Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('tomorrow'));
1047
        Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('today'));
1048
    }
1049
1050
    public function invalidLessOrEqualProvider()
1051
    {
1052
        return array(
1053
            array(2, 1),
1054
            array('aaaa', 'aaa'),
1055
            array(new \DateTime('today'), new \DateTime('yesterday')),
1056
        );
1057
    }
1058
1059
    /**
1060
     * @dataProvider invalidLessOrEqualProvider
1061
     */
1062
    public function testLessOrEqualThanThrowsException($value, $limit)
1063
    {
1064
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_LESS_OR_EQUAL);
1065
        Assertion::lessOrEqualThan($value, $limit);
1066
    }
1067
1068
    public function testGreaterThan()
1069
    {
1070
        Assertion::greaterThan(2, 1);
1071
        Assertion::greaterThan('bbb', 'aaa');
1072
        Assertion::greaterThan('aaaa', 'aaa');
1073
        Assertion::greaterThan(new \DateTime('tomorrow'), new \DateTime('today'));
1074
    }
1075
1076
    public function invalidGreaterProvider()
1077
    {
1078
        return array(
1079
            array(1, 2),
1080
            array(2, 2),
1081
            array('aaa', 'aaa'),
1082
            array('aaa', 'aaaa'),
1083
            array(new \DateTime('yesterday'), new \DateTime('today')),
1084
            array(new \DateTime('today'), new \DateTime('today')),
1085
        );
1086
    }
1087
1088
    /**
1089
     * @dataProvider validDateProvider
1090
     */
1091
    public function testValidDate($value, $format)
1092
    {
1093
        Assertion::date($value, $format);
1094
    }
1095
1096
    public function validDateProvider()
1097
    {
1098
        return array(
1099
            array('2012-03-13', 'Y-m-d'),
1100
            array('29.02.2012 12:03:36.432563', 'd.m.Y H:i:s.u'),
1101
            array('13.08.2015 17:08:23 Thu Thursday th 224 August Aug 8 15 17 432563 UTC UTC', 'd.m.Y H:i:s D l S z F M n y H u e T'),
1102
            array('1439486158', 'U')
1103
        );
1104
    }
1105
1106
    /**
1107
     * @dataProvider invalidGreaterProvider
1108
     */
1109
    public function testGreaterThanThrowsException($value, $limit)
1110
    {
1111
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER);
1112
        Assertion::greaterThan($value, $limit);
1113
    }
1114
1115
    public function testGreaterOrEqualThan()
1116
    {
1117
        Assertion::greaterOrEqualThan(2, 1);
1118
        Assertion::greaterOrEqualThan(1, 1);
1119
        Assertion::greaterOrEqualThan('bbb', 'aaa');
1120
        Assertion::greaterOrEqualThan('aaaa', 'aaa');
1121
        Assertion::greaterOrEqualThan('aaa', 'aaa');
1122
        Assertion::greaterOrEqualThan(new \DateTime('tomorrow'), new \DateTime('today'));
1123
        Assertion::greaterOrEqualThan(new \DateTime('today'), new \DateTime('today'));
1124
    }
1125
1126
    public function invalidGreaterOrEqualProvider()
1127
    {
1128
        return array(
1129
            array(1, 2),
1130
            array('aaa', 'aaaa'),
1131
            array(new \DateTime('yesterday'), new \DateTime('tomorrow')),
1132
        );
1133
    }
1134
1135
    /**
1136
     * @dataProvider invalidGreaterOrEqualProvider
1137
     *
1138
     * @param mixed $value
1139
     * @param mixed $limit
1140
     */
1141
    public function testGreaterOrEqualThanThrowsException($value, $limit)
1142
    {
1143
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_GREATER_OR_EQUAL);
1144
        Assertion::greaterOrEqualThan($value, $limit);
1145
    }
1146
1147
    /**
1148
     * @dataProvider invalidDateProvider
1149
     */
1150
    public function testInvalidDate($value, $format)
1151
    {
1152
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_DATE);
1153
        Assertion::date($value, $format);
1154
    }
1155
1156
    public function invalidDateProvider()
1157
    {
1158
        return array(
1159
            array('this is not the date', 'Y-m-d'),
1160
            array('2011-02-29', 'Y-m-d'),
1161
            array('2012.02.29 12:60:36.432563', 'Y.m.d H:i:s.u')
1162
        );
1163
    }
1164
1165
    public function testInvalidTraversable()
1166
    {
1167
        $this->setExpectedException('Assert\InvalidArgumentException', null, Assertion::INVALID_TRAVERSABLE);
1168
        Assertion::isTraversable('not traversable');
1169
    }
1170
1171
    public function testInvalidArrayAccessible()
1172
    {
1173
        $this->setExpectedException('Assert\InvalidArgumentException', null, Assertion::INVALID_ARRAY_ACCESSIBLE);
1174
        Assertion::isArrayAccessible('not array accessible');
1175
    }
1176
1177
    public function testInvalidCallable()
1178
    {
1179
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CALLABLE);
1180
        Assertion::isCallable("nonExistingFunction");
1181
    }
1182
1183
    public function testValidCallable()
1184
    {
1185
        Assertion::isCallable('\is_callable');
1186
        Assertion::isCallable(__NAMESPACE__ . "\\someCallable");
1187
        Assertion::isCallable(array(__NAMESPACE__ . "\\OneCountable", "count"));
1188
        Assertion::isCallable(function () {
1189
        });
1190
    }
1191
1192
    public function testInvalidSatisfy()
1193
    {
1194
        $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SATISFY);
1195
        Assertion::satisfy(null, function ($value) {
1196
            return !is_null($value);
1197
        });
1198
    }
1199
1200
    public function testValidSatisfy()
1201
    {
1202
        // Should not fail with true return
1203
        Assertion::satisfy(null, function ($value) {
1204
            return is_null($value);
1205
        });
1206
1207
        // Should not fail with void return
1208
        Assertion::satisfy(true, function ($value) {
1209
            if (!is_bool($value)) {
1210
                return false;
1211
            }
1212
        });
1213
    }
1214
}
1215
1216
class ChildStdClass extends \stdClass
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1217
{
1218
1219
}
1220
1221
class OneCountable implements \Countable
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1222
{
1223
    public function count()
1224
    {
1225
        return 1;
1226
    }
1227
}
1228
1229
function someCallable()
1230
{
1231
1232
}
1233