-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
266 lines (230 loc) · 6.4 KB
/
test.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
declare(strict_types=1);
include_once 'Pilot.php';
include_once 'Runner.php';
include_once 'Report.php';
use Exacodis\{
Pilot, Report, Runner
};
$pilot = new Pilot('Exacodis - A PHP minimalist test framework');
$pilot->injectStandardHelpers();
//region resources
$pilot->addResource('year', 2021);
$pilot->addResource('years', [2020, 2021]);
$pilot->addResource('is_leap', false);
$pilot->addResource('current_month', 'september');
$pilot->addResource('dummy_array_data', [
$pilot->getResource('year'), $pilot->getResource('is_leap'), $pilot->getResource('current_month')
]);
//endregion
$pilot->run(
id: '001',
description: 'Resource extractor (integer) - assertIsInt assertEqual assertNotEqual assertIn assertInStrict assertNotIn assertNotInStrict',
test: fn() => $pilot->getResource('year')
);
$pilot->assertIsInt();
$pilot->assertEqual(2021);
$pilot->assertNotEqual(2000);
$pilot->assertIn(['2021']);
$pilot->assertInStrict([2021]);
$pilot->assertNotIn(['2025']);
$pilot->assertNotInStrict(['2021']);
//$pilot->assertNotInStrict([2021]); // as the result is an integer 2021, this assertion should fail (and it does)
$pilot->run(
id: '002',
description: 'Resource extractor (array) - assertIsArray + assertEqual',
test: fn() => $pilot->getResource('years')
);
$pilot->assertIsArray();
$pilot->assertEqual([2020, 2021]);
$pilot->run(
id: '003',
description: 'Resource extractor (composed resource from other already defined resources) - assertIsArray + assertEqual',
test: fn() => $pilot->getResource('dummy_array_data')
);
$pilot->assertIsArray();
$pilot->assertEqual([2021, false, 'september']);
$pilot->run(
id: '004',
description: 'Resource extractor (string) - Dereference array - assertIsString + assertEqual',
test: fn() => $pilot->getResource('dummy_array_data')[2]
);
$pilot->assertIsString();
$pilot->assertEqual('september');
$pilot->run(
id: '005',
description: 'assertIsBool + assertEqual + assertNotEqual',
test: fn() => true
);
$pilot->assertIsBool();
$pilot->assertEqual(true);
$pilot->assertNotEqual(false);
$pilot->run(
id: '006',
description: 'Exception interceptor - assertException (object) + assertException (string) + assertException (specific InvalidArgumentException)',
test: fn() => throw new InvalidArgumentException()
);
$pilot->assertException(new Exception());
$pilot->assertException(Exception::class);
$pilot->assertException(InvalidArgumentException::class);
//$pilot->assertEqual(0); // as the result is an exception, this assertion should fail (and it does)
$pilot->run(
id: '007',
description: 'Count the values: assertCount()',
test: fn() => $pilot->getResource('dummy_array_data')
);
$pilot->assertIsArray();
$pilot->assertCount(3);
//region dynamic assert
$pilot->assert(
test: fn() => count($pilot->getResource('dummy_array_data')) === 3,
test_name: 'Dynamic assertion using manual count',
expected: 3
);
//endregion
//region private/protected methods
class Foo
{
const BAR = 'bar';
private function abc(): string
{
return 'abc';
}
private function def(int $p): int
{
return 2*$p;
}
private function hij(int $p, int $q): int
{
return 2*$p+4*$q;
}
protected function klm(): string
{
return 'klm';
}
protected function nop(int $p): int
{
return 2*$p;
}
protected function qrs(int $p, int $q): int
{
return 2*$p+4*$q;
}
private static function tuv(): string
{
return 'tuv';
}
}
$foo = new Foo();
$pilot->runClassMethod(
id: '008',
description: 'private method unit test using directly an instance of Foo',
class: $foo,
method: 'abc',
);
$pilot->assertIsString();
$pilot->assertEqual('abc');
$pilot->runClassMethod(
id: '009',
description: 'private method unit test using string notation for the class Foo',
class: 'Foo',
method: 'abc',
);
$pilot->assertIsString();
$pilot->assertEqual('abc');
$pilot->runClassMethod(
id: '010',
description: 'private method unit test using short string notation for the class Foo and the method abc',
class: 'Foo::abc',
);
$pilot->assertIsString();
$pilot->assertEqual('abc');
$pilot->runClassMethod(
id: '011',
description: 'private method unit test with one parameter',
class: 'Foo',
method: 'def',
params: [25]
);
$pilot->assertIsInt();
$pilot->assertEqual(50);
$pilot->runClassMethod(
id: '012',
description: 'private method unit test with two parameters',
class: 'Foo',
method: 'hij',
params: ['p' => 25, 'q' => 50]
);
$pilot->assertIsInt();
$pilot->assertEqual(250);
$pilot->runClassMethod(
id: '013',
description: 'protected method unit test using directly an instance of Foo',
class: $foo,
method: 'klm',
);
$pilot->assertIsString();
$pilot->assertEqual('klm');
$pilot->runClassMethod(
id: '014',
description: 'protected method unit test using string notation for the class Foo',
class: 'Foo',
method: 'klm',
);
$pilot->assertIsString();
$pilot->assertEqual('klm');
$pilot->runClassMethod(
id: '015',
description: 'protected method unit test using short string notation for the class Foo and the method abc',
class: 'Foo::klm',
);
$pilot->assertIsString();
$pilot->assertEqual('klm');
$pilot->runClassMethod(
id: '016',
description: 'protected method unit test with one parameter',
class: 'Foo',
method: 'nop',
params: [25]
);
$pilot->assertIsInt();
$pilot->assertEqual(50);
$pilot->runClassMethod(
id: '017',
description: 'protected method unit test with two parameters',
class: 'Foo',
method: 'qrs',
params: [25, 50]
);
$pilot->assertIsInt();
$pilot->assertEqual(250);
$pilot->runClassMethod(
id: '018',
description: 'private static method unit test',
class: 'Foo::tuv',
);
$pilot->assertIsString();
$pilot->assertEqual('tuv');
//endregion
// manual test
$stats = $pilot->getStats();
unset($stats['milliseconds'], $stats['hms']);
$pilot->run(
id: '100',
description: 'check the count',
test: fn() => $stats
);
$pilot->assertIsArray();
$pilot->assertEqual([
'nb_runs' => 18,
'passed_runs' => 18,
'failed_runs' => 0,
'passed_runs_percent' => 100.0,
'failed_runs_percent' => 0.0,
'nb_assertions' => 44,
'passed_assertions' => 44,
'failed_assertions' => 0,
'passed_assertions_percent' => 100.0,
'failed_assertions_percent' => 0.0
]);
$pilot->createReport();