10
10
use PhpSchool \PhpWorkshop \Utils \System ;
11
11
use PhpSchool \PhpWorkshopTest \Asset \ProvidesSolutionExercise ;
12
12
use PHPUnit \Framework \TestCase ;
13
+ use Psr \Log \LoggerInterface ;
14
+ use Psr \Log \NullLogger ;
13
15
use Symfony \Component \Filesystem \Filesystem ;
14
16
15
17
class CodePatchListenerTest extends TestCase
@@ -47,6 +49,11 @@ public function setUp(): void
47
49
touch ($ this ->solution );
48
50
}
49
51
52
+ public function tearDown (): void
53
+ {
54
+ $ this ->filesystem ->remove (dirname ($ this ->file ));
55
+ }
56
+
50
57
public function testPatchUpdatesCode (): void
51
58
{
52
59
file_put_contents ($ this ->file , 'ORIGINAL CONTENT ' );
@@ -60,7 +67,7 @@ public function testPatchUpdatesCode(): void
60
67
->with ($ exercise , 'ORIGINAL CONTENT ' )
61
68
->willReturn ('MODIFIED CONTENT ' );
62
69
63
- $ listener = new CodePatchListener ($ this ->codePatcher );
70
+ $ listener = new CodePatchListener ($ this ->codePatcher , new NullLogger (), false );
64
71
$ event = new ExerciseRunnerEvent ('event ' , $ exercise , $ input );
65
72
$ listener ->patch ($ event );
66
73
@@ -80,7 +87,7 @@ public function testRevertAfterPatch(): void
80
87
->with ($ exercise , 'ORIGINAL CONTENT ' )
81
88
->willReturn ('MODIFIED CONTENT ' );
82
89
83
- $ listener = new CodePatchListener ($ this ->codePatcher );
90
+ $ listener = new CodePatchListener ($ this ->codePatcher , new NullLogger (), false );
84
91
$ event = new ExerciseRunnerEvent ('event ' , $ exercise , $ input );
85
92
$ listener ->patch ($ event );
86
93
$ listener ->revert ($ event );
@@ -101,16 +108,55 @@ public function testPatchesProvidedSolution(): void
101
108
->withConsecutive ([$ exercise , 'ORIGINAL CONTENT ' ], [$ exercise , "<?php \n\necho 'Hello World'; \n" ])
102
109
->willReturn ('MODIFIED CONTENT ' );
103
110
104
- $ listener = new CodePatchListener ($ this ->codePatcher );
111
+ $ listener = new CodePatchListener ($ this ->codePatcher , new NullLogger (), false );
105
112
$ event = new ExerciseRunnerEvent ('event ' , $ exercise , $ input );
106
113
$ listener ->patch ($ event );
107
114
108
115
self ::assertStringEqualsFile ($ this ->file , 'MODIFIED CONTENT ' );
109
116
self ::assertStringEqualsFile ($ exercise ->getSolution ()->getEntryPoint (), 'MODIFIED CONTENT ' );
110
117
}
111
118
112
- public function tearDown (): void
119
+ public function testFileIsLoggedWhenPatches (): void
113
120
{
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 ' );
115
161
}
116
162
}
0 commit comments