Skip to content

Commit 0c3a11b

Browse files
authored
Merge pull request #224 from php-school/logging-patching-debug-mode
2 parents d22a7db + 4fc9936 commit 0c3a11b

File tree

4 files changed

+85
-7
lines changed

4 files changed

+85
-7
lines changed

app/config.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@
231231
},
232232
PrepareSolutionListener::class => create(),
233233
CodePatchListener::class => function (ContainerInterface $c) {
234-
return new CodePatchListener($c->get(CodePatcher::class));
234+
return new CodePatchListener(
235+
$c->get(CodePatcher::class),
236+
$c->get(LoggerInterface::class),
237+
$c->get('debugMode')
238+
);
235239
},
236240
SelfCheckListener::class => function (ContainerInterface $c) {
237241
return new SelfCheckListener($c->get(ResultAggregator::class));

src/Listener/CodePatchListener.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpSchool\PhpWorkshop\Event\EventInterface;
99
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
1010
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
11+
use Psr\Log\LoggerInterface;
1112
use RuntimeException;
1213

1314
/**
@@ -20,17 +21,31 @@ class CodePatchListener
2021
*/
2122
private $codePatcher;
2223

24+
/**
25+
* @var LoggerInterface
26+
*/
27+
private $logger;
28+
29+
/**
30+
* @var bool
31+
*/
32+
private $debugMode;
33+
2334
/**
2435
* @var array<string, string>
2536
*/
2637
private $originalCode = [];
2738

2839
/**
2940
* @param CodePatcher $codePatcher
41+
* @param LoggerInterface $logger
42+
* @param bool $debugMode
3043
*/
31-
public function __construct(CodePatcher $codePatcher)
44+
public function __construct(CodePatcher $codePatcher, LoggerInterface $logger, bool $debugMode)
3245
{
3346
$this->codePatcher = $codePatcher;
47+
$this->logger = $logger;
48+
$this->debugMode = $debugMode;
3449
}
3550

3651
/**
@@ -46,6 +61,8 @@ public function patch(ExerciseRunnerEvent $event): void
4661
}
4762

4863
foreach (array_filter($files) as $fileName) {
64+
$this->logger->debug("Patching file: $fileName");
65+
4966
$this->originalCode[$fileName] = (string) file_get_contents($fileName);
5067

5168
file_put_contents(
@@ -64,6 +81,11 @@ public function revert(EventInterface $event): void
6481
return;
6582
}
6683

84+
//if we're in debug mode leave the students patch for debugging
85+
if ($event instanceof ExerciseRunnerEvent && $this->debugMode) {
86+
unset($this->originalCode[$event->getInput()->getArgument('program')]);
87+
}
88+
6789
foreach ($this->originalCode as $fileName => $contents) {
6890
file_put_contents($fileName, $contents);
6991
}

src/TestUtils/WorkshopExerciseTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpSchool\PhpWorkshop\ResultAggregator;
2424
use PhpSchool\PhpWorkshop\Utils\ArrayObject;
2525
use PhpSchool\PhpWorkshop\Utils\Collection;
26+
use PhpSchool\PhpWorkshop\Utils\System;
2627
use PHPUnit\Framework\ExpectationFailedException;
2728
use PHPUnit\Framework\TestCase;
2829
use Psr\Container\ContainerInterface;
@@ -53,6 +54,11 @@ public function setUp(): void
5354
$this->container = $this->app->configure();
5455
}
5556

57+
public function tearDown(): void
58+
{
59+
(new Filesystem())->remove(System::tempDir());
60+
}
61+
5662
/**
5763
* @return class-string
5864
*/

test/Listener/CodePatchListenerTest.php

+51-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PhpSchool\PhpWorkshop\Utils\System;
1111
use PhpSchool\PhpWorkshopTest\Asset\ProvidesSolutionExercise;
1212
use PHPUnit\Framework\TestCase;
13+
use Psr\Log\LoggerInterface;
14+
use Psr\Log\NullLogger;
1315
use Symfony\Component\Filesystem\Filesystem;
1416

1517
class CodePatchListenerTest extends TestCase
@@ -47,6 +49,11 @@ public function setUp(): void
4749
touch($this->solution);
4850
}
4951

