Skip to content

Commit dc3aaf3

Browse files
committed
Fix tests
1 parent ad4eda1 commit dc3aaf3

File tree

63 files changed

+958
-704
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+958
-704
lines changed

composer.lock

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Check/CodeExistsCheck.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Stmt\InlineHTML;
1010
use PhpParser\NodeFinder;
1111
use PhpParser\Parser;
12+
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
1213
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1314
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1415
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
@@ -17,6 +18,7 @@
1718
use PhpSchool\PhpWorkshop\Result\Failure;
1819
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1920
use PhpSchool\PhpWorkshop\Result\Success;
21+
use PhpSchool\PhpWorkshop\Utils\Path;
2022

2123
class CodeExistsCheck implements SimpleCheckInterface
2224
{
@@ -47,7 +49,7 @@ public function handleError(Error $error): void
4749
}
4850
};
4951

50-
$code = (string) file_get_contents($context->input->getRequiredArgument('program'));
52+
$code = (string) file_get_contents($context->getStudentSolutionFilePath());
5153
$statements = $this->parser->parse($code, $noopHandler);
5254

5355
$empty = null === $statements || empty($statements);

src/Check/CodeParseCheck.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpSchool\PhpWorkshop\Result\Failure;
1515
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1616
use PhpSchool\PhpWorkshop\Result\Success;
17+
use PhpSchool\PhpWorkshop\Utils\Path;
1718

1819
/**
1920
* This check attempts to parse a student's solution and returns
@@ -47,18 +48,17 @@ public function getName(): string
4748
* attempts to parse it with `nikic/php-parser`. If any exceptions are thrown
4849
* by the parser, it is treated as a failure.
4950
*
50-
* @param ExerciseInterface $exercise The exercise to check against.
51-
* @param Input $input The command line arguments passed to the command.
51+
* @param ExecutionContext $context The current execution context.
5252
* @return ResultInterface The result of the check.
5353
*/
5454
public function check(ExecutionContext $context): ResultInterface
5555
{
56-
$code = (string) file_get_contents($context->input->getRequiredArgument('program'));
56+
$code = (string) file_get_contents($context->getStudentSolutionFilePath());
5757

5858
try {
5959
$this->parser->parse($code);
6060
} catch (Error $e) {
61-
return Failure::fromCheckAndCodeParseFailure($this, $e, $context->input->getRequiredArgument('program'));
61+
return Failure::fromCheckAndCodeParseFailure($this, $e, $context->getStudentSolutionFilePath());
6262
}
6363

6464
return Success::fromCheck($this);

src/Check/ComposerCheck.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function getName(): string
3636
* installed a set of required packages. If they did not a failure is returned, otherwise,
3737
* a success is returned.
3838
*
39-
* @param ExerciseInterface $exercise The exercise to check against.
40-
* @param Input $input The command line arguments passed to the command.
39+
* @param ExecutionContext $context The current execution context.
4140
* @return ResultInterface The result of the check.
4241
* @noinspection SpellCheckingInspection
4342
*/
@@ -47,19 +46,19 @@ public function check(ExecutionContext $context): ResultInterface
4746
throw new InvalidArgumentException();
4847
}
4948

