Skip to content

Commit 122174c

Browse files
committed
Increase coverage of ExerciseDispatcher
1 parent af9a0ca commit 122174c

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

test/ExerciseDispatcherTest.php

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public function setUp()
5151
touch($this->file);
5252
}
5353

54+
public function testGetEventDispatcher()
55+
{
56+
$eventDispatcher = new EventDispatcher($results = new ResultAggregator);
57+
58+
$exerciseDispatcher = new ExerciseDispatcher(
59+
$this->prophesize(RunnerManager::class)->reveal(),
60+
$results,
61+
$eventDispatcher,
62+
new CheckRepository
63+
);
64+
65+
$this->assertSame($eventDispatcher, $exerciseDispatcher->getEventDispatcher());
66+
}
67+
5468
public function testRequireCheckThrowsExceptionIfCheckDoesNotExist()
5569
{
5670
$this->expectException(InvalidArgumentException::class);
@@ -173,7 +187,7 @@ public function testVerifyThrowsExceptionIfCheckDoesNotSupportExerciseType()
173187
$check = $checkProphecy->reveal();
174188

175189
$runner = $this->prophesize(ExerciseRunnerInterface::class);
176-
$runner->getRequiredChecks()->willReturn([]);
190+
$runner->getRequiredChecks()->willReturn([get_class($check)]);
177191
$runnerManager = $this->prophesize(RunnerManager::class);
178192
$runnerManager->getRunner($exercise)->willReturn($runner->reveal());
179193

@@ -184,8 +198,6 @@ public function testVerifyThrowsExceptionIfCheckDoesNotSupportExerciseType()
184198
new CheckRepository([$check])
185199
);
186200

187-
$exerciseDispatcher->requireCheck(get_class($check));
188-
189201
$msg = 'Check: "Some Check" cannot process exercise: "Some Exercise" with type: "CLI"';
190202
$this->expectException(CheckNotApplicableException::class);
191203
$this->expectExceptionMessage($msg);
@@ -205,7 +217,7 @@ public function testVerifyThrowsExceptionIfExerciseDoesNotImplementCorrectInterf
205217
$check = $checkProphecy->reveal();
206218

207219
$runner = $this->prophesize(ExerciseRunnerInterface::class);
208-
$runner->getRequiredChecks()->willReturn([]);
220+
$runner->getRequiredChecks()->willReturn([get_class($check)]);
209221
$runnerManager = $this->prophesize(RunnerManager::class);
210222
$runnerManager->getRunner($exercise)->willReturn($runner->reveal());
211223

@@ -216,8 +228,6 @@ public function testVerifyThrowsExceptionIfExerciseDoesNotImplementCorrectInterf
216228
new CheckRepository([$check])
217229
);
218230

219-
$exerciseDispatcher->requireCheck(get_class($check));
220-
221231
$msg = 'Exercise: "Some Exercise" should implement interface: "LolIDoNotExist"';
222232
$this->expectException(ExerciseNotConfiguredException::class);
223233
$this->expectExceptionMessage($msg);
@@ -239,7 +249,7 @@ public function testVerify()
239249
$check = $checkProphecy->reveal();
240250

241251
$runner = $this->prophesize(ExerciseRunnerInterface::class);
242-
$runner->getRequiredChecks()->willReturn([]);
252+
$runner->getRequiredChecks()->willReturn([get_class($check)]);
243253
$runner->verify($input)->willReturn(new Success('Success!'));
244254
$runnerManager = $this->prophesize(RunnerManager::class);
245255
$runnerManager->getRunner($exercise)->willReturn($runner->reveal());
@@ -251,8 +261,6 @@ public function testVerify()
251261
new CheckRepository([$check])
252262
);
253263

254-
$exerciseDispatcher->requireCheck(get_class($check));
255-
256264
$result = $exerciseDispatcher->verify($exercise, $input);
257265
$this->assertInstanceOf(ResultAggregator::class, $result);
258266
$this->assertTrue($result->isSuccessful());
@@ -276,7 +284,7 @@ public function testVerifyOnlyRunsRequiredChecks()
276284
$check2 = $checkProphecy2->reveal();
277285

278286
$runner = $this->prophesize(ExerciseRunnerInterface::class);
279-
$runner->getRequiredChecks()->willReturn([]);
287+
$runner->getRequiredChecks()->willReturn([get_class($check1)]);
280288
$runner->verify($input)->willReturn(new Success('Success!'));
281289
$runnerManager = $this->prophesize(RunnerManager::class);
282290
$runnerManager->getRunner($exercise)->willReturn($runner->reveal());
@@ -288,13 +296,51 @@ public function testVerifyOnlyRunsRequiredChecks()
288296
new CheckRepository([$check1, $check2])
289297
);
290298

