Skip to content

Commit 4b9cd34

Browse files
committed
Merge pull request #106 from php-school/simplify-require-check
Resolves #104: Simplify require checks - BC Break
2 parents b581957 + a6d8ccc commit 4b9cd34

14 files changed

+163
-58
lines changed

src/Check/CodeParseCheck.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ public function getExerciseInterface()
7777
{
7878
return ExerciseInterface::class;
7979
}
80+
81+
/**
82+
*
83+
* @return string
84+
*/
85+
public function getPosition()
86+
{
87+
return SimpleCheckInterface::CHECK_BEFORE;
88+
}
8089
}

src/Check/ComposerCheck.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ public function getExerciseInterface()
7979
{
8080
return ComposerExerciseCheck::class;
8181
}
82+
83+
/**
84+
*
85+
* @return string
86+
*/
87+
public function getPosition()
88+
{
89+
return SimpleCheckInterface::CHECK_BEFORE;
90+
}
8291
}

src/Check/FileExistsCheck.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ public function getExerciseInterface()
5454
{
5555
return ExerciseInterface::class;
5656
}
57+
58+
/**
59+
*
60+
* @return string
61+
*/
62+
public function getPosition()
63+
{
64+
return SimpleCheckInterface::CHECK_BEFORE;
65+
}
5766
}

src/Check/FunctionRequirementsCheck.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,13 @@ public function getExerciseInterface()
108108
{
109109
return FunctionRequirementsExerciseCheck::class;
110110
}
111+
112+
/**
113+
*
114+
* @return string
115+
*/
116+
public function getPosition()
117+
{
118+
return SimpleCheckInterface::CHECK_AFTER;
119+
}
111120
}

src/Check/PhpLintCheck.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,13 @@ public function getExerciseInterface()
5858
{
5959
return ExerciseInterface::class;
6060
}
61+
62+
/**
63+
*
64+
* @return string
65+
*/
66+
public function getPosition()
67+
{
68+
return SimpleCheckInterface::CHECK_BEFORE;
69+
}
6170
}

src/Check/SimpleCheckInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
*/
1414
interface SimpleCheckInterface extends CheckInterface
1515
{
16+
/**
17+
* Run this check before exercise verifying
18+
*
19+
* @return string
20+
*/
21+
const CHECK_BEFORE = 'before';
22+
23+
/**
24+
* Run this check after exercise verifying
25+
*
26+
* @return string
27+
*/
28+
const CHECK_AFTER = 'after';
29+
1630
/**
1731
* Can this check run this exercise?
1832
*
@@ -27,4 +41,11 @@ public function canRun(ExerciseType $exerciseType);
2741
* @return ResultInterface
2842
*/
2943
public function check(ExerciseInterface $exercise, $fileName);
44+
45+
/**
46+
* either static::CHECK_BEFORE | static::CHECK_AFTER
47+
*
48+
* @return string
49+
*/
50+
public function getPosition();
3051
}

src/ExerciseDispatcher.php

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpSchool\PhpWorkshop\Check\CheckInterface;
88
use PhpSchool\PhpWorkshop\Check\CheckRepository;
99
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
10+
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
1011
use PhpSchool\PhpWorkshop\Event\Event;
1112
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
1213
use PhpSchool\PhpWorkshop\Exception\CheckNotApplicableException;
@@ -24,9 +25,6 @@
2425
*/
2526
class ExerciseDispatcher
2627
{
27-
const CHECK_BEFORE = 'before';
28-
const CHECK_AFTER = 'after';
29-
3028
/**
3129
* @var CheckInterface[]
3230
*/
@@ -77,42 +75,34 @@ public function __construct(
7775

7876
/**
7977
* @param string $requiredCheck
80-
* @param string $position
8178
* @throws InvalidArgumentException
8279
*/
83-
public function requireCheck($requiredCheck, $position)
80+
public function requireCheck($requiredCheck)
8481
{
8582
if (!$this->checkRepository->has($requiredCheck)) {
8683
throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $requiredCheck));
8784
}
8885

89-
switch ($position) {
90-
case static::CHECK_BEFORE:
91-
$this->checksToRunBefore[] = $this->checkRepository->getByClass($requiredCheck);
92-
break;
93-
case static::CHECK_AFTER:
94-
$this->checksToRunAfter[] = $this->checkRepository->getByClass($requiredCheck);
95-
break;
96-
default:
97-
throw InvalidArgumentException::notValidParameter(
98-
'position',
99-
[static::CHECK_BEFORE, static::CHECK_AFTER],
100-
$position
101-
);
102-
}
103-
}
86+
$check = $this->checkRepository->getByClass($requiredCheck);
10487