50-
if (!file_exists(sprintf('%s/composer.json', dirname($context->input->getRequiredArgument('program'))))) {
49+
if (!file_exists(sprintf('%s/composer.json', $context->studentEnvironment->workingDirectory))) {
5150
return ComposerFailure::fromCheckAndMissingFileOrFolder($this, 'composer.json');
5251
}
5352

54-
if (!file_exists(sprintf('%s/composer.lock', dirname($context->input->getRequiredArgument('program'))))) {
53+
if (!file_exists(sprintf('%s/composer.lock', $context->studentEnvironment->workingDirectory))) {
5554
return ComposerFailure::fromCheckAndMissingFileOrFolder($this, 'composer.lock');
5655
}
5756

58-
if (!file_exists(sprintf('%s/vendor', dirname($context->input->getRequiredArgument('program'))))) {
57+
if (!file_exists(sprintf('%s/vendor', $context->studentEnvironment->workingDirectory))) {
5958
return ComposerFailure::fromCheckAndMissingFileOrFolder($this, 'vendor');
6059
}
6160

62-
$lockFile = new LockFileParser(sprintf('%s/composer.lock', dirname($context->input->getRequiredArgument('program'))));
61+
$lockFile = new LockFileParser(sprintf('%s/composer.lock', $context->studentEnvironment->workingDirectory));
6362
$missingPackages = array_filter($context->exercise->getRequiredPackages(), function ($package) use ($lockFile) {
6463
return !$lockFile->hasInstalledPackage($package);
6564
});

src/Check/FileComparisonCheck.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function getName(): string
3636
/**
3737
* Simply check that the file exists.
3838
*
39-
* @param ExerciseInterface&ProvidesSolution $exercise The exercise to check against.
40-
* @param Input $input The command line arguments passed to the command.
39+
* @param ExecutionContext $context The current execution context.
4140
* @return ResultInterface The result of the check.
4241
*/
4342
public function check(ExecutionContext $context): ResultInterface
@@ -49,7 +48,7 @@ public function check(ExecutionContext $context): ResultInterface
4948
foreach ($context->exercise->getFilesToCompare() as $key => $file) {
5049
[$options, $file] = $this->getOptionsAndFile($key, $file);
5150

52-
$studentFile = Path::join(dirname($context->input->getRequiredArgument('program')), $file);
51+
$studentFile = Path::join($context->studentEnvironment->workingDirectory, $file);
5352
$referenceFile = Path::join($context->referenceEnvironment->workingDirectory, $file);
5453

5554
if (!file_exists($referenceFile)) {

src/Check/FileExistsCheck.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpSchool\PhpWorkshop\Result\Failure;
1313
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1414
use PhpSchool\PhpWorkshop\Result\Success;
15+
use PhpSchool\PhpWorkshop\Utils\Path;
1516

1617
/**
1718
* This check verifies that the student's solution file actually exists.
@@ -29,19 +30,22 @@ public function getName(): string
2930
/**
3031
* Simply check that the file exists.
3132
*
32-
* @param ExerciseInterface $exercise The exercise to check against.
33-
* @param Input $input The command line arguments passed to the command.
33+
* @param ExecutionContext $context The current execution context.
3434
* @return ResultInterface The result of the check.
3535
*/
3636
public function check(ExecutionContext $context): ResultInterface
3737
{
38-
if (file_exists($context->input->getRequiredArgument('program'))) {
38+
if (!$context->hasStudentSolution()) {
39+
return Success::fromCheck($this);
40+
}
41+
42+
if (file_exists($context->getStudentSolutionFilePath())) {
3943
return Success::fromCheck($this);
4044
}
4145

4246
return Failure::fromCheckAndReason(
4347
$this,
44-
sprintf('File: "%s" does not exist', $context->input->getRequiredArgument('program'))
48+
sprintf('File: "%s" does not exist', $context->getStudentSolutionFilePath())
4549
);
4650
}
4751

src/Check/FunctionRequirementsCheck.php

+5-11
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,16 @@
2020
use PhpSchool\PhpWorkshop\Result\FunctionRequirementsFailure;
2121
use PhpSchool\PhpWorkshop\Result\ResultInterface;
2222
use PhpSchool\PhpWorkshop\Result\Success;
23+
use PhpSchool\PhpWorkshop\Utils\Path;
2324

2425
/**
2526
* This check verifies that the student's solution contains usages of some required functions
2627
* and also does not use certain functions (specified by the exercise).
2728
*/
2829
class FunctionRequirementsCheck implements SimpleCheckInterface
2930
{
30-
/**
31-
* @var Parser
32-
*/
33-
private $parser;
31+
private Parser $parser;
3432

35-
/**
36-
* @param Parser $parser
37-
*/
3833
public function __construct(Parser $parser)
3934
{
4035
$this->parser = $parser;
@@ -53,8 +48,7 @@ public function getName(): string
5348
* required functions and that banned functions are not used. The requirements
5449
* are pulled from the exercise.
5550
*
56-
* @param ExerciseInterface $exercise The exercise to check against.
57-
* @param Input $input The command line arguments passed to the command.
51+
* @param ExecutionContext $context The current execution context.
5852
* @return ResultInterface The result of the check.
5953
*/
6054
public function check(ExecutionContext $context): ResultInterface
@@ -66,12 +60,12 @@ public function check(ExecutionContext $context): ResultInterface
6660
$requiredFunctions = $context->exercise->getRequiredFunctions();
6761
$bannedFunctions = $context->exercise->getBannedFunctions();
6862

69-
$code = (string) file_get_contents($context->input->getRequiredArgument('program'));
63+
$code = (string) file_get_contents($context->getStudentSolutionFilePath());
7064

7165
try {
7266
$ast = $this->parser->parse($code) ?? [];
7367
} catch (Error $e) {
74-
return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getRequiredArgument('program'));
68+
return Failure::fromCheckAndCodeParseFailure($this, $e, $context->getStudentSolutionFilePath());
7569
}
7670

7771
$visitor = new FunctionVisitor($requiredFunctions, $bannedFunctions);

src/Check/PhpLintCheck.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ public function getName(): string
3232
/**
3333
* Simply check the student's solution can be linted with `php -l`.
3434
*
35-
* @param ExerciseInterface $exercise The exercise to check against.
36-
* @param Input $input The command line arguments passed to the command.
35+
* @param ExecutionContext $context The current execution context.
3736
* @return ResultInterface The result of the check.
3837
*/
3938
public function check(ExecutionContext $context): ResultInterface
4039
{
4140
$finder = new ExecutableFinder();
42-
$process = new Process([$finder->find('php'), '-l', $context->input->getArgument('program')]);
41+
$process = new Process([$finder->find('php'), '-l', $context->getStudentSolutionFilePath()]);
4342
$process->run();
4443

4544
if ($process->isSuccessful()) {

src/Event/Event.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Event implements EventInterface
2828
* @param string $name The event name.
2929
* @param array<mixed> $parameters The event parameters.
3030
*/
31-
public function __construct(string $name, public RunnerContext $context, array $parameters = [])
31+
public function __construct(string $name, array $parameters = [])
3232
{
3333
$this->name = $name;
3434
$this->parameters = $parameters;

src/Event/ExerciseRunnerEvent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(string $name, public RunnerContext $context, array $
2222
{
2323
$parameters['input'] = $context->getExecutionContext()->input;
2424
$parameters['exercise'] = $context->getExecutionContext()->exercise;
25-
parent::__construct($name, $context, $parameters);
25+
parent::__construct($name, $parameters);
2626
}
2727

2828
/**

src/Exercise/AbstractExercise.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
1515
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
1616
use ReflectionClass;
17+
1718
use function Sodium\randombytes_uniform;
1819

1920
/**
@@ -93,9 +94,4 @@ public static function normaliseName(string $name): string
9394
public function configure(ExerciseDispatcher $dispatcher, RunnerContext $context): void
9495
{
9596
}
96-
97-
public function getArgs(): array
98-
{
99-
return [];
100-
}
10197
}

src/Exercise/CliExercise.php

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace PhpSchool\PhpWorkshop\Exercise;
66

7-
use PhpSchool\PhpWorkshop\ExerciseRunner\CliExecutionContext;
8-
97
/**
108
* This interface describes the additional methods a CLI type exercise should implement.
119
*/

src/ExerciseCheck/SelfCheck.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ interface SelfCheck
2424
* The method is passed the absolute file path to the student's solution and should return a result
2525
* object which indicates the success or not of the check.
2626
*
27-
* @param Input $input The command line arguments passed to the command.
27+
* @param ExecutionContext $context The current execution context.
2828
* @return ResultInterface The result of the check.
2929
*/
30-
public function check(ExecutionContext $environment): ResultInterface;
30+
public function check(ExecutionContext $context): ResultInterface;
3131
}

0 commit comments

Comments
 (0)