1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Assert |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this package in the file LICENSE.txt. |
9
|
|
|
* If you did not receive a copy of the license and are unable to |
10
|
|
|
* obtain it through the world-wide-web, please send an email |
11
|
|
|
* to [email protected] so I can send you a copy immediately. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Assert\Tests; |
15
|
|
|
|
16
|
|
|
use Assert\Assert; |
17
|
|
|
use Assert\Assertion; |
18
|
|
|
use Assert\InvalidArgumentException; |
19
|
|
|
use Assert\LazyAssertionException; |
20
|
|
|
|
21
|
|
|
class CustomAssertionClassTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
CustomAssertion::clearCalls(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
*/ |
31
|
|
|
public function it_uses_custom_exception_class() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->expectException(CustomException::class); |
34
|
|
|
CustomAssertion::integer('foo'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
*/ |
40
|
|
|
public function it_uses_custom_assertion_class_for_assertion_chains() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$string = 's' . uniqid(); |
43
|
|
|
CustomAssert::that($string)->string(); |
44
|
|
|
$this->assertSame([['string', $string]], CustomAssertion::getCalls()); |
45
|
|
|
|
46
|
|
|
$this->expectException(CustomException::class); |
47
|
|
|
CustomAssert::that($string)->integer(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
*/ |
53
|
|
|
public function it_uses_custom_exception_for_lazy_assertion_chains() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->expectException(CustomLazyAssertionException::class); |
56
|
|
|
CustomAssert::lazy() |
57
|
|
|
->that('foo', 'foo')->integer() |
58
|
|
|
->verifyNow() |
59
|
|
|
; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
*/ |
65
|
|
|
public function it_uses_custom_exception_for_lazy_assertion_chains_when_first_assertion_does_not_fail() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->expectException(CustomLazyAssertionException::class); |
68
|
|
|
CustomAssert::lazy() |
69
|
|
|
->that('foo', 'foo')->string() |
70
|
|
|
->that('bar', 'bar')->integer() |
71
|
|
|
->verifyNow() |
72
|
|
|
; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
class CustomException extends InvalidArgumentException |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
class CustomLazyAssertionException extends LazyAssertionException |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
class CustomAssertion extends Assertion |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
protected static $exceptionClass = CustomException::class; |
87
|
|
|
private static $calls = []; |
88
|
|
|
|
89
|
|
|
public static function clearCalls() |
90
|
|
|
{ |
91
|
|
|
self::$calls = []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function getCalls() |
95
|
|
|
{ |
96
|
|
|
return self::$calls; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function string($value, $message = null, $propertyPath = null) |
100
|
|
|
{ |
101
|
|
|
self::$calls[] = ['string', $value]; |
102
|
|
|
return parent::string($value, $message, $propertyPath); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
class CustomAssert extends Assert |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
protected static $assertionClass = CustomAssertion::class; |
109
|
|
|
protected static $lazyAssertionExceptionClass = CustomLazyAssertionException::class; |
110
|
|
|
} |
111
|
|
|
|