-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDatabaseCheck.php
More file actions
123 lines (100 loc) · 3.96 KB
/
DatabaseCheck.php
File metadata and controls
123 lines (100 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshop\Check;
use PDO;
use PhpSchool\PhpWorkshop\Event\CgiExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
use PhpSchool\PhpWorkshop\Event\CliExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\ExerciseCheck\DatabaseExerciseCheck;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\Success;
use PhpSchool\PhpWorkshop\Utils\Path;
use PhpSchool\PhpWorkshop\Utils\System;
use Symfony\Component\Filesystem\Filesystem;
/**
* This check sets up a database and a `PDO` object. It prepends the database DSN as a CLI argument to the student's
* solution so they can connect to the database. The `PDO` object is passed to the exercise before and after the
* student's solution has been executed, allowing you to first seed the database and then verify the contents of the
* database.
*/
class DatabaseCheck implements ListenableCheckInterface
{
private Filesystem $filesystem;
private ?string $dbContent = null;
public function __construct(Filesystem $filesystem = null)
{
$this->filesystem = $filesystem ? $filesystem : new Filesystem();
}
/**
* Return the check's name
*/
public function getName(): string
{
return 'Database Verification Check';
}
public function getExerciseInterface(): string
{
return DatabaseExerciseCheck::class;
}
/**
* Here we attach to various events to seed, verify and inject the DSN's
* to the student & reference solution programs' CLI arguments.
*
* @param EventDispatcher $eventDispatcher
*/
public function attach(EventDispatcher $eventDispatcher): void
{
$eventDispatcher->listen(['verify.start', 'run.start'], function (Event $e) {
$path = System::randomTempPath('sqlite');
$this->filesystem->touch($path);
try {
$db = $this->getPDO($path);
/** @var DatabaseExerciseCheck $exercise */
$exercise = $e->getParameter('exercise');
$exercise->seed($db);
$this->dbContent = (string) file_get_contents($path);
} finally {
unset($db);
$this->filesystem->remove($path);
}
});
$eventDispatcher->listen(
['cli.verify.prepare', 'cgi.verify.prepare'],
function (CliExerciseRunnerEvent|CgiExerciseRunnerEvent $e) {
$e->getScenario()->withFile('db.sqlite', (string) $this->dbContent);
$this->dbContent = null;
},
);
$eventDispatcher->listen(
'cli.verify.reference-execute.pre',
fn(CliExecuteEvent $e) => $e->prependArg('sqlite:db.sqlite'),
);
$eventDispatcher->listen(
['cli.verify.student-execute.pre', 'cli.run.student-execute.pre'],
fn(CliExecuteEvent $e) => $e->prependArg('sqlite:db.sqlite'),
);
$eventDispatcher->insertVerifier('verify.finish', function (ExerciseRunnerEvent $e) {
$db = $this->getPDO(Path::join($e->getContext()->getStudentExecutionDirectory(), 'db.sqlite'));
try {
/** @var DatabaseExerciseCheck $exercise */
$exercise = $e->getParameter('exercise');
$verifyResult = $exercise->verify($db);
if (false === $verifyResult) {
return Failure::fromNameAndReason($this->getName(), 'Database verification failed');
}
return new Success('Database Verification Check');
} finally {
unset($db);
}
});
}
private function getPDO(string $path): PDO
{
$db = new PDO('sqlite:' . $path);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
}