291-
$exerciseDispatcher->requireCheck(get_class($check1));
299+
$result = $exerciseDispatcher->verify($exercise, $input);
300+
$this->assertInstanceOf(ResultAggregator::class, $result);
301+
$this->assertTrue($result->isSuccessful());
302+
}
303+
304+
public function testVerifyWithBeforeAndAfterRequiredChecks()
305+
{
306+
$input = new Input('app', ['program' => $this->file]);
307+
$exercise = new CliExerciseImpl('Some Exercise');
308+
309+
$checkProphecy1 = $this->prophesize(SimpleCheckInterface::class);
310+
$checkProphecy1->canRun($exercise->getType())->willReturn(true);
311+
$checkProphecy1->getPosition()->willReturn(SimpleCheckInterface::CHECK_BEFORE);
312+
$checkProphecy1->getExerciseInterface()->willReturn(ExerciseInterface::class);
313+
$checkProphecy1->check($exercise, $input)->willReturn(new Success('Success!'));
314+
315+
$checkProphecy2 = $this->prophesize(SimpleCheckInterface::class);
316+
$checkProphecy2->canRun($exercise->getType())->willReturn(true);
317+
$checkProphecy2->getPosition()->willReturn(SimpleCheckInterface::CHECK_AFTER);
318+
$checkProphecy2->getExerciseInterface()->willReturn(ExerciseInterface::class);
319+
$checkProphecy2->check($exercise, $input)->willReturn(new Success('Success!'));
320+
321+
$check1 = $checkProphecy1->reveal();
322+
$check2 = $checkProphecy2->reveal();
323+
324+
$runner = $this->prophesize(ExerciseRunnerInterface::class);
325+
$runner->getRequiredChecks()->willReturn([get_class($check1), get_class($check2)]);
326+
$runner->verify($input)->willReturn(new Success('Success!'));
327+
$runnerManager = $this->prophesize(RunnerManager::class);
328+
$runnerManager->getRunner($exercise)->willReturn($runner->reveal());
329+
330+
$exerciseDispatcher = new ExerciseDispatcher(
331+
$runnerManager->reveal(),
332+
new ResultAggregator,
333+
$this->prophesize(EventDispatcher::class)->reveal(),
334+
new CheckRepository([$check1, $check2])
335+
);
292336

293337
$result = $exerciseDispatcher->verify($exercise, $input);
294338
$this->assertInstanceOf(ResultAggregator::class, $result);
295339
$this->assertTrue($result->isSuccessful());
340+
$this->assertCount(3, $result);
296341
}
297342

343+
298344
public function testWhenBeforeChecksFailTheyReturnImmediately()
299345
{
300346
$input = new Input('app', ['program' => $this->file]);
@@ -316,7 +362,7 @@ public function testWhenBeforeChecksFailTheyReturnImmediately()
316362
$check2 = $checkProphecy2->reveal();
317363

318364
$runner = $this->prophesize(ExerciseRunnerInterface::class);
319-
$runner->getRequiredChecks()->willReturn([]);
365+
$runner->getRequiredChecks()->willReturn([get_class($check1), get_class($check2)]);
320366
$runner->verify($input)->shouldNotBeCalled();
321367
$runnerManager = $this->prophesize(RunnerManager::class);
322368
$runnerManager->getRunner($exercise)->willReturn($runner->reveal());
@@ -328,9 +374,6 @@ public function testWhenBeforeChecksFailTheyReturnImmediately()
328374
new CheckRepository([$check1, $check2])
329375
);
330376

331-
$exerciseDispatcher->requireCheck(get_class($check1));
332-
$exerciseDispatcher->requireCheck(get_class($check2));
333-
334377
$result = $exerciseDispatcher->verify($exercise, $input);
335378
$this->assertInstanceOf(ResultAggregator::class, $result);
336379
$this->assertFalse($result->isSuccessful());
@@ -432,8 +475,6 @@ public function testRun()
432475
$output = $this->prophesize(OutputInterface::class)->reveal();
433476
$exercise = new CliExerciseImpl('Some Exercise');
434477

435-
436-
437478
$runner = $this->prophesize(ExerciseRunnerInterface::class);
438479
$runner->getRequiredChecks()->willReturn([]);
439480
$runner->run($input, $output)->willReturn(true);

0 commit comments

Comments
 (0)