Skip to content

Add code exists check #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
use PhpSchool\PhpWorkshop\Utils\RequestRenderer;
use PhpSchool\PhpWorkshop\WorkshopType;
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
use PhpSchool\PhpWorkshop\Check\CodeExistsCheck;
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck;
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
use PhpSchool\PhpWorkshop\Command\CreditsCommand;
Expand Down Expand Up @@ -109,6 +110,7 @@
CheckRepository::class => function (ContainerInterface $c) {
return new CheckRepository([
$c->get(FileExistsCheck::class),
$c->get(CodeExistsCheck::class),
$c->get(PhpLintCheck::class),
$c->get(CodeParseCheck::class),
$c->get(ComposerCheck::class),
Expand Down Expand Up @@ -239,6 +241,9 @@
//checks
FileExistsCheck::class => create(),
PhpLintCheck::class => create(),
CodeExistsCheck::class => function (ContainerInterface $c) {
return new CodeExistsCheck($c->get(Parser::class));
},
CodeParseCheck::class => function (ContainerInterface $c) {
return new CodeParseCheck($c->get(Parser::class));
},
Expand Down
85 changes: 85 additions & 0 deletions src/Check/CodeExistsCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Check;

use PhpParser\Error;
use PhpParser\ErrorHandler;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\NodeFinder;
use PhpParser\Parser;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\ResultInterface;
use PhpSchool\PhpWorkshop\Result\Success;

class CodeExistsCheck implements SimpleCheckInterface
{
/**
* @var Parser
*/
private $parser;

public function __construct(Parser $parser)
{
$this->parser = $parser;
}

public function getName(): string
{
return 'Code Exists Check';
}

/**
* Check solution provided contains code
* Note: We don't care if it's valid code at this point
*/
public function check(ExerciseInterface $exercise, Input $input): ResultInterface
{
$noopHandler = new class implements ErrorHandler {
public function handleError(Error $error): void
{
}
};

$code = (string) file_get_contents($input->getRequiredArgument('program'));
$statements = $this->parser->parse($code, $noopHandler);

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

if (!$empty) {
$openingTag = is_array($statements) && count($statements) === 1 ? $statements[0] : null;
$empty = $openingTag instanceof InlineHTML ? in_array($openingTag->value, ['<?php', '<?']) : false;
}

if ($empty) {
return Failure::fromCheckAndReason($this, 'No code was found');
}

return Success::fromCheck($this);
}

/**
* This check can run on any exercise type.
*/
public function canRun(ExerciseType $exerciseType): bool
{
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI], true);
}

public function getExerciseInterface(): string
{
return ExerciseInterface::class;
}

/**
* This check must run before executing the solution because all solutions require code
*/
public function getPosition(): string
{
return SimpleCheckInterface::CHECK_BEFORE;
}
}
2 changes: 2 additions & 0 deletions src/ExerciseRunner/CgiRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpSchool\PhpWorkshop\ExerciseRunner;

use GuzzleHttp\Psr7\Message;
use PhpSchool\PhpWorkshop\Check\CodeExistsCheck;
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
Expand Down Expand Up @@ -55,6 +56,7 @@ class CgiRunner implements ExerciseRunnerInterface
*/
private static $requiredChecks = [
FileExistsCheck::class,
CodeExistsCheck::class,
PhpLintCheck::class,
CodeParseCheck::class,
];
Expand Down
2 changes: 2 additions & 0 deletions src/ExerciseRunner/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpSchool\PhpWorkshop\ExerciseRunner;

use PhpSchool\PhpWorkshop\Check\CodeExistsCheck;
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
Expand Down Expand Up @@ -50,6 +51,7 @@ class CliRunner implements ExerciseRunnerInterface
*/
private static $requiredChecks = [
FileExistsCheck::class,
CodeExistsCheck::class,
PhpLintCheck::class,
CodeParseCheck::class,
];
Expand Down
81 changes: 81 additions & 0 deletions test/Check/CodeExistsCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace PhpSchool\PhpWorkshopTest\Check;

use PhpParser\ParserFactory;
use PhpSchool\PhpWorkshop\Check\CodeExistsCheck;
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Input\Input;
use PHPUnit\Framework\TestCase;
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\Success;

class CodeExistsCheckTest extends TestCase
{
/**
* @var string
*/
private $testDir;

/**
* @var FileExistsCheck
*/
private $check;

/**
* @var ExerciseInterface
*/
private $exercise;

public function setUp(): void
{
$this->testDir = sprintf(
'%s/%s/%s',
str_replace('\\', '/', sys_get_temp_dir()),
basename(str_replace('\\', '/', get_class($this))),
$this->getName()
);

mkdir($this->testDir, 0777, true);
$this->check = new CodeExistsCheck((new ParserFactory())->create(ParserFactory::PREFER_PHP7));
$this->exercise = $this->createMock(ExerciseInterface::class);
$this->assertEquals('Code Exists Check', $this->check->getName());
$this->assertEquals(ExerciseInterface::class, $this->check->getExerciseInterface());
$this->assertEquals(SimpleCheckInterface::CHECK_BEFORE, $this->check->getPosition());

$this->assertTrue($this->check->canRun(ExerciseType::CGI()));
$this->assertTrue($this->check->canRun(ExerciseType::CLI()));

$this->file = sprintf('%s/submission.php', $this->testDir);
touch($this->file);
}

public function testSuccess(): void
{
file_put_contents($this->file, '<?php echo "Hello World";');

$this->assertInstanceOf(
Success::class,
$this->check->check($this->exercise, new Input('app', ['program' => $this->file]))
);
}

public function testFailure(): void
{
file_put_contents($this->file, '<?php');

$failure = $this->check->check($this->exercise, new Input('app', ['program' => $this->file]));

$this->assertInstanceOf(Failure::class, $failure);
$this->assertEquals('No code was found', $failure->getReason());
}

public function tearDown(): void
{
unlink($this->file);
rmdir($this->testDir);
}
}
2 changes: 2 additions & 0 deletions test/ExerciseRunner/CgiRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Colors\Color;
use GuzzleHttp\Psr7\Request;
use PhpSchool\PhpWorkshop\Check\CodeExistsCheck;
use PhpSchool\Terminal\Terminal;
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function testRequiredChecks(): void
{
$requiredChecks = [
FileExistsCheck::class,
CodeExistsCheck::class,
PhpLintCheck::class,
CodeParseCheck::class,
];
Expand Down
2 changes: 2 additions & 0 deletions test/ExerciseRunner/CliRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpSchool\PhpWorkshopTest\ExerciseRunner;

use Colors\Color;
use PhpSchool\PhpWorkshop\Check\CodeExistsCheck;
use PhpSchool\Terminal\Terminal;
use PhpSchool\PhpWorkshop\Check\CodeParseCheck;
use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function testRequiredChecks(): void
{
$requiredChecks = [
FileExistsCheck::class,
CodeExistsCheck::class,
PhpLintCheck::class,
CodeParseCheck::class,
];
Expand Down