Skip to content

Commit 9e6d113

Browse files
committed
Update check signature and fix tests
1 parent d0587ce commit 9e6d113

11 files changed

+81
-46
lines changed

src/Check/CodeParseCheck.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Parser;
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11+
use PhpSchool\PhpWorkshop\Input\Input;
1112
use PhpSchool\PhpWorkshop\Result\Failure;
1213
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1314
use PhpSchool\PhpWorkshop\Result\Success;
@@ -51,18 +52,18 @@ public function getName()
5152
* by the parser, it is treated as a failure.
5253
*
5354
* @param ExerciseInterface $exercise The exercise to check against.
54-
* @param string $fileName The absolute path to the student's solution.
55+
* @param Input $input The command line arguments passed to the command.
5556
* @return ResultInterface The result of the check.
5657
*/
57-
public function check(ExerciseInterface $exercise, $fileName)
58+
public function check(ExerciseInterface $exercise, Input $input)
5859
{
5960

60-
$code = file_get_contents($fileName);
61+
$code = file_get_contents($input->getArgument('program'));
6162

6263
try {
6364
$this->parser->parse($code);
6465
} catch (Error $e) {
65-
return Failure::fromCheckAndCodeParseFailure($this, $e, $fileName);
66+
return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getArgument('program'));
6667
}
6768

6869
return Success::fromCheck($this);
@@ -76,7 +77,7 @@ public function check(ExerciseInterface $exercise, $fileName)
7677
*/
7778
public function canRun(ExerciseType $exerciseType)
7879
{
79-
return true;
80+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
8081
}
8182

