Skip to content

Commit cdeca05

Browse files
committed
Merge pull request #88 from php-school/database-check-run
Enable Database Check to work on Run command
2 parents a270cf8 + 644dc28 commit cdeca05

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

src/Check/DatabaseCheck.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@ public function attach(EventDispatcher $eventDispatcher)
9191
copy($this->userDatabasePath, $this->solutionDatabasePath);
9292
});
9393

94+
$eventDispatcher->listen('run.start', function (Event $e) use ($db) {
95+
$e->getParameter('exercise')->seed($db);
96+
});
97+
9498
$eventDispatcher->listen('cli.verify.solution-execute.pre', function (CliExecuteEvent $e) use ($solutionDsn) {
9599
$e->prependArg($solutionDsn);
96100
});
97101

98-
$eventDispatcher->listen('cli.verify.user-execute.pre', function (CliExecuteEvent $e) use ($userDsn) {
99-
$e->prependArg($userDsn);
100-
});
102+
$eventDispatcher->listen(
103+
['cli.verify.user-execute.pre', 'cli.run.user-execute.pre'],
104+
function (CliExecuteEvent $e) use ($userDsn) {
105+
$e->prependArg($userDsn);
106+
}
107+
);
101108

102109
$eventDispatcher->insertVerifier('verify.finish', function (Event $e) use ($db) {
103110
$verifyResult = $e->getParameter('exercise')->verify($db);
@@ -109,14 +116,18 @@ public function attach(EventDispatcher $eventDispatcher)
109116
return new Success('Database Verification Check');
110117
});
111118

112-
$cleanup = function () use ($db) {
113-
unset($db);
114-
unlink($this->userDatabasePath);
115-
unlink($this->solutionDatabasePath);
116-
rmdir($this->databaseDirectory);
117-
};
118-
119-
$eventDispatcher->listen('cli.verify.solution-execute.fail', $cleanup);
120-
$eventDispatcher->listen('verify.finish', $cleanup);
119+
$eventDispatcher->listen(
120+
[
121+
'cli.verify.solution-execute.fail',
122+
'verify.finish',
123+
'run.finish'
124+
],
125+
function (Event $e) use ($db) {
126+
unset($db);
127+
@unlink($this->userDatabasePath);
128+
@unlink($this->solutionDatabasePath);
129+
rmdir($this->databaseDirectory);
130+
}
131+
);
121132
}
122133
}

test/Check/DatabaseCheckTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
1212
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
1313
use PhpSchool\PhpWorkshop\Factory\RunnerFactory;
14+
use PhpSchool\PhpWorkshop\Output\OutputInterface;
15+
use PhpSchool\PhpWorkshop\Output\StdOutput;
1416
use PhpSchool\PhpWorkshop\ResultAggregator;
1517
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
1618
use PhpSchool\PhpWorkshopTest\Asset\DatabaseExercise;
@@ -110,6 +112,37 @@ public function testSuccessIsReturnedIfDatabaseVerificationPassed()
110112
$this->assertTrue($results->isSuccessful());
111113
}
112114

115+
public function testRunExercise()
116+
{
117+
$this->exercise
118+
->expects($this->once())
119+
->method('getArgs')
120+
->will($this->returnValue([]));
121+
122+
$this->exercise
123+
->expects($this->atLeastOnce())
124+
->method('getType')
125+
->will($this->returnValue(ExerciseType::CLI()));
126+
127+
$this->exercise
128+
->expects($this->once())
129+
->method('configure')
130+
->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
131+
$dispatcher->requireListenableCheck(DatabaseCheck::class);
132+
}));
133+
134+
$results = new ResultAggregator;
135+
$eventDispatcher = new EventDispatcher($results);
136+
$checkRepository = new CheckRepository([$this->check]);
137+
$dispatcher = new ExerciseDispatcher(new RunnerFactory, $results, $eventDispatcher, $checkRepository);
138+
139+
$dispatcher->run(
140+
$this->exercise,
141+
__DIR__ . '/../res/database/user-solution-alter-db.php',
142+
$this->getMock(OutputInterface::class)
143+
);
144+
}
145+
113146
public function testFailureIsReturnedIfDatabaseVerificationFails()
114147
{
115148
$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution.php'));

0 commit comments

Comments
 (0)