1717use PhpSchool \PhpWorkshopTest \Asset \PatchableExercise ;
1818use PHPUnit \Framework \TestCase ;
1919use PhpSchool \PhpWorkshop \CodeInsertion as Insertion ;
20+ use Psr \Log \LoggerInterface ;
21+ use Psr \Log \NullLogger ;
2022
2123class CodePatcherTest extends TestCase
2224{
@@ -25,7 +27,12 @@ public function testDefaultPatchIsAppliedIfAvailable(): void
2527 $ patch = (new Patch ())
2628 ->withInsertion (new Insertion (Insertion::TYPE_BEFORE , 'ini_set("display_errors", 1); ' ));
2729
28- $ patcher = new CodePatcher ((new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ), new Standard (), $ patch );
30+ $ patcher = new CodePatcher (
31+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
32+ new Standard (),
33+ new NullLogger (),
34+ $ patch
35+ );
2936 $ exercise = $ this ->createMock (ExerciseInterface::class);
3037
3138 $ expected = "<?php \n\nini_set( \"display_errors \", 1); \n\$original = true; " ;
@@ -34,7 +41,11 @@ public function testDefaultPatchIsAppliedIfAvailable(): void
3441
3542 public function testPatcherDoesNotApplyPatchIfNotPatchableExercise (): void
3643 {
37- $ patcher = new CodePatcher ((new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ), new Standard ());
44+ $ patcher = new CodePatcher (
45+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
46+ new Standard (),
47+ new NullLogger ()
48+ );
3849 $ exercise = $ this ->createMock (ExerciseInterface::class);
3950
4051 $ code = '<?php $original = true; ' ;
@@ -46,8 +57,11 @@ public function testPatcherDoesNotApplyPatchIfNotPatchableExercise(): void
4657 */
4758 public function testPatcher (string $ code , Patch $ patch , string $ expectedResult ): void
4859 {
49- $ patcher = new CodePatcher ((new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ), new Standard ());
50-
60+ $ patcher = new CodePatcher (
61+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
62+ new Standard (),
63+ new NullLogger ()
64+ );
5165 $ exercise = $ this ->createMock (PatchableExercise::class);
5266
5367 $ exercise
@@ -146,7 +160,11 @@ public function testBeforeInsertionInsertsAfterStrictTypesDeclaration(): void
146160 $ code = '<?php declare(strict_types=1); $original = true; ' ;
147161 $ patch = (new Patch ())->withInsertion (new Insertion (Insertion::TYPE_BEFORE , '$before = "here"; ' ));
148162
149- $ patcher = new CodePatcher ((new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ), new Standard ());
163+ $ patcher = new CodePatcher (
164+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
165+ new Standard (),
166+ new NullLogger ()
167+ );
150168
151169 $ exercise = $ this ->createMock (PatchableExercise::class);
152170
@@ -174,7 +192,11 @@ public function testTransformerWithStrictTypes(): void
174192 ];
175193 });
176194
177- $ patcher = new CodePatcher ((new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ), new Standard ());
195+ $ patcher = new CodePatcher (
196+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
197+ new Standard (),
198+ new NullLogger ()
199+ );
178200
179201 $ exercise = $ this ->createMock (PatchableExercise::class);
180202
@@ -202,7 +224,11 @@ public function testTransformerWhichAddsStrictTypesDoesNotResultInDoubleStrictTy
202224 ])];
203225 });
204226
205- $ patcher = new CodePatcher ((new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ), new Standard ());
227+ $ patcher = new CodePatcher (
228+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
229+ new Standard (),
230+ new NullLogger ()
231+ );
206232
207233 $ exercise = $ this ->createMock (PatchableExercise::class);
208234
@@ -231,7 +257,11 @@ public function testAddingStrictTypesDeclareDoesNotBreakBeforeInsertion(): void
231257 })
232258 ->withInsertion (new Insertion (Insertion::TYPE_BEFORE , '$before = "here"; ' ));
233259
234- $ patcher = new CodePatcher ((new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ), new Standard ());
260+ $ patcher = new CodePatcher (
261+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
262+ new Standard (),
263+ new NullLogger ()
264+ );
235265
236266 $ exercise = $ this ->createMock (PatchableExercise::class);
237267
@@ -245,4 +275,35 @@ public function testAddingStrictTypesDeclareDoesNotBreakBeforeInsertion(): void
245275 $ patcher ->patch ($ exercise , $ code )
246276 );
247277 }
278+
279+ public function testExceptionIsLoggedIfCodeIsNotParseable (): void
280+ {
281+ $ patcher = new CodePatcher (
282+ (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 ),
283+ new Standard (),
284+ $ logger = $ this ->createMock (LoggerInterface::class)
285+ );
286+
287+ $ exercise = $ this ->createMock (PatchableExercise::class);
288+
289+ $ patch = (new Patch ())->withInsertion (new Insertion (Insertion::TYPE_BEFORE , '$before = "here" ' ));
290+
291+ $ exercise
292+ ->expects ($ this ->once ())
293+ ->method ('getPatch ' )
294+ ->willReturn ($ patch );
295+
296+ $ logger
297+ ->expects ($ this ->once ())
298+ ->method ('critical ' )
299+ ->with (
300+ 'Code Insertion could not be parsed: Syntax error, unexpected EOF on line 1 ' ,
301+ ['code ' => '$before = "here" ' ]
302+ );
303+
304+ $ this ->assertEquals (
305+ "<?php \n\n\$original = true; " ,
306+ $ patcher ->patch ($ exercise , '<?php $original = true; ' )
307+ );
308+ }
248309}
0 commit comments