8283
/**

src/Check/ComposerCheck.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
77
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
88
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
9+
use PhpSchool\PhpWorkshop\Input\Input;
910
use PhpSchool\PhpWorkshop\Result\Failure;
1011
use PhpSchool\PhpWorkshop\Result\ResultInterface;
1112
use PhpSchool\PhpWorkshop\Result\Success;
@@ -35,24 +36,24 @@ public function getName()
3536
* a success is returned.
3637
*
3738
* @param ExerciseInterface $exercise The exercise to check against.
38-
* @param string $fileName The absolute path to the student's solution.
39+
* @param Input $input The command line arguments passed to the command.
3940
* @return ResultInterface The result of the check.
4041
*/
41-
public function check(ExerciseInterface $exercise, $fileName)
42+
public function check(ExerciseInterface $exercise, Input $input)
4243
{
4344
if (!$exercise instanceof ComposerExerciseCheck) {
4445
throw new \InvalidArgumentException;
4546
}
4647

47-
if (!file_exists(sprintf('%s/composer.json', dirname($fileName)))) {
48+
if (!file_exists(sprintf('%s/composer.json', dirname($input->getArgument('program'))))) {
4849
return new Failure($this->getName(), 'No composer.json file found');
4950
}
5051

51-
if (!file_exists(sprintf('%s/composer.lock', dirname($fileName)))) {
52+
if (!file_exists(sprintf('%s/composer.lock', dirname($input->getArgument('program'))))) {
5253
return new Failure($this->getName(), 'No composer.lock file found');
5354
}
5455

55-
$lockFile = new LockFileParser(sprintf('%s/composer.lock', dirname($fileName)));
56+
$lockFile = new LockFileParser(sprintf('%s/composer.lock', dirname($input->getArgument('program'))));
5657
$missingPackages = array_filter($exercise->getRequiredPackages(), function ($package) use ($lockFile) {
5758
return !$lockFile->hasInstalledPackage($package);
5859
});
@@ -78,7 +79,7 @@ public function check(ExerciseInterface $exercise, $fileName)
7879
*/
7980
public function canRun(ExerciseType $exerciseType)
8081
{
81-
return true;
82+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
8283
}
8384

8485
/**

src/Check/FileExistsCheck.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Result\Failure;
89
use PhpSchool\PhpWorkshop\Result\ResultInterface;
910
use PhpSchool\PhpWorkshop\Result\Success;
@@ -30,16 +31,16 @@ public function getName()
3031
* Simply check that the file exists.
3132
*
3233
* @param ExerciseInterface $exercise The exercise to check against.
33-
* @param string $fileName The absolute path to the student's solution.
34+
* @param Input $input The command line arguments passed to the command.
3435
* @return ResultInterface The result of the check.
3536
*/
36-
public function check(ExerciseInterface $exercise, $fileName)
37+
public function check(ExerciseInterface $exercise, Input $input)
3738
{
38-
if (file_exists($fileName)) {
39+
if (file_exists($input->getArgument('program'))) {
3940
return Success::fromCheck($this);
4041
}
4142

42-
return Failure::fromCheckAndReason($this, sprintf('File: "%s" does not exist', $fileName));
43+
return Failure::fromCheckAndReason($this, sprintf('File: "%s" does not exist', $input->getArgument('program')));
4344
}
4445

4546
/**
@@ -50,7 +51,7 @@ public function check(ExerciseInterface $exercise, $fileName)
5051
*/
5152
public function canRun(ExerciseType $exerciseType)
5253
{
53-
return true;
54+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
5455
}
5556

5657
/**

src/Check/FunctionRequirementsCheck.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1111
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck;
12+
use PhpSchool\PhpWorkshop\Input\Input;
1213
use PhpSchool\PhpWorkshop\NodeVisitor\FunctionVisitor;
1314
use PhpSchool\PhpWorkshop\Result\Failure;
1415
use PhpSchool\PhpWorkshop\Result\FunctionRequirementsFailure;
@@ -54,10 +55,10 @@ public function getName()
5455
* are pulled from the exercise.
5556
*
5657
* @param ExerciseInterface $exercise The exercise to check against.
57-
* @param string $fileName The absolute path to the student's solution.
58+
* @param Input $input The command line arguments passed to the command.
5859
* @return ResultInterface The result of the check.
5960
*/
60-
public function check(ExerciseInterface $exercise, $fileName)
61+
public function check(ExerciseInterface $exercise, Input $input)
6162
{
6263
if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
6364
throw new \InvalidArgumentException;
@@ -66,12 +67,12 @@ public function check(ExerciseInterface $exercise, $fileName)
6667
$requiredFunctions = $exercise->getRequiredFunctions();
6768
$bannedFunctions = $exercise->getBannedFunctions();
6869

69-
$code = file_get_contents($fileName);
70+
$code = file_get_contents($input->getArgument('program'));
7071

7172
try {
7273
$ast = $this->parser->parse($code);
7374
} catch (Error $e) {
74-
return Failure::fromCheckAndCodeParseFailure($this, $e, $fileName);
75+
return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getArgument('program'));
7576
}
7677

7778
$visitor = new FunctionVisitor($requiredFunctions, $bannedFunctions);
@@ -107,7 +108,7 @@ public function check(ExerciseInterface $exercise, $fileName)
107108
*/
108109
public function canRun(ExerciseType $exerciseType)
109110
{
110-
return true;
111+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
111112
}
112113

113114
/**

src/Check/PhpLintCheck.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
7+
use PhpSchool\PhpWorkshop\Input\Input;
8+
use PhpSchool\PhpWorkshop\Result\ResultInterface;
79
use PhpSchool\PhpWorkshop\Result\Success;
810
use PhpSchool\PhpWorkshop\Result\Failure;
911
use Symfony\Component\Process\Process;
@@ -32,12 +34,12 @@ public function getName()
3234
* Simply check the student's solution can be linted with `php -l`.
3335
*
3436
* @param ExerciseInterface $exercise The exercise to check against.
35-
* @param string $fileName The absolute path to the student's solution.
37+
* @param Input $input The command line arguments passed to the command.
3638
* @return ResultInterface The result of the check.
3739
*/
38-
public function check(ExerciseInterface $exercise, $fileName)
40+
public function check(ExerciseInterface $exercise, Input $input)
3941
{
40-
$process = new Process(sprintf('%s -l %s', PHP_BINARY, $fileName));
42+
$process = new Process(sprintf('%s -l %s', PHP_BINARY, $input->getArgument('program')));
4143
$process->run();
4244

4345
if ($process->isSuccessful()) {
@@ -55,7 +57,7 @@ public function check(ExerciseInterface $exercise, $fileName)
5557
*/
5658
public function canRun(ExerciseType $exerciseType)
5759
{
58-
return true;
60+
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI]);
5961
}
6062

6163
/**

src/Check/SimpleCheckInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PhpSchool\PhpWorkshop\Result\ResultInterface;
89

910
/**
@@ -47,10 +48,10 @@ public function canRun(ExerciseType $exerciseType);
4748
* should be returned.
4849
*
4950
* @param ExerciseInterface $exercise The exercise to check against.
50-
* @param string $fileName The absolute path to the student's solution.
51+
* @param Input $input The command line arguments passed to the command.
5152
* @return ResultInterface The result of the check.
5253
*/
53-
public function check(ExerciseInterface $exercise, $fileName);
54+
public function check(ExerciseInterface $exercise, Input $input);
5455

5556
/**
5657
* Either `static::CHECK_BEFORE` | `static::CHECK_AFTER`.

test/Check/CodeParseCheckTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
1010
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11+
use PhpSchool\PhpWorkshop\Input\Input;
1112
use PhpSchool\PhpWorkshop\Result\Failure;
1213
use PhpSchool\PhpWorkshop\Result\Success;
1314
use PHPUnit_Framework_TestCase;
@@ -20,7 +21,7 @@
2021
class CodeParseCheckTest extends PHPUnit_Framework_TestCase
2122
{
2223
/**
23-
* @var CheckInterface
24+
* @var SimpleCheckInterface
2425
*/
2526
private $check;
2627

@@ -48,7 +49,10 @@ public function testUnParseableCodeReturnsFailure()
4849
{
4950
file_put_contents($this->file, '<?php $lol');
5051

51-
$result = $this->check->check($this->createMock(ExerciseInterface::class), $this->file);
52+
$result = $this->check->check(
53+
$this->createMock(ExerciseInterface::class),
54+
new Input('app', ['program' => $this->file])
55+
);
5256
$this->assertInstanceOf(Failure::class, $result);
5357

5458
$this->assertEquals('Code Parse Check', $result->getCheckName());
@@ -62,7 +66,10 @@ public function testParseableCodeReturnsSuccess()
6266
{
6367
file_put_contents($this->file, '<?php $lol = "lol";');
6468

65-
$result = $this->check->check($this->createMock(ExerciseInterface::class), $this->file);
69+
$result = $this->check->check(
70+
$this->createMock(ExerciseInterface::class),
71+
new Input('app', ['program' => $this->file])
72+
);
6673
$this->assertInstanceOf(Success::class, $result);
6774

6875
$this->assertEquals('Code Parse Check', $result->getCheckName());

test/Check/ComposerCheckTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
99
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
1010
use PhpSchool\PhpWorkshop\ExerciseCheck\ComposerExerciseCheck;
11+
use PhpSchool\PhpWorkshop\Input\Input;
1112
use PhpSchool\PhpWorkshop\Result\Failure;
1213
use PhpSchool\PhpWorkshop\Result\Success;
1314
use PhpSchool\PhpWorkshopTest\Asset\ComposerExercise;
@@ -47,12 +48,15 @@ public function testExceptionIsThrownIfNotValidExercise()
4748
$exercise = $this->createMock(ExerciseInterface::class);
4849
$this->expectException(InvalidArgumentException::class);
4950

50-
$this->check->check($exercise, '');
51+
$this->check->check($exercise, new Input('app'));
5152
}
5253

5354
public function testCheckReturnsFailureIfNoComposerFile()
5455
{
55-
$result = $this->check->check($this->exercise, 'invalid/solution');
56+
$result = $this->check->check(
57+
$this->exercise,
58+
new Input('app', ['program' => 'invalid/solution'])
59+
);
5660

5761
$this->assertInstanceOf(Failure::class, $result);
5862
$this->assertSame('Composer Dependency Check', $result->getCheckName());
@@ -61,7 +65,10 @@ public function testCheckReturnsFailureIfNoComposerFile()
6165

6266
public function testCheckReturnsFailureIfNoComposerLockFile()
6367
{
64-
$result = $this->check->check($this->exercise, __DIR__ . '/../res/composer/not-locked/solution.php');
68+
$result = $this->check->check(
69+
$this->exercise,
70+
new Input('app', ['program' => __DIR__ . '/../res/composer/not-locked/solution.php'])
71+
);
6572

6673
$this->assertInstanceOf(Failure::class, $result);
6774
$this->assertSame('Composer Dependency Check', $result->getCheckName());
@@ -80,8 +87,8 @@ public function testCheckReturnsFailureIfDependencyNotRequired($dependency, $sol
8087
$exercise->expects($this->once())
8188
->method('getRequiredPackages')
8289
->will($this->returnValue([$dependency]));
83-
84-
$result = $this->check->check($exercise, $solutionFile);
90+
91+
$result = $this->check->check($exercise, new Input('app', ['program' => $solutionFile]));
8592

8693
$this->assertInstanceOf(Failure::class, $result);
8794
$this->assertSame('Composer Dependency Check', $result->getCheckName());
@@ -104,7 +111,10 @@ public function dependencyProvider()
104111

105112
public function testCheckReturnsSuccessIfCorrectLockFile()
106113
{
107-
$result = $this->check->check($this->exercise, __DIR__ . '/../res/composer/good-solution/solution.php');
114+
$result = $this->check->check(
115+
$this->exercise,
116+
new Input('app', ['program' => __DIR__ . '/../res/composer/good-solution/solution.php'])
117+
);
108118

109119
$this->assertInstanceOf(Success::class, $result);
110120
$this->assertSame('Composer Dependency Check', $result->getCheckName());

test/Check/FileExistsCheckTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
66
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
7+
use PhpSchool\PhpWorkshop\Input\Input;
78
use PHPUnit_Framework_TestCase;
89
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
910
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
@@ -51,14 +52,17 @@ public function testSuccess()
5152
$file = sprintf('%s/test.txt', $this->testDir);
5253
touch($file);
5354

54-
$this->assertInstanceOf(Success::class, $this->check->check($this->exercise, $file));
55+
$this->assertInstanceOf(
56+
Success::class,
57+
$this->check->check($this->exercise, new Input('app', ['program' => $file]))
58+
);
5559
unlink($file);
5660
}
5761

5862
public function testFailure()
5963
{
6064
$file = sprintf('%s/test.txt', $this->testDir);
61-
$failure = $this->check->check($this->exercise, $file);
65+
$failure = $this->check->check($this->exercise, new Input('app', ['program' => $file]));
6266
$this->assertInstanceOf(Failure::class, $failure);
6367
$this->assertEquals(sprintf('File: "%s" does not exist', $file), $failure->getReason());
6468
}

0 commit comments

Comments
 (0)