7
7
use PhpSchool \PhpWorkshop \Exercise \ExerciseInterface ;
8
8
use PhpSchool \PhpWorkshop \Input \Input ;
9
9
use PhpSchool \PhpWorkshop \Listener \CodePatchListener ;
10
+ use PhpSchool \PhpWorkshop \Utils \System ;
11
+ use PhpSchool \PhpWorkshopTest \Asset \ProvidesSolutionExercise ;
10
12
use PHPUnit \Framework \TestCase ;
11
- use RuntimeException ;
12
13
use Symfony \Component \Filesystem \Filesystem ;
13
14
14
15
class CodePatchListenerTest extends TestCase
@@ -18,6 +19,11 @@ class CodePatchListenerTest extends TestCase
18
19
*/
19
20
private $ file ;
20
21
22
+ /**
23
+ * @var string
24
+ */
25
+ private $ solution ;
26
+
21
27
/**
22
28
* @var Filesystem
23
29
*/
@@ -33,25 +39,35 @@ public function setUp(): void
33
39
$ this ->filesystem = new Filesystem ();
34
40
$ this ->codePatcher = $ this ->createMock (CodePatcher::class);
35
41
36
- $ this ->file = sprintf ('%s/%s/submission.php ' , str_replace ( '\\' , ' / ' , sys_get_temp_dir () ), $ this ->getName ());
42
+ $ this ->file = sprintf ('%s/%s/submission.php ' , System:: tempDir ( ), $ this ->getName ());
37
43
mkdir (dirname ($ this ->file ), 0775 , true );
38
44
touch ($ this ->file );
45
+
46
+ $ this ->solution = sprintf ('%s/%s/solution.php ' , System::tempDir (), $ this ->getName ());
47
+ touch ($ this ->solution );
39
48
}
40
49
41
- public function testRevertThrowsExceptionIfPatchNotPreviouslyCalled (): void
50
+ public function testPatchUpdatesCode (): void
42
51
{
52
+ file_put_contents ($ this ->file , 'ORIGINAL CONTENT ' );
53
+
43
54
$ input = new Input ('app ' , ['program ' => $ this ->file ]);
44
55
$ exercise = $ this ->createMock (ExerciseInterface::class);
45
56
57
+ $ this ->codePatcher
58
+ ->expects ($ this ->once ())
59
+ ->method ('patch ' )
60
+ ->with ($ exercise , 'ORIGINAL CONTENT ' )
61
+ ->willReturn ('MODIFIED CONTENT ' );
62
+
46
63
$ listener = new CodePatchListener ($ this ->codePatcher );
47
64
$ event = new ExerciseRunnerEvent ('event ' , $ exercise , $ input );
65
+ $ listener ->patch ($ event );
48
66
49
- $ this ->expectException (RuntimeException::class);
50
- $ this ->expectExceptionMessage ('Can only revert previously patched code ' );
51
- $ listener ->revert ($ event );
67
+ self ::assertStringEqualsFile ($ this ->file , 'MODIFIED CONTENT ' );
52
68
}
53
69
54
- public function testPatchUpdatesCode (): void
70
+ public function testRevertAfterPatch (): void
55
71
{
56
72
file_put_contents ($ this ->file , 'ORIGINAL CONTENT ' );
57
73
@@ -67,29 +83,30 @@ public function testPatchUpdatesCode(): void
67
83
$ listener = new CodePatchListener ($ this ->codePatcher );
68
84
$ event = new ExerciseRunnerEvent ('event ' , $ exercise , $ input );
69
85
$ listener ->patch ($ event );
86
+ $ listener ->revert ($ event );
70
87
71
- $ this -> assertStringEqualsFile ($ this ->file , 'MODIFIED CONTENT ' );
88
+ self :: assertStringEqualsFile ($ this ->file , 'ORIGINAL CONTENT ' );
72
89
}
73
90
74
- public function testRevertAfterPatch (): void
91
+ public function testPatchesProvidedSolution (): void
75
92
{
76
93
file_put_contents ($ this ->file , 'ORIGINAL CONTENT ' );
77
94
78
95
$ input = new Input ('app ' , ['program ' => $ this ->file ]);
79
- $ exercise = $ this -> createMock (ExerciseInterface::class );
96
+ $ exercise = new ProvidesSolutionExercise ( );
80
97
81
98
$ this ->codePatcher
82
- ->expects ($ this ->once ( ))
99
+ ->expects ($ this ->exactly ( 2 ))
83
100
->method ('patch ' )
84
- ->with ( $ exercise , 'ORIGINAL CONTENT ' )
101
+ ->withConsecutive ([ $ exercise , 'ORIGINAL CONTENT ' ], [ $ exercise , " <?php \n\n echo 'Hello World'; \n" ] )
85
102
->willReturn ('MODIFIED CONTENT ' );
86
103
87
104
$ listener = new CodePatchListener ($ this ->codePatcher );
88
105
$ event = new ExerciseRunnerEvent ('event ' , $ exercise , $ input );
89
106
$ listener ->patch ($ event );
90
- $ listener ->revert ($ event );
91
107
92
- $ this ->assertStringEqualsFile ($ this ->file , 'ORIGINAL CONTENT ' );
108
+ self ::assertStringEqualsFile ($ this ->file , 'MODIFIED CONTENT ' );
109
+ self ::assertStringEqualsFile ($ exercise ->getSolution ()->getEntryPoint (), 'MODIFIED CONTENT ' );
93
110
}
94
111
95
112
public function tearDown (): void
0 commit comments