Skip to content

Commit 03b3b68

Browse files
committed
Context
1 parent ac6ac0e commit 03b3b68

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

src/Exercise/MockExercise.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Exercise;
4+
5+
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
6+
7+
class MockExercise extends AbstractExercise implements ExerciseInterface
8+
{
9+
public function getName(): string
10+
{
11+
return 'Mock Exercise';
12+
}
13+
14+
public function getDescription(): string
15+
{
16+
return 'Mock Exercise';
17+
}
18+
19+
public function getType(): ExerciseType
20+
{
21+
return ExerciseType::CUSTOM();
22+
}
23+
24+
public function getProblem(): string
25+
{
26+
return 'problem-file.md';
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ExerciseRunner\Context;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\Input\Input;
7+
use PhpSchool\PhpWorkshop\Utils\Path;
8+
use PhpSchool\PhpWorkshop\Utils\System;
9+
10+
class ExecutionContext
11+
{
12+
public string $studentExecutionDirectory;
13+
public string $referenceExecutionDirectory;
14+
15+
public function __construct(
16+
string $studentWorkingDirectory,
17+
string $referenceWorkingDirectory,
18+
public ExerciseInterface $exercise,
19+
public Input $input,
20+
) {
21+
$this->studentExecutionDirectory = $studentWorkingDirectory;
22+
$this->referenceExecutionDirectory = $referenceWorkingDirectory;
23+
}
24+
25+
public function hasStudentSolution(): bool
26+
{
27+
return $this->input->hasArgument('program');
28+
}
29+
30+
public function getEntryPoint(): string
31+
{
32+
return Path::join(
33+
$this->studentExecutionDirectory,
34+
basename($this->input->getRequiredArgument('program'))
35+
);
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ExerciseRunner\Context;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\Input\Input;
7+
use PhpSchool\PhpWorkshop\Utils\System;
8+
9+
class ExecutionContextFactory
10+
{
11+
public function fromInputAndExercise(Input $input, ExerciseInterface $exercise): ExecutionContext
12+
{
13+
return new ExecutionContext(
14+
dirname($input->getRequiredArgument('program')),
15+
System::randomTempDir(),
16+
$exercise,
17+
$input
18+
);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ExerciseRunner\Context;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\Input\Input;
7+
use PhpSchool\PhpWorkshop\Utils\System;
8+
use Symfony\Component\Filesystem\Filesystem;
9+
10+
class StaticExecutionContextFactory extends ExecutionContextFactory
11+
{
12+
public function __construct(private TestContext $context)
13+
{
14+
}
15+
16+
public function fromInputAndExercise(Input $input, ExerciseInterface $exercise): ExecutionContext
17+
{
18+
return $this->context;
19+
}
20+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\ExerciseRunner\Context;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
6+
use PhpSchool\PhpWorkshop\Exercise\MockExercise;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
9+
use PhpSchool\PhpWorkshop\Utils\System;
10+
use Symfony\Component\Filesystem\Filesystem;
11+
use PhpSchool\PhpWorkshop\Utils\Path;
12+
13+
class TestContext extends ExecutionContext
14+
{
15+
public Filesystem $filesystem;
16+
public ExerciseInterface $exercise;
17+
18+
private function __construct(
19+
ExerciseInterface $exercise = null,
20+
Input $input = null
21+
) {
22+
$this->exercise = $exercise ?? new MockExercise();
23+
24+
$this->filesystem = new Filesystem();
25+
26+
parent::__construct(
27+
System::randomTempDir(),
28+
System::randomTempDir(),
29+
$this->exercise,
30+
$input ? $input : new Input('test', ['program' => 'solution.php']),
31+
);
32+
}
33+
34+
public function importStudentSolution(string $file): void
35+
{
36+
copy($file, Path::join($this->studentExecutionDirectory, 'solution.php'));
37+
}
38+
39+
public function importStudentSolutionFolder(string $folder): void
40+
{
41+
$this->filesystem->mirror($folder, $this->studentExecutionDirectory);
42+
}
43+
44+
public function importReferenceSolution(SolutionInterface $solution): void
45+
{
46+
foreach ($solution->getFiles() as $file) {
47+
$this->filesystem->copy(
48+
$file->getAbsolutePath(),
49+
Path::join($this->referenceExecutionDirectory, $file->getRelativePath())
50+
);
51+
}
52+
}
53+
54+
public static function withDirectories(ExerciseInterface $exercise = null, Input $input = null): self
55+
{
56+
$self = new self($exercise, $input);
57+
58+
$self->filesystem->mkdir($self->studentExecutionDirectory);
59+
$self->filesystem->mkdir($self->referenceExecutionDirectory);
60+
61+
return $self;
62+
}
63+
64+
public static function withoutDirectories(ExerciseInterface $exercise = null, Input $input = null): self
65+
{
66+
return new self($exercise, $input);
67+
}
68+
69+
public function __destruct()
70+
{
71+
$this->filesystem->remove($this->studentExecutionDirectory);
72+
$this->filesystem->remove($this->referenceExecutionDirectory);
73+
}
74+
}

src/Utils/System.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public static function tempDir(string $path = ''): string
2323
{
2424
return Path::join(self::realpath(sys_get_temp_dir()), 'php-school', $path);
2525
}
26+
27+
public static function randomTempDir(): string
28+
{
29+
return Path::join(self::realpath(sys_get_temp_dir()), 'php-school', bin2hex(random_bytes(4)));
30+
}
2631
}

test/Utils/SystemTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ public function testTempDirWithPath(): void
3333
$expect = sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), 'test');
3434
self::assertSame($expect, System::tempDir('test'));
3535
}
36+
37+
public function testRandomTempDir(): void
38+
{
39+
self::assertTrue(str_starts_with(System::randomTempDir(), realpath(sys_get_temp_dir()) . '/php-school'));
40+
}
3641
}

0 commit comments

Comments
 (0)