1010use PhpSchool \PhpWorkshop \Utils \System ;
1111use PhpSchool \PhpWorkshopTest \Asset \ProvidesSolutionExercise ;
1212use PHPUnit \Framework \TestCase ;
13+ use Psr \Log \LoggerInterface ;
14+ use Psr \Log \NullLogger ;
1315use Symfony \Component \Filesystem \Filesystem ;
1416
1517class 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