1 | <?php |
||
2 | |||
3 | /** |
||
4 | * TestSuiteSpec.php |
||
5 | * |
||
6 | * This file tests the behavior of the TestSuite class. |
||
7 | * |
||
8 | * PHP version 7.4 |
||
9 | * |
||
10 | * @category Core |
||
11 | * @package RedboxTestSuite |
||
12 | * @author Johnny Mast <[email protected]> |
||
13 | * @license https://opensource.org/licenses/MIT MIT |
||
14 | * @link https://github.com/johnnymast/redbox-testsuite |
||
15 | * @since 1.0 |
||
16 | */ |
||
17 | |||
18 | namespace spec\Redbox\Testsuite; |
||
19 | |||
20 | use PhpSpec\ObjectBehavior; |
||
21 | use PhpSpec\Wrapper\Collaborator as CollaboratorAlias; |
||
22 | use Redbox\Testsuite\Interfaces\ContainerInterface; |
||
23 | use Redbox\Testsuite\TestCase; |
||
24 | use Redbox\Testsuite\Tests\Assets\MockableContainer; |
||
25 | use Redbox\Testsuite\Tests\Assets\MockableTestCase; |
||
26 | use Redbox\Testsuite\TestSuite; |
||
27 | |||
28 | /** |
||
29 | * Class |
||
30 | * TestSuiteSpec |
||
31 | * |
||
32 | * @package spec\Redbox\Testsuite |
||
33 | */ |
||
34 | class TestSuiteSpec extends ObjectBehavior |
||
35 | { |
||
36 | /** |
||
37 | * Provides |
||
38 | * - shouldBeInstanceOfClassByName() |
||
39 | * - shouldNotBeInstanceOfClassByName() |
||
40 | * - shouldReturnImplementationOf() |
||
41 | * - shouldNotReturnImplementationOf() |
||
42 | * |
||
43 | * @return \Closure[] |
||
44 | */ |
||
45 | public function getMatchers(): array |
||
46 | { |
||
47 | return [ |
||
48 | 'haveInstancesOfObjectWithType' => function ($subject, $type, $num = 0, $counter = 0) { |
||
49 | foreach ($subject as $test) { |
||
50 | if ($test instanceof $type) { |
||
51 | $counter++; |
||
52 | } |
||
53 | } |
||
54 | return ($counter == $num); |
||
55 | }, |
||
56 | 'returnImplementationOf' => function ($subject, $interface) { |
||
57 | return ($subject instanceof $interface); |
||
58 | } |
||
59 | ]; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Test the TestSuite class is initializable. |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | function it_is_initializable() |
||
0 ignored issues
–
show
|
|||
68 | { |
||
69 | $this->shouldHaveType(TestSuite::class); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Check if there is a default container present upon initialization. |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | function it_should_have_a_default_container() |
||
78 | { |
||
79 | $this->getContainer()->shouldReturnImplementationOf(ContainerInterface::class); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Test if setting and then retrieving a container via getContainer |
||
84 | * returns the same object. |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | function it_should_be_abled_of_setting_custom_container() |
||
89 | { |
||
90 | $container = new MockableContainer(); |
||
91 | |||
92 | $this->setContainer($container); |
||
93 | |||
94 | $this->getContainer()->shouldBeAnInstanceOf(MockableContainer::class); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Test that an invalid argument exception will be thrown if an invalid test is being |
||
99 | * attached to the testsuite. |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | function it_should_not_attach_unknown_tests() |
||
104 | { |
||
105 | $this->shouldThrow(\InvalidArgumentException::class)->during('attach', ['invalid']); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Test that attaching a Test works. |
||
110 | * |
||
111 | * @param CollaboratorAlias $test This is a fake instance of the Test Abstract. |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | function it_should_attach_a_test($test) |
||
116 | { |
||
117 | $test->beADoubleOf(TestCase::class); |
||
118 | |||
119 | $this->attach($test); |
||
120 | $this->has($test)->shouldReturn(true); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Test that one Test can be attached to the test suite. |
||
125 | * |
||
126 | * @param CollaboratorAlias $test This is a fake instance of the Test Abstract. |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | function it_should_detach_a_test(CollaboratorAlias $test) |
||
131 | { |
||
132 | $test->beADoubleOf(TestCase::class); |
||
133 | |||
134 | $this->detach($test); |
||
135 | $this->has($test)->shouldReturn(false); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Test getTests returns all tests. |
||
140 | * |
||
141 | * @param CollaboratorAlias $test This is a fake instance of the Test Abstract. |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | function it_should_return_all_tests_with_gettests(CollaboratorAlias $test) |
||
146 | { |
||
147 | $test->beADoubleOf(TestCase::class); |
||
148 | |||
149 | $this->attach($test); |
||
150 | |||
151 | $this->getTests()->shouldHaveCount(1); |
||
152 | $this->getTests()->shouldContain($test); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Test that multiple classes can be attacked by passing the attach function an array with tests. |
||
157 | * |
||
158 | * @param CollaboratorAlias $test1 This is a fake instance of the Test Abstract. |
||
159 | * @param CollaboratorAlias $test2 This is an other fake instance of the Test Abstract. |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | function it_should_allow_for_multiple_attachments(CollaboratorAlias $test1, CollaboratorAlias $test2) |
||
164 | { |
||
165 | $test1->beADoubleOf(TestCase::class); |
||
166 | $test2->beADoubleOf(TestCase::class); |
||
167 | |||
168 | $this->attach([$test1, $test2]); |
||
169 | |||
170 | $this->has($test1)->shouldReturn(true); |
||
171 | $this->has($test2)->shouldReturn(true); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Test one test can be attached by just using a classname. This |
||
176 | * test will automatically be loaded by the test suite. |
||
177 | * |
||
178 | * @return void |
||
179 | */ |
||
180 | function it_can_attach_one_test_by_class_name() |
||
181 | { |
||
182 | $this->getTests()->shouldHaveCount(0); |
||
183 | $this->attach([MockableTestCase::class]); |
||
184 | |||
185 | $this->getTests()->shouldHaveCount(1); |
||
186 | |||
187 | $this->getTests()->shouldHaveInstancesOfObjectWithType(MockableTestCase::class, 1); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Test one test can be attached by just using a classname. This |
||
192 | * test will automatically be loaded by the test suite. |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | function it_can_attach_multiple_tests_by_class_name() |
||
197 | { |
||
198 | $this->getTests()->shouldHaveCount(0); |
||
199 | $this->attach([MockableTestCase::class, MockableTestCase::class]); |
||
200 | |||
201 | $this->getTests()->shouldHaveCount(2); |
||
202 | |||
203 | $this->getTests()->shouldHaveInstancesOfObjectWithType(MockableTestCase::class, 2); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Test that a test suite could run a single test. |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | function it_should_run_a_single_test() |
||
212 | { |
||
213 | $test = MockableTestCase::create(); |
||
214 | |||
215 | $this->attach($test); |
||
216 | $this->run()->shouldReturn(1); |
||
217 | |||
218 | /** |
||
219 | * Dont know if we need this but lets |
||
220 | * do it. |
||
221 | */ |
||
222 | $this->detach($test); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Test that a test suite can run multiple tests. |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | function it_should_run_a_multiple_tests() |
||
231 | { |
||
232 | $test1 = MockableTestCase::create(); |
||
233 | $test2 = MockableTestCase::create(); |
||
234 | |||
235 | $this->attach([$test1, $test2]); |
||
236 | $this->run()->shouldReturn(2); |
||
237 | |||
238 | /** |
||
239 | * Don't know if we need this but lets |
||
240 | * do it. |
||
241 | */ |
||
242 | $this->detach($test1); |
||
243 | $this->detach($test2); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Test that a test score will be returned to the test suite |
||
248 | * correctly. |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | function it_should_score_five_with_one_test() |
||
253 | { |
||
254 | $test = MockableTestCase::createWith(5); |
||
255 | $this->attach($test); |
||
256 | |||
257 | $this->run(false); |
||
258 | $this->getScore()->shouldReturn(5); |
||
259 | |||
260 | /** |
||
261 | * Don't know if we need this but lets |
||
262 | * do it. |
||
263 | */ |
||
264 | $this->detach($test); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Testing that the score of multiple tests in a suite |
||
269 | * will be summed together correctly. |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | function it_should_score_eight_with_both_of_the_tests() |
||
274 | { |
||
275 | $test1 = MockableTestCase::createWith(5); |
||
276 | $test2 = MockableTestCase::createWith(3); |
||
277 | |||
278 | $this->attach([$test1, $test2]); |
||
279 | |||
280 | $this->run(false); |
||
281 | $this->getScore()->shouldReturn(8); |
||
282 | |||
283 | /** |
||
284 | * Don't know if we need this but lets |
||
285 | * do it. |
||
286 | */ |
||
287 | $this->detach($test1); |
||
288 | $this->detach($test2); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Test that scores on a Test Suite can be reset. |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | function it_can_reset_it_self() |
||
297 | { |
||
298 | $test1 = MockableTestCase::createWith(5); |
||
299 | $test2 = MockableTestCase::createWith(3); |
||
300 | |||
301 | $this->attach([$test1, $test2]); |
||
302 | |||
303 | $this->run(false); |
||
304 | $this->getScore()->shouldReturn(8); |
||
305 | |||
306 | /** |
||
307 | * Don't know if we need this but lets |
||
308 | * do it. |
||
309 | */ |
||
310 | $this->detach($test1); |
||
311 | $this->detach($test2); |
||
312 | |||
313 | $this->reset(); |
||
314 | $this->getScore()->shouldReturn(0); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Test the default score for a TestSuite is 0. |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | function it_the_default_value_for_scores_is_0() |
||
323 | { |
||
324 | $this->getScore()->shouldBe(0); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Test if beforeTest() exists. |
||
329 | * |
||
330 | * @return void |
||
331 | */ |
||
332 | function it_should_have_function_beforetest() |
||
333 | { |
||
334 | $this->beforeTest(); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Test if afterTest() exists. |
||
339 | * |
||
340 | * @return void |
||
341 | */ |
||
342 | function it_should_have_function_aftertest() |
||
343 | { |
||
344 | $this->afterTest(); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Test if the average function of the TestSuite works |
||
349 | * correctly. |
||
350 | * |
||
351 | * @return false |
||
352 | */ |
||
353 | function it_should_have_a_working_average_function() |
||
354 | { |
||
355 | $test1 = MockableTestCase::createWith(3); |
||
356 | $test2 = MockableTestCase::createWith(3); |
||
357 | $test3 = MockableTestCase::createWith(3); |
||
358 | |||
359 | $this->attach([$test1, $test2, $test3]) |
||
360 | ->run(false); |
||
361 | |||
362 | $this->average()->shouldReturn(3); |
||
363 | } |
||
364 | } |
||
365 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.