Skip to content

Patch internal solution entrypoint files #211

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
May 19, 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
44 changes: 23 additions & 21 deletions src/Listener/CodePatchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace PhpSchool\PhpWorkshop\Listener;

use PhpSchool\PhpWorkshop\CodePatcher;
use PhpSchool\PhpWorkshop\Event\EventInterface;
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
use RuntimeException;

/**
* Listener which patches student's solutions
* Listener which patches internal and student's solutions
*/
class CodePatchListener
{
Expand All @@ -19,9 +21,9 @@ class CodePatchListener
private $codePatcher;

/**
* @var string
* @var array<string, string>
*/
private $originalCode;
private $originalCode = [];

/**
* @param CodePatcher $codePatcher
Expand All @@ -36,34 +38,34 @@ public function __construct(CodePatcher $codePatcher)
*/
public function patch(ExerciseRunnerEvent $event): void
{
$fileName = $event->getInput()->getArgument('program');
$files = [$event->getInput()->getArgument('program')];

if (null === $fileName) {
return;
$exercise = $event->getExercise();
if ($exercise instanceof ProvidesSolution) {
$files[] = $exercise->getSolution()->getEntryPoint();
}

$this->originalCode = (string) file_get_contents($fileName);
file_put_contents(
$fileName,
$this->codePatcher->patch($event->getExercise(), $this->originalCode)
);
foreach (array_filter($files) as $fileName) {
$this->originalCode[$fileName] = (string) file_get_contents($fileName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if the files are the same name? I think it’s using full paths right so shouldn’t matter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comes in as full paths from what I can see, so technically shouldn't matter 👍


file_put_contents(
$fileName,
$this->codePatcher->patch($event->getExercise(), $this->originalCode[$fileName])
);
}
}

/**
* @param ExerciseRunnerEvent $event
* @param EventInterface $event
*/
public function revert(ExerciseRunnerEvent $event): void
public function revert(EventInterface $event): void
{
if (null === $this->originalCode) {
throw new RuntimeException('Can only revert previously patched code');
}

$fileName = $event->getInput()->getArgument('program');

if (null === $fileName) {
if (null === $this->originalCode || empty($this->originalCode)) {
return;
}

file_put_contents($fileName, $this->originalCode);
foreach ($this->originalCode as $fileName => $contents) {
file_put_contents($fileName, $contents);
}
}
}
50 changes: 50 additions & 0 deletions test/Asset/ProvidesSolutionExercise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshopTest\Asset;

use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;

class ProvidesSolutionExercise implements ExerciseInterface, ProvidesSolution
{
public function getName(): string
{
return 'exercise-provides-solution';
}

public function getType(): ExerciseType
{
// TODO: Implement getType() method.
}

public function getProblem(): string
{
// TODO: Implement getProblem() method.
}

public function configure(ExerciseDispatcher $dispatcher): void
{
// TODO: Implement configure() method.
}

public function getDescription(): string
{
// TODO: Implement getDescription() method.
}

public function tearDown(): void
{
// TODO: Implement tearDown() method.
}

public function getSolution(): SolutionInterface
{
return SingleFileSolution::fromFile(__DIR__ . '/provided-solution/solution.php');
}
}
3 changes: 3 additions & 0 deletions test/Asset/provided-solution/solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'Hello World';
45 changes: 31 additions & 14 deletions test/Listener/CodePatchListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Listener\CodePatchListener;
use PhpSchool\PhpWorkshop\Utils\System;
use PhpSchool\PhpWorkshopTest\Asset\ProvidesSolutionExercise;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Filesystem\Filesystem;

class CodePatchListenerTest extends TestCase
Expand All @@ -18,6 +19,11 @@ class CodePatchListenerTest extends TestCase
*/
private $file;

/**
* @var string
*/
private $solution;

/**
* @var Filesystem
*/
Expand All @@ -33,25 +39,35 @@ public function setUp(): void
$this->filesystem = new Filesystem();
$this->codePatcher = $this->createMock(CodePatcher::class);

$this->file = sprintf('%s/%s/submission.php', str_replace('\\', '/', sys_get_temp_dir()), $this->getName());
$this->file = sprintf('%s/%s/submission.php', System::tempDir(), $this->getName());
mkdir(dirname($this->file), 0775, true);
touch($this->file);

$this->solution = sprintf('%s/%s/solution.php', System::tempDir(), $this->getName());
touch($this->solution);
}

public function testRevertThrowsExceptionIfPatchNotPreviouslyCalled(): void
public function testPatchUpdatesCode(): void
{
file_put_contents($this->file, 'ORIGINAL CONTENT');

$input = new Input('app', ['program' => $this->file]);
$exercise = $this->createMock(ExerciseInterface::class);

$this->codePatcher
->expects($this->once())
->method('patch')
->with($exercise, 'ORIGINAL CONTENT')
->willReturn('MODIFIED CONTENT');

$listener = new CodePatchListener($this->codePatcher);
$event = new ExerciseRunnerEvent('event', $exercise, $input);
$listener->patch($event);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Can only revert previously patched code');
$listener->revert($event);
self::assertStringEqualsFile($this->file, 'MODIFIED CONTENT');
}

public function testPatchUpdatesCode(): void
public function testRevertAfterPatch(): void
{
file_put_contents($this->file, 'ORIGINAL CONTENT');

Expand All @@ -67,29 +83,30 @@ public function testPatchUpdatesCode(): void
$listener = new CodePatchListener($this->codePatcher);
$event = new ExerciseRunnerEvent('event', $exercise, $input);
$listener->patch($event);
$listener->revert($event);

$this->assertStringEqualsFile($this->file, 'MODIFIED CONTENT');
self::assertStringEqualsFile($this->file, 'ORIGINAL CONTENT');
}

public function testRevertAfterPatch(): void
public function testPatchesProvidedSolution(): void
{
file_put_contents($this->file, 'ORIGINAL CONTENT');

$input = new Input('app', ['program' => $this->file]);
$exercise = $this->createMock(ExerciseInterface::class);
$exercise = new ProvidesSolutionExercise();

$this->codePatcher
->expects($this->once())
->expects($this->exactly(2))
->method('patch')
->with($exercise, 'ORIGINAL CONTENT')
->withConsecutive([$exercise, 'ORIGINAL CONTENT'], [$exercise, "<?php\n\necho 'Hello World';\n"])
->willReturn('MODIFIED CONTENT');

$listener = new CodePatchListener($this->codePatcher);
$event = new ExerciseRunnerEvent('event', $exercise, $input);
$listener->patch($event);
$listener->revert($event);

$this->assertStringEqualsFile($this->file, 'ORIGINAL CONTENT');
self::assertStringEqualsFile($this->file, 'MODIFIED CONTENT');
self::assertStringEqualsFile($exercise->getSolution()->getEntryPoint(), 'MODIFIED CONTENT');
}

public function tearDown(): void
Expand Down