8
8
use PhpSchool \PhpWorkshop \Listener \InitialCodeListener ;
9
9
use PhpSchool \PhpWorkshopTest \Asset \CliExerciseImpl ;
10
10
use PhpSchool \PhpWorkshopTest \Asset \ExerciseWithInitialCode ;
11
- use PHPUnit \Framework \TestCase ;
12
- use Symfony \Component \Filesystem \Filesystem ;
11
+ use PhpSchool \PhpWorkshopTest \ContainerAwareTest ;
13
12
14
- class InitialCodeListenerTest extends TestCase
13
+ class InitialCodeListenerTest extends ContainerAwareTest
15
14
{
16
- /**
17
- * @var Filesystem
18
- */
19
- private $ filesystem ;
20
-
21
- /**
22
- * @var string
23
- */
24
- private $ cwd ;
25
-
26
15
public function setUp (): void
27
16
{
28
- $ this -> filesystem = new Filesystem ();
17
+ parent :: setUp ();
29
18
30
- $ this ->cwd = sprintf ( ' %s/%s ' , str_replace ( '\\' , ' / ' , sys_get_temp_dir ()), $ this -> getName () );
31
- mkdir ( $ this ->cwd , 0775 , true );
19
+ $ this ->mockCurrentWorkingDirectory ( );
20
+ $ this ->mockLogger ( );
32
21
}
33
22
34
23
public function testExerciseCodeIsCopiedIfExerciseProvidesInitialCode (): void
@@ -37,30 +26,67 @@ public function testExerciseCodeIsCopiedIfExerciseProvidesInitialCode(): void
37
26
38
27
$ event = new Event ('exercise.selected ' , ['exercise ' => $ exercise ]);
39
28
40
- $ listener = new InitialCodeListener ( $ this ->cwd );
29
+ $ listener = $ this ->container -> get (InitialCodeListener::class );
41
30
$ listener ->__invoke ($ event );
42
31
43
- $ this ->assertFileExists ($ this ->cwd . '/init-solution.php ' );
32
+ $ this ->assertFileExists ($ this ->getCurrentWorkingDirectory () . '/init-solution.php ' );
44
33
$ this ->assertFileEquals (
45
34
$ exercise ->getInitialCode ()->getFiles ()[0 ]->getAbsolutePath (),
46
- $ this ->cwd . '/init-solution.php '
35
+ $ this ->getCurrentWorkingDirectory () . '/init-solution.php '
36
+ );
37
+
38
+ $ this ->assertLoggerHasMessages (
39
+ [
40
+ [
41
+ 'level ' => 'debug ' ,
42
+ 'message ' => 'File successfully copied to working directory ' ,
43
+ 'context ' => [
44
+ 'exercise ' => 'exercise-with-initial-code ' ,
45
+ 'workingDir ' => $ this ->getCurrentWorkingDirectory (),
46
+ 'file ' => $ exercise ->getInitialCode ()->getFiles ()[0 ]->getAbsolutePath ()
47
+ ]
48
+ ]
49
+ ]
47
50
);
48
51
}
49
52
50
- public function testExerciseCodeIsNotCopiedIfExerciseDoesNotProvideInitialCode (): void
53
+ public function testExerciseCodeIsNotCopiedIfFileWithSameNameExistsInWorkingDirectory (): void
51
54
{
52
- $ exercise = new CliExerciseImpl ();
55
+ $ exercise = new ExerciseWithInitialCode ();
53
56
54
57
$ event = new Event ('exercise.selected ' , ['exercise ' => $ exercise ]);
55
58
56
- $ listener = new InitialCodeListener ($ this ->cwd );
59
+ touch ($ this ->getCurrentWorkingDirectory () . '/init-solution.php ' );
60
+
61
+ $ listener = $ this ->container ->get (InitialCodeListener::class);
57
62
$ listener ->__invoke ($ event );
58
63
59
- $ this ->assertEmpty (array_diff (scandir ($ this ->cwd ), ['. ' , '.. ' ]));
64
+ $ this ->assertFileExists ($ this ->getCurrentWorkingDirectory () . '/init-solution.php ' );
65
+
66
+ $ this ->assertLoggerHasMessages (
67
+ [
68
+ [
69
+ 'level ' => 'debug ' ,
70
+ 'message ' => 'File not copied. File with same name already exists in working directory ' ,
71
+ 'context ' => [
72
+ 'exercise ' => 'exercise-with-initial-code ' ,
73
+ 'workingDir ' => $ this ->getCurrentWorkingDirectory (),
74
+ 'file ' => $ exercise ->getInitialCode ()->getFiles ()[0 ]->getAbsolutePath ()
75
+ ]
76
+ ]
77
+ ]
78
+ );
60
79
}
61
80
62
- public function tearDown (): void
81
+ public function testExerciseCodeIsNotCopiedIfExerciseDoesNotProvideInitialCode (): void
63
82
{
64
- $ this ->filesystem ->remove ($ this ->cwd );
83
+ $ exercise = new CliExerciseImpl ();
84
+
85
+ $ event = new Event ('exercise.selected ' , ['exercise ' => $ exercise ]);
86
+
87
+ $ listener = $ this ->container ->get (InitialCodeListener::class);
88
+ $ listener ->__invoke ($ event );
89
+
90
+ $ this ->assertEmpty (array_diff (scandir ($ this ->getCurrentWorkingDirectory ()), ['. ' , '.. ' ]));
65
91
}
66
92
}
0 commit comments