77use  PhpSchool \PhpWorkshop \Exercise \ExerciseInterface ;
88use  PhpSchool \PhpWorkshop \Input \Input ;
99use  PhpSchool \PhpWorkshop \Listener \CodePatchListener ;
10+ use  PhpSchool \PhpWorkshop \Utils \System ;
11+ use  PhpSchool \PhpWorkshopTest \Asset \ProvidesSolutionExercise ;
1012use  PHPUnit \Framework \TestCase ;
11- use  RuntimeException ;
1213use  Symfony \Component \Filesystem \Filesystem ;
1314
1415class  CodePatchListenerTest extends  TestCase
@@ -18,6 +19,11 @@ class CodePatchListenerTest extends TestCase
1819     */ 
1920    private  $ file
2021
22+     /** 
23+      * @var string 
24+      */ 
25+     private  $ solution
26+ 
2127    /** 
2228     * @var Filesystem 
2329     */ 
@@ -33,25 +39,35 @@ public function setUp(): void
3339        $ this filesystem  = new  Filesystem ();
3440        $ this codePatcher  = $ this createMock (CodePatcher::class);
3541
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 ());
3743        mkdir (dirname ($ this file ), 0775 , true );
3844        touch ($ this file );
45+ 
46+         $ this solution  = sprintf ('%s/%s/solution.php ' , System::tempDir (), $ this getName ());
47+         touch ($ this solution );
3948    }
4049
41-     public  function  testRevertThrowsExceptionIfPatchNotPreviouslyCalled (): void 
50+     public  function  testPatchUpdatesCode (): void 
4251    {
52+         file_put_contents ($ this file , 'ORIGINAL CONTENT ' );
53+ 
4354        $ inputnew  Input ('app ' , ['program '  => $ this file ]);
4455        $ exercise$ this createMock (ExerciseInterface::class);
4556
57+         $ this codePatcher 
58+             ->expects ($ this once ())
59+             ->method ('patch ' )
60+             ->with ($ exercise'ORIGINAL CONTENT ' )
61+             ->willReturn ('MODIFIED CONTENT ' );
62+ 
4663        $ listenernew  CodePatchListener ($ this codePatcher );
4764        $ eventnew  ExerciseRunnerEvent ('event ' , $ exercise$ input
65+         $ listenerpatch ($ event
4866
49-         $ this expectException (RuntimeException::class);
50-         $ this expectExceptionMessage ('Can only revert previously patched code ' );
51-         $ listenerrevert ($ event
67+         self ::assertStringEqualsFile ($ this file , 'MODIFIED CONTENT ' );
5268    }
5369
54-     public  function  testPatchUpdatesCode (): void 
70+     public  function  testRevertAfterPatch (): void 
5571    {
5672        file_put_contents ($ this file , 'ORIGINAL CONTENT ' );
5773
@@ -67,29 +83,30 @@ public function testPatchUpdatesCode(): void
6783        $ listenernew  CodePatchListener ($ this codePatcher );
6884        $ eventnew  ExerciseRunnerEvent ('event ' , $ exercise$ input
6985        $ listenerpatch ($ event
86+         $ listenerrevert ($ event
7087
71-         $ this -> assertStringEqualsFile ($ this file , 'MODIFIED  CONTENT );
88+         self :: assertStringEqualsFile ($ this file , 'ORIGINAL  CONTENT );
7289    }
7390
74-     public  function  testRevertAfterPatch (): void 
91+     public  function  testPatchesProvidedSolution (): void 
7592    {
7693        file_put_contents ($ this file , 'ORIGINAL CONTENT ' );
7794
7895        $ inputnew  Input ('app ' , ['program '  => $ this file ]);
79-         $ exercise$ this -> createMock (ExerciseInterface::class );
96+         $ exercisenew   ProvidesSolutionExercise ( );
8097
8198        $ this codePatcher 
82-             ->expects ($ this once ( ))
99+             ->expects ($ this exactly ( 2 ))
83100            ->method ('patch ' )
84101            ->with ($ exercise'ORIGINAL CONTENT ' )
85102            ->willReturn ('MODIFIED CONTENT ' );
86103
87104        $ listenernew  CodePatchListener ($ this codePatcher );
88105        $ eventnew  ExerciseRunnerEvent ('event ' , $ exercise$ input
89106        $ listenerpatch ($ event
90-         $ listenerrevert ($ event
91107
92-         $ this assertStringEqualsFile ($ this file , 'ORIGINAL CONTENT ' );
108+         self ::assertStringEqualsFile ($ this file , 'MODIFIED CONTENT ' );
109+         self ::assertStringEqualsFile ($ exercisegetSolution ()->getEntryPoint (), 'MODIFIED CONTENT ' );
93110    }
94111
95112    public  function  tearDown (): void 
0 commit comments