17
17
use PhpSchool \PhpWorkshopTest \Asset \PatchableExercise ;
18
18
use PHPUnit \Framework \TestCase ;
19
19
use PhpSchool \PhpWorkshop \CodeInsertion as Insertion ;
20
+ use Psr \Log \LoggerInterface ;
21
+ use Psr \Log \NullLogger ;
20
22
21
23
class CodePatcherTest extends TestCase
22
24
{
@@ -25,7 +27,12 @@ public function testDefaultPatchIsAppliedIfAvailable(): void
25
27
$ patch = (new Patch ())
26
28
->withInsertion (new Insertion (Insertion::TYPE_BEFORE , 'ini_set("display_errors", 1); ' ));
27
29
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
+ );
29
36
$ exercise = $ this ->createMock (ExerciseInterface::class);
30
37
31
38
$ expected = "<?php \n\nini_set( \"display_errors \", 1); \n\$original = true; " ;
@@ -34,7 +41,11 @@ public function testDefaultPatchIsAppliedIfAvailable(): void
34
41
35
42
public function testPatcherDoesNotApplyPatchIfNotPatchableExercise (): void
36
43
{
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
+ );
38
49
$ exercise = $ this ->createMock (ExerciseInterface::class);
39
50
40
51
$ code = '<?php $original = true; ' ;
@@ -46,8 +57,11 @@ public function testPatcherDoesNotApplyPatchIfNotPatchableExercise(): void
46
57
*/
47
58
public function testPatcher (string $ code , Patch $ patch , string $ expectedResult ): void
48
59
{
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
+ );
51
65
$ exercise = $ this ->createMock (PatchableExercise::class);
52
66
53
67
$ exercise
@@ -146,7 +160,11 @@ public function testBeforeInsertionInsertsAfterStrictTypesDeclaration(): void
146
160
$ code = '<?php declare(strict_types=1); $original = true; ' ;
147
161
$ patch = (new Patch ())->withInsertion (new Insertion (Insertion::TYPE_BEFORE , '$before = "here"; ' ));
148
162
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
+ );
150
168
151
169
$ exercise = $ this ->createMock (PatchableExercise::class);
152
170
@@ -174,7 +192,11 @@ public function testTransformerWithStrictTypes(): void
174
192
];
175
193
});
176
194
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
+ );
178
200
179
201
$ exercise = $ this ->createMock (PatchableExercise::class);
180
202
@@ -202,7 +224,11 @@ public function testTransformerWhichAddsStrictTypesDoesNotResultInDoubleStrictTy
202
224
])];
203
225
});
204
226
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
+ );
206
232
207
233
$ exercise = $ this ->createMock (PatchableExercise::class);
208
234
@@ -231,7 +257,11 @@ public function testAddingStrictTypesDeclareDoesNotBreakBeforeInsertion(): void
231
257
})
232
258
->withInsertion (new Insertion (Insertion::TYPE_BEFORE , '$before = "here"; ' ));
233
259
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
+ );
235
265
236
266
$ exercise = $ this ->createMock (PatchableExercise::class);
237
267
@@ -245,4 +275,35 @@ public function testAddingStrictTypesDeclareDoesNotBreakBeforeInsertion(): void
245
275
$ patcher ->patch ($ exercise , $ code )
246
276
);
247
277
}
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
+ }
248
309
}
0 commit comments