4
4
5
5
use InvalidArgumentException ;
6
6
use PhpSchool \PhpWorkshop \Solution \DirectorySolution ;
7
- use PhpSchool \PhpWorkshop \Utils \Path ;
8
- use PHPUnit \ Framework \ TestCase ;
7
+ use PhpSchool \PhpWorkshop \Utils \System ;
8
+ use PhpSchool \ PhpWorkshopTest \ BaseTest ;
9
9
use Symfony \Component \Filesystem \Filesystem ;
10
10
11
- class DirectorySolutionTest extends TestCase
11
+ class DirectorySolutionTest extends BaseTest
12
12
{
13
- /**
14
- * @var string
15
- */
16
- private $ tempPath ;
17
-
18
- public function setUp (): void
19
- {
20
- $ this ->tempPath = sprintf ('%s/%s ' , realpath (sys_get_temp_dir ()), $ this ->getName ());
21
- @mkdir ($ this ->tempPath );
22
- }
23
-
24
13
public function tearDown (): void
25
14
{
26
- $ fileSystem = new Filesystem ();
27
- $ fileSystem -> remove (Path:: join ( realpath ( sys_get_temp_dir ()), ' php-school ' ));
28
- $ fileSystem -> remove ( $ this -> tempPath );
15
+ ( new Filesystem ())-> remove (System:: tempDir ( ' php-school ' ) );
16
+
17
+ parent :: tearDown ( );
29
18
}
30
19
31
20
public function testExceptionIsThrownIfEntryPointDoesNotExist (): void
32
21
{
33
- touch ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) );
22
+ $ this -> getTemporaryFile ( ' some-class.php ' );
34
23
35
24
$ this ->expectException (InvalidArgumentException::class);
36
25
$ this ->expectExceptionMessageMatches ('/Entry point: "solution.php" does not exist in: ".*"/ ' );
37
26
38
- DirectorySolution::fromDirectory ($ this ->tempPath );
27
+ DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
39
28
}
40
29
41
30
public function testWithDefaultEntryPoint (): void
42
31
{
43
- file_put_contents ( sprintf ( ' %s/ solution.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
44
- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
32
+ $ this -> getTemporaryFile ( ' solution.php ' , 'ENTRYPOINT ' );
33
+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
45
34
46
- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
35
+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
47
36
48
37
self ::assertFalse ($ solution ->hasComposerFile ());
49
38
self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -56,10 +45,10 @@ public function testWithDefaultEntryPoint(): void
56
45
57
46
public function testWithManualEntryPoint (): void
58
47
{
59
- file_put_contents ( sprintf ( ' %s/ index.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
60
- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
48
+ $ this -> getTemporaryFile ( ' index.php ' , 'ENTRYPOINT ' );
49
+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
61
50
62
- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , [], 'index.php ' );
51
+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , [], 'index.php ' );
63
52
64
53
self ::assertFalse ($ solution ->hasComposerFile ());
65
54
self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -72,11 +61,11 @@ public function testWithManualEntryPoint(): void
72
61
73
62
public function testHasComposerFileReturnsTrueIfPresent (): void
74
63
{
75
- file_put_contents ( sprintf ( ' %s/ solution.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
76
- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
77
- touch ( sprintf ( ' %s/ composer.lock ', $ this -> tempPath ) );
64
+ $ this -> getTemporaryFile ( ' solution.php ' , 'ENTRYPOINT ' );
65
+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
66
+ $ this -> getTemporaryFile ( ' composer.lock ' );
78
67
79
- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
68
+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
80
69
81
70
self ::assertTrue ($ solution ->hasComposerFile ());
82
71
self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -89,13 +78,13 @@ public function testHasComposerFileReturnsTrueIfPresent(): void
89
78
90
79
public function testWithExceptions (): void
91
80
{
92
- file_put_contents ( sprintf ( ' %s/ solution.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
93
- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
94
- touch ( sprintf ( ' %s/ exclude.txt ', $ this -> tempPath ) );
81
+ $ this -> getTemporaryFile ( ' solution.php ' , 'ENTRYPOINT ' );
82
+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
83
+ $ this -> getTemporaryFile ( ' exclude.txt ' );
95
84
96
85
$ exclusions = ['exclude.txt ' ];
97
86
98
- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , $ exclusions );
87
+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , $ exclusions );
99
88
100
89
self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
101
90
$ files = $ solution ->getFiles ();
@@ -107,16 +96,13 @@ public function testWithExceptions(): void
107
96
108
97
public function testWithNestedDirectories (): void
109
98
{
110
- @mkdir (sprintf ('%s/nested ' , $ this ->tempPath ), 0775 , true );
111
- @mkdir (sprintf ('%s/nested/deep ' , $ this ->tempPath ), 0775 , true );
112
-
113
- file_put_contents (sprintf ('%s/solution.php ' , $ this ->tempPath ), 'ENTRYPOINT ' );
114
- file_put_contents (sprintf ('%s/some-class.php ' , $ this ->tempPath ), 'SOME CLASS ' );
115
- file_put_contents (sprintf ('%s/composer.json ' , $ this ->tempPath ), 'COMPOSER DATA ' );
116
- file_put_contents (sprintf ('%s/nested/another-class.php ' , $ this ->tempPath ), 'ANOTHER CLASS ' );
117
- file_put_contents (sprintf ('%s/nested/deep/even-more.php ' , $ this ->tempPath ), 'EVEN MOAR ' );
99
+ $ this ->getTemporaryFile ('solution.php ' , 'ENTRYPOINT ' );
100
+ $ this ->getTemporaryFile ('some-class.php ' , 'SOME CLASS ' );
101
+ $ this ->getTemporaryFile ('composer.json ' , 'COMPOSER DATA ' );
102
+ $ this ->getTemporaryFile ('nested/another-class.php ' , 'ANOTHER CLASS ' );
103
+ $ this ->getTemporaryFile ('nested/deep/even-more.php ' , 'EVEN MOAR ' );
118
104
119
- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
105
+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
120
106
121
107
self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
122
108
$ files = $ solution ->getFiles ();
@@ -131,21 +117,16 @@ public function testWithNestedDirectories(): void
131
117
132
118
public function testExceptionsWithNestedDirectories (): void
133
119
{
134
- @mkdir (sprintf ('%s/nested ' , $ this ->tempPath ), 0775 , true );
135
- @mkdir (sprintf ('%s/nested/deep ' , $ this ->tempPath ), 0775 , true );
136
- @mkdir (sprintf ('%s/vendor ' , $ this ->tempPath ), 0775 , true );
137
- @mkdir (sprintf ('%s/vendor/somelib ' , $ this ->tempPath ), 0775 , true );
138
-
139
- file_put_contents (sprintf ('%s/solution.php ' , $ this ->tempPath ), 'ENTRYPOINT ' );
140
- file_put_contents (sprintf ('%s/some-class.php ' , $ this ->tempPath ), 'SOME CLASS ' );
141
- touch (sprintf ('%s/exclude.txt ' , $ this ->tempPath ));
142
- touch (sprintf ('%s/nested/exclude.txt ' , $ this ->tempPath ));
143
- touch (sprintf ('%s/nested/deep/exclude.txt ' , $ this ->tempPath ));
144
- touch (sprintf ('%s/vendor/somelib/app.php ' , $ this ->tempPath ));
120
+ $ this ->getTemporaryFile ('solution.php ' , 'ENTRYPOINT ' );
121
+ $ this ->getTemporaryFile ('some-class.php ' , 'SOME CLASS ' );
122
+ $ this ->getTemporaryFile ('exclude.txt ' );
123
+ $ this ->getTemporaryFile ('nested/exclude.txt ' );
124
+ $ this ->getTemporaryFile ('nested/deep/exclude.txt ' );
125
+ $ this ->getTemporaryFile ('vendor/somelib/app.php ' );
145
126
146
127
$ exclusions = ['exclude.txt ' , 'vendor ' ];
147
128
148
- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , $ exclusions );
129
+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , $ exclusions );
149
130
150
131
self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
151
132
$ files = $ solution ->getFiles ();
0 commit comments