Skip to content

Log code parse errors from patches #222

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 1 commit into from
Jun 12, 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
4 changes: 2 additions & 2 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@
return $parserFactory->create(ParserFactory::PREFER_PHP7);
},
CodePatcher::class => function (ContainerInterface $c) {
$patch = (new Patch)
$patch = (new Patch())
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'ini_set("display_errors", "1");'))
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'error_reporting(E_ALL);'))
->withInsertion(new Insertion(Insertion ::TYPE_BEFORE, 'date_default_timezone_set("Europe/London");'));

return new CodePatcher($c->get(Parser::class), new Standard, $patch);
return new CodePatcher($c->get(Parser::class), new Standard(), $c->get(LoggerInterface::class), $patch);
},
FakerGenerator::class => function () {
return FakerFactory::create();
Expand Down
15 changes: 13 additions & 2 deletions src/CodePatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\SubmissionPatchable;
use PhpSchool\PhpWorkshop\Patch\Transformer;
use Psr\Log\LoggerInterface;

/**
* Service to apply patches to a student's solution. Accepts a default patch via the constructor.
Expand All @@ -30,6 +31,11 @@ class CodePatcher
*/
private $printer;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var Patch|null
*/
Expand All @@ -45,12 +51,14 @@ class CodePatcher
*
* @param Parser $parser
* @param Standard $printer
* @param LoggerInterface $logger
* @param Patch|null $defaultPatch
*/
public function __construct(Parser $parser, Standard $printer, Patch $defaultPatch = null)
public function __construct(Parser $parser, Standard $printer, LoggerInterface $logger, Patch $defaultPatch = null)
{
$this->parser = $parser;
$this->printer = $printer;
$this->logger = $logger;
$this->defaultPatch = $defaultPatch;
}

Expand Down Expand Up @@ -141,7 +149,10 @@ private function applyCodeInsertion(CodeInsertion $codeInsertion, array $stateme
$codeToInsert = sprintf('<?php %s', preg_replace('/^\s*<\?php/', '', $codeToInsert));
$additionalStatements = $this->parser->parse($codeToInsert) ?? [];
} catch (Error $e) {
//we should probably log this and have a dev mode or something
$this->logger->critical(
'Code Insertion could not be parsed: ' . $e->getMessage(),
['code' => $codeInsertion->getCode()]
);
return $statements;
}

Expand Down
77 changes: 69 additions & 8 deletions test/CodePatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use PhpSchool\PhpWorkshopTest\Asset\PatchableExercise;
use PHPUnit\Framework\TestCase;
use PhpSchool\PhpWorkshop\CodeInsertion as Insertion;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class CodePatcherTest extends TestCase
{
Expand All @@ -25,7 +27,12 @@ public function testDefaultPatchIsAppliedIfAvailable(): void
$patch = (new Patch())
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'ini_set("display_errors", 1);'));

$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard(), $patch);
$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
new NullLogger(),
$patch
);
$exercise = $this->createMock(ExerciseInterface::class);

$expected = "<?php\n\nini_set(\"display_errors\", 1);\n\$original = true;";
Expand All @@ -34,7 +41,11 @@ public function testDefaultPatchIsAppliedIfAvailable(): void

public function testPatcherDoesNotApplyPatchIfNotPatchableExercise(): void
{
$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());
$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
new NullLogger()
);
$exercise = $this->createMock(ExerciseInterface::class);

$code = '<?php $original = true;';
Expand All @@ -46,8 +57,11 @@ public function testPatcherDoesNotApplyPatchIfNotPatchableExercise(): void
*/
public function testPatcher(string $code, Patch $patch, string $expectedResult): void
{
$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());

$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
new NullLogger()
);
$exercise = $this->createMock(PatchableExercise::class);

$exercise
Expand Down Expand Up @@ -146,7 +160,11 @@ public function testBeforeInsertionInsertsAfterStrictTypesDeclaration(): void
$code = '<?php declare(strict_types=1); $original = true;';
$patch = (new Patch())->withInsertion(new Insertion(Insertion::TYPE_BEFORE, '$before = "here";'));

$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());
$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
new NullLogger()
);

$exercise = $this->createMock(PatchableExercise::class);

Expand Down Expand Up @@ -174,7 +192,11 @@ public function testTransformerWithStrictTypes(): void
];
});

$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());
$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
new NullLogger()
);

$exercise = $this->createMock(PatchableExercise::class);

Expand Down Expand Up @@ -202,7 +224,11 @@ public function testTransformerWhichAddsStrictTypesDoesNotResultInDoubleStrictTy
])];
});

$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());
$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
new NullLogger()
);

$exercise = $this->createMock(PatchableExercise::class);

Expand Down Expand Up @@ -231,7 +257,11 @@ public function testAddingStrictTypesDeclareDoesNotBreakBeforeInsertion(): void
})
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, '$before = "here";'));

$patcher = new CodePatcher((new ParserFactory())->create(ParserFactory::PREFER_PHP7), new Standard());
$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
new NullLogger()
);

$exercise = $this->createMock(PatchableExercise::class);

Expand All @@ -245,4 +275,35 @@ public function testAddingStrictTypesDeclareDoesNotBreakBeforeInsertion(): void
$patcher->patch($exercise, $code)
);
}

public function testExceptionIsLoggedIfCodeIsNotParseable(): void
{
$patcher = new CodePatcher(
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
new Standard(),
$logger = $this->createMock(LoggerInterface::class)
);

$exercise = $this->createMock(PatchableExercise::class);

$patch = (new Patch())->withInsertion(new Insertion(Insertion::TYPE_BEFORE, '$before = "here"'));

$exercise
->expects($this->once())
->method('getPatch')
->willReturn($patch);

$logger
->expects($this->once())
->method('critical')
->with(
'Code Insertion could not be parsed: Syntax error, unexpected EOF on line 1',
['code' => '$before = "here"']
);

$this->assertEquals(
"<?php\n\n\$original = true;",
$patcher->patch($exercise, '<?php $original = true;')
);
}
}