66use PhpSchool \PhpWorkshop \ExerciseRepository ;
77use PhpSchool \PhpWorkshop \UserState ;
88use PhpSchool \PhpWorkshop \UserStateSerializer ;
9+ use PhpSchool \PhpWorkshop \Utils \Path ;
10+ use PhpSchool \PhpWorkshop \Utils \System ;
911use PhpSchool \PhpWorkshopTest \Asset \CliExerciseInterface ;
10- use PHPUnit \Framework \TestCase ;
1112use Yoast \PHPUnitPolyfills \Polyfills \AssertionRenames ;
1213
13- class UserStateSerializerTest extends TestCase
14+ class UserStateSerializerTest extends BaseTest
1415{
1516 use AssertionRenames;
1617
@@ -36,29 +37,29 @@ class UserStateSerializerTest extends TestCase
3637
3738 public function setUp (): void
3839 {
39- $ this ->tmpDir = sprintf ('%s/%s/%s ' , sys_get_temp_dir (), $ this ->getName (), random_int (1 , 100 ));
40- $ this ->tmpFile = sprintf ('%s/.phpschool-save.json ' , $ this ->tmpDir );
4140 $ this ->exerciseRepository = new ExerciseRepository ([]);
4241 }
4342
4443 public function testIfDirNotExistsItIsCreated (): void
4544 {
46- $ this -> assertFileDoesNotExist ($ this ->tmpDir );
47- new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
48- $ this ->assertFileExists ($ this ->tmpDir );
45+ self :: assertFileDoesNotExist (System:: tempDir ( $ this ->getName ()) );
46+ new UserStateSerializer (System:: tempDir ( $ this ->getName ()) , $ this ->workshopName , $ this ->exerciseRepository );
47+ $ this ->assertFileExists (System:: tempDir ( $ this ->getName ()) );
4948 }
5049
5150 public function testConstructWhenFileExists (): void
5251 {
53- mkdir ($ this ->tmpDir , 0777 , true );
54- $ this ->assertFileExists ($ this ->tmpDir );
55- new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
52+ $ this ->assertFileExists ($ this ->getTemporaryDirectory ());
53+ new UserStateSerializer ($ this ->getTemporaryDirectory (), $ this ->workshopName , $ this ->exerciseRepository );
5654 }
5755
5856 public function testSerializeEmptySate (): void
5957 {
60- mkdir ($ this ->tmpDir , 0777 , true );
61- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
58+ $ serializer = new UserStateSerializer (
59+ $ this ->getTemporaryDirectory (),
60+ $ this ->workshopName ,
61+ $ this ->exerciseRepository
62+ );
6263
6364 $ state = new UserState ();
6465
@@ -70,13 +71,16 @@ public function testSerializeEmptySate(): void
7071 ]);
7172
7273 $ serializer ->serialize ($ state );
73- $ this ->assertSame ($ expected , file_get_contents ($ this ->tmpFile ));
74+ $ this ->assertSame ($ expected , file_get_contents ($ this ->getTemporaryFile ( ' .phpschool-save.json ' ) ));
7475 }
7576
7677 public function testSerialize (): void
7778 {
78- mkdir ($ this ->tmpDir , 0777 , true );
79- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
79+ $ serializer = new UserStateSerializer (
80+ $ this ->getTemporaryDirectory (),
81+ $ this ->workshopName ,
82+ $ this ->exerciseRepository
83+ );
8084
8185 $ state = new UserState (['exercise1 ' ], 'exercise2 ' );
8286 $ serializer ->serialize ($ state );
@@ -90,33 +94,43 @@ public function testSerialize(): void
9094
9195 $ serializer ->serialize ($ state );
9296
93- $ this ->assertSame ($ expected , file_get_contents ($ this ->tmpFile ));
97+ $ this ->assertSame ($ expected , file_get_contents ($ this ->getTemporaryFile ( ' .phpschool-save.json ' ) ));
9498 }
9599
96100 public function testDeserializeNonExistingFile (): void
97101 {
98- mkdir ($ this ->tmpDir , 0777 , true );
99- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
102+ $ serializer = new UserStateSerializer (
103+ $ this ->getTemporaryDirectory (),
104+ $ this ->workshopName ,
105+ $ this ->exerciseRepository
106+ );
107+
100108 $ state = $ serializer ->deSerialize ();
101109 $ this ->assertFalse ($ state ->isAssignedExercise ());
102110 $ this ->assertEmpty ($ state ->getCompletedExercises ());
103111 }
104112
105113 public function testDeserializeEmptyFile (): void
106114 {
107- mkdir ($ this ->tmpDir , 0777 , true );
108- file_put_contents ($ this ->tmpFile , '' );
109- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
115+ $ this ->getTemporaryFile ('.phpschool-save.json ' , '' );
116+ $ serializer = new UserStateSerializer (
117+ $ this ->getTemporaryDirectory (),
118+ $ this ->workshopName ,
119+ $ this ->exerciseRepository
120+ );
110121 $ state = $ serializer ->deSerialize ();
111122 $ this ->assertFalse ($ state ->isAssignedExercise ());
112123 $ this ->assertEmpty ($ state ->getCompletedExercises ());
113124 }
114125
115126 public function testDeserializeNonValidJson (): void
116127 {
117- mkdir ($ this ->tmpDir , 0777 , true );
118- file_put_contents ($ this ->tmpFile , 'yayayayayanotjson ' );
119- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
128+ $ this ->getTemporaryFile ('.phpschool-save.json ' , 'yayayayayanotjson ' );
129+ $ serializer = new UserStateSerializer (
130+ $ this ->getTemporaryDirectory (),
131+ $ this ->workshopName ,
132+ $ this ->exerciseRepository
133+ );
120134 $ state = $ serializer ->deSerialize ();
121135 $ this ->assertFalse ($ state ->isAssignedExercise ());
122136 $ this ->assertEmpty ($ state ->getCompletedExercises ());
@@ -127,21 +141,19 @@ public function testDeserializeNonValidJson(): void
127141 */
128142 public function testDeserialize (array $ data , array $ expected ): void
129143 {
130- mkdir ($ this ->tmpDir , 0777 , true );
131- file_put_contents ($ this ->tmpFile , json_encode ($ data ));
132- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ this ->exerciseRepository );
144+ $ this ->getTemporaryFile ('.phpschool-save.json ' , json_encode ($ data ));
145+ $ serializer = new UserStateSerializer (
146+ $ this ->getTemporaryDirectory (),
147+ $ this ->workshopName ,
148+ $ this ->exerciseRepository
149+ );
133150 $ state = $ serializer ->deSerialize ();
134151
135152 $ this ->assertEquals ($ expected ['completed_exercises ' ], $ state ->getCompletedExercises ());
136153 $ this ->assertEquals (
137154 $ expected ['current_exercise ' ],
138155 $ state ->isAssignedExercise () ? $ state ->getCurrentExercise () : null
139156 );
140-
141- if (file_exists ($ this ->tmpFile )) {
142- unlink ($ this ->tmpFile );
143- }
144- rmdir ($ this ->tmpDir );
145157 }
146158
147159 public function deserializerProvider (): array
@@ -188,32 +200,29 @@ public function deserializerProvider(): array
188200
189201 public function testOldDataWillBeMigratedWhenInCorrectWorkshop (): void
190202 {
191- $ oldSave = sprintf ('%s/.phpschool.json ' , $ this ->tmpDir );
192- $ newSave = sprintf ('%s/.phpschool-save.json ' , $ this ->tmpDir );
193-
194203 $ exercise1 = $ this ->createMock (CliExerciseInterface::class);
195204 $ exercise2 = $ this ->createMock (CliExerciseInterface::class);
196205 $ exercise1 ->method ('getType ' )->willReturn (ExerciseType::CLI ());
197206 $ exercise2 ->method ('getType ' )->willReturn (ExerciseType::CLI ());
198207 $ exercise1 ->method ('getName ' )->willReturn ('Exercise 1 ' );
199208 $ exercise2 ->method ('getName ' )->willReturn ('Exercise 2 ' );
200209
201- $ exercises = [
202- $ exercise1 ,
203- $ exercise2
204- ];
205-
206- $ repo = new ExerciseRepository ($ exercises );
207-
208210 $ oldData = [
209211 'current_exercise ' => 'Exercise 3 ' ,
210212 'completed_exercises ' => ['Exercise 1 ' , 'Exercise 2 ' ],
211213 ];
212214
213- mkdir ($ this ->tmpDir , 0777 , true );
214- file_put_contents ($ oldSave , json_encode ($ oldData ));
215+ $ oldSave = $ this ->getTemporaryFile ('.phpschool.json ' , json_encode ($ oldData ));
216+
217+ $ serializer = new UserStateSerializer (
218+ $ this ->getTemporaryDirectory (),
219+ $ this ->workshopName ,
220+ new ExerciseRepository ([
221+ $ exercise1 ,
222+ $ exercise2
223+ ])
224+ );
215225
216- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ repo );
217226 $ state = $ serializer ->deSerialize ();
218227
219228 $ this ->assertEquals (['Exercise 1 ' , 'Exercise 2 ' ], $ state ->getCompletedExercises ());
@@ -226,16 +235,16 @@ public function testOldDataWillBeMigratedWhenInCorrectWorkshop(): void
226235 ],
227236 ];
228237
229- $ this ->assertFileDoesNotExist ($ oldSave );
230- $ this ->assertFileExists ($ newSave );
231- $ this ->assertEquals ($ expected , json_decode (file_get_contents ($ newSave ), true ));
238+ self ::assertFileDoesNotExist ($ oldSave );
239+ $ this ->assertFileExists (Path::join ($ this ->getTemporaryDirectory (), '.phpschool-save.json ' ));
240+ $ this ->assertEquals (
241+ $ expected ,
242+ json_decode (file_get_contents ($ this ->getTemporaryFile ('.phpschool-save.json ' )), true )
243+ );
232244 }
233245
234246 public function testOldDataWillNotBeMigratedWhenNotInCorrectWorkshop (): void
235247 {
236- $ oldSave = sprintf ('%s/.phpschool.json ' , $ this ->tmpDir );
237- $ newSave = sprintf ('%s/.phpschool-save.json ' , $ this ->tmpDir );
238-
239248 $ exercise1 = $ this ->createMock (CliExerciseInterface::class);
240249 $ exercise2 = $ this ->createMock (CliExerciseInterface::class);
241250 $ exercise1 ->method ('getType ' )->willReturn (ExerciseType::CLI ());
@@ -254,41 +263,28 @@ public function testOldDataWillNotBeMigratedWhenNotInCorrectWorkshop(): void
254263 'completed_exercises ' => ['Exercise 1 from a diff workshop ' , 'Exercise 2 from a diff workshop ' ],
255264 ];
256265
257- mkdir ($ this ->tmpDir , 0777 , true );
258- file_put_contents ($ oldSave , json_encode ($ oldData ));
266+ $ oldSave = $ this ->getTemporaryFile ('.phpschool.json ' , json_encode ($ oldData ));
259267
260- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ repo );
268+ $ serializer = new UserStateSerializer ($ this ->getTemporaryDirectory () , $ this ->workshopName , $ repo );
261269 $ state = $ serializer ->deSerialize ();
262270
263271 $ this ->assertEquals ([], $ state ->getCompletedExercises ());
264272 $ this ->assertFalse ($ state ->isAssignedExercise ());
265273
266274 $ this ->assertFileExists ($ oldSave );
267275 $ this ->assertEquals ($ oldData , json_decode (file_get_contents ($ oldSave ), true ));
268- $ this ->assertFileDoesNotExist ($ newSave );
269-
270- unlink ($ oldSave );
276+ $ this ->assertFileDoesNotExist (Path::join ($ this ->getTemporaryDirectory (), '.phpschool-save.json ' ));
271277 }
272278
273279 public function testOldDataWillNotBeMigratedWhenNotInCorrectWorkshopWithOtherWorkshop (): void
274280 {
275- $ oldSave = sprintf ('%s/.phpschool.json ' , $ this ->tmpDir );
276- $ newSave = sprintf ('%s/.phpschool-save.json ' , $ this ->tmpDir );
277-
278281 $ exercise1 = $ this ->createMock (CliExerciseInterface::class);
279282 $ exercise2 = $ this ->createMock (CliExerciseInterface::class);
280283 $ exercise1 ->method ('getType ' )->willReturn (ExerciseType::CLI ());
281284 $ exercise2 ->method ('getType ' )->willReturn (ExerciseType::CLI ());
282285 $ exercise1 ->method ('getName ' )->willReturn ('Exercise 1 ' );
283286 $ exercise2 ->method ('getName ' )->willReturn ('Exercise 2 ' );
284287
285- $ exercises = [
286- $ exercise1 ,
287- $ exercise2
288- ];
289-
290- $ repo = new ExerciseRepository ($ exercises );
291-
292288 $ oldData = [
293289 'current_exercise ' => 'Exercise 3 ' ,
294290 'completed_exercises ' => ['Exercise 1 from a diff workshop ' , 'Exercise 2 from a diff workshop ' ],
@@ -301,11 +297,17 @@ public function testOldDataWillNotBeMigratedWhenNotInCorrectWorkshopWithOtherWor
301297 ],
302298 ];
303299
304- mkdir ($ this ->tmpDir , 0777 , true );
305- file_put_contents ($ oldSave , json_encode ($ oldData ));
306- file_put_contents ($ newSave , json_encode ($ newData ));
300+ $ oldSave = $ this ->getTemporaryFile ('.phpschool.json ' , json_encode ($ oldData ));
301+ $ newSave = $ this ->getTemporaryFile ('.phpschool-save.json ' , json_encode ($ newData ));
307302
308- $ serializer = new UserStateSerializer ($ this ->tmpDir , $ this ->workshopName , $ repo );
303+ $ serializer = new UserStateSerializer (
304+ $ this ->getTemporaryDirectory (),
305+ $ this ->workshopName ,
306+ new ExerciseRepository ([
307+ $ exercise1 ,
308+ $ exercise2
309+ ])
310+ );
309311 $ state = $ serializer ->deSerialize ();
310312
311313 $ this ->assertEquals (['Exercise 1 ' ], $ state ->getCompletedExercises ());
@@ -321,18 +323,5 @@ public function testOldDataWillNotBeMigratedWhenNotInCorrectWorkshopWithOtherWor
321323
322324 $ this ->assertFileExists ($ newSave );
323325 $ this ->assertEquals ($ newData , json_decode (file_get_contents ($ newSave ), true ));
324-
325- unlink ($ oldSave );
326- }
327-
328- public function tearDown (): void
329- {
330- if (file_exists ($ this ->tmpFile )) {
331- unlink ($ this ->tmpFile );
332- }
333-
334- if (file_exists ($ this ->tmpDir )) {
335- rmdir ($ this ->tmpDir );
336- }
337326 }
338327}
0 commit comments