105-
/**
106-
* @param string $requiredCheck
107-
* @throws InvalidArgumentException
108-
*/
109-
public function requireListenableCheck($requiredCheck)
110-
{
111-
if (!$this->checkRepository->has($requiredCheck)) {
112-
throw new InvalidArgumentException(sprintf('Check: "%s" does not exist', $requiredCheck));
113-
}
88+
if ($check instanceof SimpleCheckInterface) {
89+
switch ($check->getPosition()) {
90+
case SimpleCheckInterface::CHECK_BEFORE:
91+
$this->checksToRunBefore[] = $check;
92+
break;
93+
case SimpleCheckInterface::CHECK_AFTER:
94+
$this->checksToRunAfter[] = $check;
95+
break;
96+
default:
97+
throw InvalidArgumentException::notValidParameter(
98+
'position',
99+
[SimpleCheckInterface::CHECK_BEFORE, SimpleCheckInterface::CHECK_AFTER],
100+
$check->getPosition()
101+
);
102+
}
114103

115-
$check = $this->checkRepository->getByClass($requiredCheck);
104+
return;
105+
}
116106

117107
if (!$check instanceof ListenableCheckInterface) {
118108
throw new InvalidArgumentException(sprintf('Check: "%s" is not a listenable check', $requiredCheck));

test/Check/CodeParseCheckTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\ParserFactory;
66
use PhpSchool\PhpWorkshop\Check\CheckInterface;
77
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
8+
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
89
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
910
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1011
use PhpSchool\PhpWorkshop\Result\Failure;
@@ -33,6 +34,7 @@ public function setUp()
3334
$this->check = new CodeParseCheck((new ParserFactory)->create(ParserFactory::PREFER_PHP7));
3435
$this->assertEquals('Code Parse Check', $this->check->getName());
3536
$this->assertEquals(ExerciseInterface::class, $this->check->getExerciseInterface());
37+
$this->assertEquals(SimpleCheckInterface::CHECK_BEFORE, $this->check->getPosition());
3638

3739
$this->assertTrue($this->check->canRun(ExerciseType::CGI()));
3840
$this->assertTrue($this->check->canRun(ExerciseType::CLI()));

test/Check/ComposerCheckTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InvalidArgumentException;
66
use PhpSchool\PhpWorkshop\Check\ComposerCheck;
7+
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
78
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
89
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
910
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
@@ -35,6 +36,7 @@ public function setUp()
3536
$this->exercise = new ComposerExercise;
3637
$this->assertEquals('Composer Dependency Check', $this->check->getName());
3738
$this->assertEquals(ComposerExerciseCheck::class, $this->check->getExerciseInterface());
39+
$this->assertEquals(SimpleCheckInterface::CHECK_BEFORE, $this->check->getPosition());
3840

3941
$this->assertTrue($this->check->canRun(ExerciseType::CGI()));
4042
$this->assertTrue($this->check->canRun(ExerciseType::CLI()));

test/Check/DatabaseCheckTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testIfPDOThrowsExceptionItCleansUp()
108108
->expects($this->once())
109109
->method('configure')
110110
->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
111-
$dispatcher->requireListenableCheck(DatabaseCheck::class);
111+
$dispatcher->requireCheck(DatabaseCheck::class);
112112
}));
113113

114114
$this->exercise
@@ -148,7 +148,7 @@ public function testSuccessIsReturnedIfDatabaseVerificationPassed()
148148
->expects($this->once())
149149
->method('configure')
150150
->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
151-
$dispatcher->requireListenableCheck(DatabaseCheck::class);
151+
$dispatcher->requireCheck(DatabaseCheck::class);
152152
}));
153153

154154
$this->exercise
@@ -183,7 +183,7 @@ public function testRunExercise()
183183
->expects($this->once())
184184
->method('configure')
185185
->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
186-
$dispatcher->requireListenableCheck(DatabaseCheck::class);
186+
$dispatcher->requireCheck(DatabaseCheck::class);
187187
}));
188188

189189
$results = new ResultAggregator;
@@ -220,7 +220,7 @@ public function testFailureIsReturnedIfDatabaseVerificationFails()
220220
->expects($this->once())
221221
->method('configure')
222222
->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
223-
$dispatcher->requireListenableCheck(DatabaseCheck::class);
223+
$dispatcher->requireCheck(DatabaseCheck::class);
224224
}));
225225

226226
$this->exercise
@@ -264,7 +264,7 @@ public function testAlteringDatabaseInSolutionDoesNotEffectDatabaseInUserSolutio
264264
->expects($this->once())
265265
->method('configure')
266266
->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
267-
$dispatcher->requireListenableCheck(DatabaseCheck::class);
267+
$dispatcher->requireCheck(DatabaseCheck::class);
268268
}));
269269

270270
$this->exercise

0 commit comments

Comments
 (0)