-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssertExceptionTest.php
More file actions
144 lines (109 loc) · 3.92 KB
/
AssertExceptionTest.php
File metadata and controls
144 lines (109 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Tests;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Zrnik\PHPUnit\AssertException;
use Zrnik\PHPUnit\Exceptions;
class AssertExceptionTest extends TestCase
{
use Exceptions;
//region PHPUnit Way
public function test_ExpectException_First(): void
{
$exampleObject = new ExampleObject();
$this->expectException(NotInRangeException::class);
$exampleObject->assertRange(0);
//The execution ends here, the method will not continue,
// after first exception thrown, so I need to create
// method for every exception tested...
}
public function test_ExpectException_Second(): void
{
$exampleObject = new ExampleObject();
$this->expectException(NotInRangeException::class);
$exampleObject->assertRange(11);
}
public function test_OK_Values(): void
{
$exampleObject = new ExampleObject();
$exampleObject->assertRange(1);
$exampleObject->assertRange(10);
$this->addToAssertionCount(2); // Yey! Not thrown!
}
//endregion
//region try/catch way
public function test_TryCatch(): void
{
$exampleObject = new ExampleObject();
try {
$exampleObject->assertRange(0);
throw new AssertionFailedError(sprintf("Exception '%s' expected, but not thrown!", NotInRangeException::class));
} catch (NotInRangeException $ex) {
$this->addToAssertionCount(1); // Yey! Thrown!
}
$exampleObject->assertRange(1);
$exampleObject->assertRange(10);
$this->addToAssertionCount(2); // Yey! Not thrown!
try {
$exampleObject->assertRange(11);
throw new AssertionFailedError(sprintf("Exception '%s' expected, but not thrown!", NotInRangeException::class));
} catch (NotInRangeException $ex) {
$this->addToAssertionCount(1); // Yey! Thrown!
}
}
//endregion
//region This Library Way
//region Static
public function test_Library_Static(): void
{
$exampleObject = new ExampleObject();
AssertException::assertExceptionThrown(
NotInRangeException::class,
function () use ($exampleObject) {
$exampleObject->assertRange(0);
}
);
$this->addToAssertionCount(1); //Must be done by hand!
AssertException::assertNoExceptionThrown(function () use ($exampleObject) {
$exampleObject->assertRange(1);
$exampleObject->assertRange(10);
});
$this->addToAssertionCount(2); //Must be done by hand!
$throwable = AssertException::assertExceptionThrown(
NotInRangeException::class,
function () use ($exampleObject) {
$exampleObject->assertRange(11);
}
);
$this->assertInstanceOf(NotInRangeException::class, $throwable);
$this->addToAssertionCount(1); //Must be done by hand!
}
//endregion
//region Trait
public function test_Library_Trait(): void
{
$exampleObject = new ExampleObject();
$throwable = $this->assertExceptionThrown(
NotInRangeException::class,
function () use ($exampleObject) {
$exampleObject->assertRange(0);
}
);
$this->assertInstanceOf(NotInRangeException::class, $throwable);
$this->assertNoExceptionThrown(
function () use ($exampleObject) {
$exampleObject->assertRange(1);
$exampleObject->assertRange(10);
}
);
$throwable = $this->assertExceptionThrown(
NotInRangeException::class,
function () use ($exampleObject) {
$exampleObject->assertRange(11);
}
);
$this->assertInstanceOf(NotInRangeException::class, $throwable);
}
//endregion
//endregion
}