Skip to content

Commit ef18d66

Browse files
committed
Test for when exercise does not implement correct interface
1 parent 0a77613 commit ef18d66

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Asset;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
6+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
7+
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
8+
9+
/**
10+
* @author Aydin Hassan <aydin@hotmail.co.uk>
11+
*/
12+
class CliExerciseMissingInterface extends AbstractExercise implements ExerciseInterface
13+
{
14+
15+
/**
16+
* Get the name of the exercise, like `Hello World!`.
17+
*
18+
* @return string
19+
*/
20+
public function getName()
21+
{
22+
return 'CLI exercise missing interface';
23+
}
24+
25+
/**
26+
* A short description of the exercise.
27+
*
28+
* @return string
29+
*/
30+
public function getDescription()
31+
{
32+
return 'CLI exercise missing interface';
33+
}
34+
35+
/**
36+
* Return the type of exercise. This is an ENUM. See `PhpSchool\PhpWorkshop\Exercise\ExerciseType`.
37+
*
38+
* @return ExerciseType
39+
*/
40+
public function getType()
41+
{
42+
return ExerciseType::CLI();
43+
}
44+
}

test/ExerciseRepositoryTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace PhpSchool\PhpWorkshopTest;
44

5-
use InvalidArgumentException;
5+
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
77
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
88
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseInterface;
9+
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseMissingInterface;
910
use PHPUnit_Framework_TestCase;
1011
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1112
use PhpSchool\PhpWorkshop\ExerciseRepository;
@@ -81,4 +82,15 @@ public function testIterator()
8182
$repo = new ExerciseRepository($exercises);
8283
$this->assertEquals($exercises, iterator_to_array($repo));
8384
}
85+
86+
public function testExceptionIsThrownWhenTryingToAddExerciseWhichDoesNotImplementCorrectInterface()
87+
{
88+
$this->expectException(InvalidArgumentException::class);
89+
$message = '"PhpSchool\PhpWorkshopTest\Asset\CliExerciseMissingInterface" is required to implement ';
90+
$message .= '"PhpSchool\PhpWorkshop\Exercise\CliExercise", but it does not';
91+
$this->expectExceptionMessage($message);
92+
93+
$exercise = new CliExerciseMissingInterface;
94+
new ExerciseRepository([$exercise]);
95+
}
8496
}

0 commit comments

Comments
 (0)