-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestContext.php
More file actions
102 lines (85 loc) · 3.37 KB
/
TestContext.php
File metadata and controls
102 lines (85 loc) · 3.37 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
<?php
namespace PhpSchool\PhpWorkshop\ExerciseRunner\Context;
use PhpSchool\PhpWorkshop\Exception\RuntimeException;
use PhpSchool\PhpWorkshop\Exercise\CgiExercise;
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\MockExercise;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
use PhpSchool\PhpWorkshop\Utils\System;
use Symfony\Component\Filesystem\Filesystem;
use PhpSchool\PhpWorkshop\Utils\Path;
class TestContext extends ExecutionContext
{
private Filesystem $filesystem;
private ExerciseInterface $exercise;
private bool $studentSolutionDirWasCreated = false;
private bool $referenceSolutionDirWasCreated = false;
public function __construct(
ExerciseInterface $exercise = null,
Input $input = null,
string $studentDirectory = null,
) {
$this->exercise = $exercise ?? new MockExercise();
$this->filesystem = new Filesystem();
if ($studentDirectory === null) {
$studentDirectory = System::randomTempDir();
}
parent::__construct(
$studentDirectory,
System::randomTempDir(),
$this->exercise,
$input ? $input : new Input('test', ['program' => 'solution.php']),
);
}
public function createStudentSolutionDirectory(): void
{
$this->filesystem->mkdir($this->getStudentExecutionDirectory());
$this->studentSolutionDirWasCreated = true;
}
public function createReferenceSolutionDirectory(): void
{
$this->filesystem->mkdir($this->getReferenceExecutionDirectory());
$this->referenceSolutionDirWasCreated = true;
}
public function importStudentFileFromString(string $content, string $filename = 'solution.php'): void
{
if (!$this->studentSolutionDirWasCreated) {
throw new RuntimeException(
sprintf('Student execution directory not created. Call %s::createStudentSolutionDirectory() first.', self::class)
);
}
file_put_contents(Path::join($this->getStudentExecutionDirectory(), $filename), $content);
}
public function importReferenceFileFromString(string $content, string $filename = 'solution.php'): void
{
if (!$this->referenceSolutionDirWasCreated) {
throw new RuntimeException(
sprintf('Reference execution directory not created. Call %s::createReferenceSolutionDirectory() first.', self::class)
);
}
file_put_contents(Path::join($this->getReferenceExecutionDirectory(), $filename), $content);
}
public static function fromExerciseAndStudentSolution(ExerciseInterface $exercise, string $file): self
{
if (file_exists($file)) {
$file = (string) realpath($file);
}
$input = new Input('test', ['program' => $file]);
return new self(
exercise: $exercise,
input: $input,
studentDirectory: dirname($file)
);
}
public function __destruct()
{
if ($this->studentSolutionDirWasCreated) {
$this->filesystem->remove($this->getStudentExecutionDirectory());
}
if ($this->referenceSolutionDirWasCreated) {
$this->filesystem->remove($this->getReferenceExecutionDirectory());
}
}
}