Skip to content

Enable Database Check to work on Run command #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/Check/DatabaseCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,20 @@ public function attach(EventDispatcher $eventDispatcher)
copy($this->userDatabasePath, $this->solutionDatabasePath);
});

$eventDispatcher->listen('run.start', function (Event $e) use ($db) {
$e->getParameter('exercise')->seed($db);
});

$eventDispatcher->listen('cli.verify.solution-execute.pre', function (CliExecuteEvent $e) use ($solutionDsn) {
$e->prependArg($solutionDsn);
});

$eventDispatcher->listen('cli.verify.user-execute.pre', function (CliExecuteEvent $e) use ($userDsn) {
$e->prependArg($userDsn);
});
$eventDispatcher->listen(
['cli.verify.user-execute.pre', 'cli.run.user-execute.pre'],
function (CliExecuteEvent $e) use ($userDsn) {
$e->prependArg($userDsn);
}
);

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

$cleanup = function () use ($db) {
unset($db);
unlink($this->userDatabasePath);
unlink($this->solutionDatabasePath);
rmdir($this->databaseDirectory);
};

$eventDispatcher->listen('cli.verify.solution-execute.fail', $cleanup);
$eventDispatcher->listen('verify.finish', $cleanup);
$eventDispatcher->listen(
[
'cli.verify.solution-execute.fail',
'verify.finish',
'run.finish'
],
function (Event $e) use ($db) {
unset($db);
@unlink($this->userDatabasePath);
@unlink($this->solutionDatabasePath);
rmdir($this->databaseDirectory);
}
);
}
}
33 changes: 33 additions & 0 deletions test/Check/DatabaseCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\Factory\RunnerFactory;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Output\StdOutput;
use PhpSchool\PhpWorkshop\ResultAggregator;
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
use PhpSchool\PhpWorkshopTest\Asset\DatabaseExercise;
Expand Down Expand Up @@ -110,6 +112,37 @@ public function testSuccessIsReturnedIfDatabaseVerificationPassed()
$this->assertTrue($results->isSuccessful());
}

public function testRunExercise()
{
$this->exercise
->expects($this->once())
->method('getArgs')
->will($this->returnValue([]));

$this->exercise
->expects($this->atLeastOnce())
->method('getType')
->will($this->returnValue(ExerciseType::CLI()));

$this->exercise
->expects($this->once())
->method('configure')
->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
$dispatcher->requireListenableCheck(DatabaseCheck::class);
}));

$results = new ResultAggregator;
$eventDispatcher = new EventDispatcher($results);
$checkRepository = new CheckRepository([$this->check]);
$dispatcher = new ExerciseDispatcher(new RunnerFactory, $results, $eventDispatcher, $checkRepository);

$dispatcher->run(
$this->exercise,
__DIR__ . '/../res/database/user-solution-alter-db.php',
$this->getMock(OutputInterface::class)
);
}

public function testFailureIsReturnedIfDatabaseVerificationFails()
{
$solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution.php'));
Expand Down