52+
public function tearDown(): void
53+
{
54+
$this->filesystem->remove(dirname($this->file));
55+
}
56+
5057
public function testPatchUpdatesCode(): void
5158
{
5259
file_put_contents($this->file, 'ORIGINAL CONTENT');
@@ -60,7 +67,7 @@ public function testPatchUpdatesCode(): void
6067
->with($exercise, 'ORIGINAL CONTENT')
6168
->willReturn('MODIFIED CONTENT');
6269

63-
$listener = new CodePatchListener($this->codePatcher);
70+
$listener = new CodePatchListener($this->codePatcher, new NullLogger(), false);
6471
$event = new ExerciseRunnerEvent('event', $exercise, $input);
6572
$listener->patch($event);
6673

@@ -80,7 +87,7 @@ public function testRevertAfterPatch(): void
8087
->with($exercise, 'ORIGINAL CONTENT')
8188
->willReturn('MODIFIED CONTENT');
8289

83-
$listener = new CodePatchListener($this->codePatcher);
90+
$listener = new CodePatchListener($this->codePatcher, new NullLogger(), false);
8491
$event = new ExerciseRunnerEvent('event', $exercise, $input);
8592
$listener->patch($event);
8693
$listener->revert($event);
@@ -101,16 +108,55 @@ public function testPatchesProvidedSolution(): void
101108
->withConsecutive([$exercise, 'ORIGINAL CONTENT'], [$exercise, "<?php\n\necho 'Hello World';\n"])
102109
->willReturn('MODIFIED CONTENT');
103110

104-
$listener = new CodePatchListener($this->codePatcher);
111+
$listener = new CodePatchListener($this->codePatcher, new NullLogger(), false);
105112
$event = new ExerciseRunnerEvent('event', $exercise, $input);
106113
$listener->patch($event);
107114

108115
self::assertStringEqualsFile($this->file, 'MODIFIED CONTENT');
109116
self::assertStringEqualsFile($exercise->getSolution()->getEntryPoint(), 'MODIFIED CONTENT');
110117
}
111118

112-
public function tearDown(): void
119+
public function testFileIsLoggedWhenPatches(): void
113120
{
114-
$this->filesystem->remove(dirname($this->file));
121+
file_put_contents($this->file, 'ORIGINAL CONTENT');
122+
123+
$input = new Input('app', ['program' => $this->file]);
124+
$exercise = $this->createMock(ExerciseInterface::class);
125+
126+
$this->codePatcher
127+
->expects($this->once())
128+
->method('patch')
129+
->with($exercise, 'ORIGINAL CONTENT')
130+
->willReturn('MODIFIED CONTENT');
131+
132+
$logger = $this->createMock(LoggerInterface::class);
133+
$logger->expects($this->once())
134+
->method('debug')
135+
->with('Patching file: ' . $this->file);
136+
137+
$listener = new CodePatchListener($this->codePatcher, $logger, false);
138+
$event = new ExerciseRunnerEvent('event', $exercise, $input);
139+
$listener->patch($event);
140+
}
141+
142+
public function testRevertDoesNotRevertStudentSubmissionPatchIfInDebugMode(): void
143+
{
144+
file_put_contents($this->file, 'ORIGINAL CONTENT');
145+
146+
$input = new Input('app', ['program' => $this->file]);
147+
$exercise = $this->createMock(ExerciseInterface::class);
148+
149+
$this->codePatcher
150+
->expects($this->once())
151+
->method('patch')
152+
->with($exercise, 'ORIGINAL CONTENT')
153+
->willReturn('MODIFIED CONTENT');
154+
155+
$listener = new CodePatchListener($this->codePatcher, new NullLogger(), true);
156+
$event = new ExerciseRunnerEvent('event', $exercise, $input);
157+
$listener->patch($event);
158+
$listener->revert($event);
159+
160+
self::assertStringEqualsFile($this->file, 'MODIFIED CONTENT');
115161
}
116162
}

0 commit comments

Comments
 (0)