44
55use InvalidArgumentException ;
66use PhpSchool \PhpWorkshop \Solution \DirectorySolution ;
7+ use PhpSchool \PhpWorkshop \Utils \Path ;
78use PhpSchool \PhpWorkshop \Utils \System ;
8- use PHPUnit \ Framework \ TestCase ;
9+ use PhpSchool \ PhpWorkshopTest \ BaseTest ;
910use Symfony \Component \Filesystem \Filesystem ;
1011
11- class DirectorySolutionTest extends TestCase
12+ class DirectorySolutionTest extends BaseTest
1213{
13- /**
14- * @var string
15- */
16- private $ tempPath ;
17-
18- public function setUp (): void
19- {
20- $ this ->tempPath = System::tempDir ($ this ->getName ());
21- @mkdir ($ this ->tempPath );
22- }
23-
2414 public function tearDown (): void
2515 {
26- $ fileSystem = new Filesystem ();
16+ ( new Filesystem ())-> remove (Path:: join (System:: tempDir (), ' php-school ' ) );
2717
28- $ fileSystem ->remove (System::tempDir ('php-school ' ));
29- $ fileSystem ->remove ($ this ->tempPath );
18+ parent ::tearDown ();
3019 }
3120
3221 public function testExceptionIsThrownIfEntryPointDoesNotExist (): void
3322 {
34- touch ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) );
23+ $ this -> getTemporaryFile ( ' some-class.php ' );
3524
3625 $ this ->expectException (InvalidArgumentException::class);
3726 $ this ->expectExceptionMessageMatches ('/Entry point: "solution.php" does not exist in: ".*"/ ' );
3827
39- DirectorySolution::fromDirectory ($ this ->tempPath );
28+ DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
4029 }
4130
4231 public function testWithDefaultEntryPoint (): void
4332 {
44- file_put_contents ( sprintf ( ' %s/ solution.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
45- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
33+ $ this -> getTemporaryFile ( ' solution.php ' , 'ENTRYPOINT ' );
34+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
4635
47- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
36+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
4837
4938 self ::assertFalse ($ solution ->hasComposerFile ());
5039 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -57,10 +46,10 @@ public function testWithDefaultEntryPoint(): void
5746
5847 public function testWithManualEntryPoint (): void
5948 {
60- file_put_contents ( sprintf ( ' %s/ index.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
61- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
49+ $ this -> getTemporaryFile ( ' index.php ' , 'ENTRYPOINT ' );
50+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
6251
63- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , [], 'index.php ' );
52+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , [], 'index.php ' );
6453
6554 self ::assertFalse ($ solution ->hasComposerFile ());
6655 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -73,11 +62,11 @@ public function testWithManualEntryPoint(): void
7362
7463 public function testHasComposerFileReturnsTrueIfPresent (): void
7564 {
76- file_put_contents ( sprintf ( ' %s/ solution.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
77- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
78- touch ( sprintf ( ' %s/ composer.lock ', $ this -> tempPath ) );
65+ $ this -> getTemporaryFile ( ' solution.php ' , 'ENTRYPOINT ' );
66+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
67+ $ this -> getTemporaryFile ( ' composer.lock ' );
7968
80- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
69+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
8170
8271 self ::assertTrue ($ solution ->hasComposerFile ());
8372 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -90,13 +79,13 @@ public function testHasComposerFileReturnsTrueIfPresent(): void
9079
9180 public function testWithExceptions (): void
9281 {
93- file_put_contents ( sprintf ( ' %s/ solution.php ', $ this -> tempPath ) , 'ENTRYPOINT ' );
94- file_put_contents ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) , 'SOME CLASS ' );
95- touch ( sprintf ( ' %s/ exclude.txt ', $ this -> tempPath ) );
82+ $ this -> getTemporaryFile ( ' solution.php ' , 'ENTRYPOINT ' );
83+ $ this -> getTemporaryFile ( ' some-class.php ' , 'SOME CLASS ' );
84+ $ this -> getTemporaryFile ( ' exclude.txt ' );
9685
9786 $ exclusions = ['exclude.txt ' ];
9887
99- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , $ exclusions );
88+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , $ exclusions );
10089
10190 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
10291 $ files = $ solution ->getFiles ();
@@ -108,16 +97,13 @@ public function testWithExceptions(): void
10897
10998 public function testWithNestedDirectories (): void
11099 {
111- @mkdir (sprintf ('%s/nested ' , $ this ->tempPath ), 0775 , true );
112- @mkdir (sprintf ('%s/nested/deep ' , $ this ->tempPath ), 0775 , true );
113-
114- file_put_contents (sprintf ('%s/solution.php ' , $ this ->tempPath ), 'ENTRYPOINT ' );
115- file_put_contents (sprintf ('%s/some-class.php ' , $ this ->tempPath ), 'SOME CLASS ' );
116- file_put_contents (sprintf ('%s/composer.json ' , $ this ->tempPath ), 'COMPOSER DATA ' );
117- file_put_contents (sprintf ('%s/nested/another-class.php ' , $ this ->tempPath ), 'ANOTHER CLASS ' );
118- file_put_contents (sprintf ('%s/nested/deep/even-more.php ' , $ this ->tempPath ), 'EVEN MOAR ' );
100+ $ this ->getTemporaryFile ('solution.php ' , 'ENTRYPOINT ' );
101+ $ this ->getTemporaryFile ('some-class.php ' , 'SOME CLASS ' );
102+ $ this ->getTemporaryFile ('composer.json ' , 'COMPOSER DATA ' );
103+ $ this ->getTemporaryFile ('nested/another-class.php ' , 'ANOTHER CLASS ' );
104+ $ this ->getTemporaryFile ('nested/deep/even-more.php ' , 'EVEN MOAR ' );
119105
120- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
106+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
121107
122108 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
123109 $ files = $ solution ->getFiles ();
@@ -132,21 +118,16 @@ public function testWithNestedDirectories(): void
132118
133119 public function testExceptionsWithNestedDirectories (): void
134120 {
135- @mkdir (sprintf ('%s/nested ' , $ this ->tempPath ), 0775 , true );
136- @mkdir (sprintf ('%s/nested/deep ' , $ this ->tempPath ), 0775 , true );
137- @mkdir (sprintf ('%s/vendor ' , $ this ->tempPath ), 0775 , true );
138- @mkdir (sprintf ('%s/vendor/somelib ' , $ this ->tempPath ), 0775 , true );
139-
140- file_put_contents (sprintf ('%s/solution.php ' , $ this ->tempPath ), 'ENTRYPOINT ' );
141- file_put_contents (sprintf ('%s/some-class.php ' , $ this ->tempPath ), 'SOME CLASS ' );
142- touch (sprintf ('%s/exclude.txt ' , $ this ->tempPath ));
143- touch (sprintf ('%s/nested/exclude.txt ' , $ this ->tempPath ));
144- touch (sprintf ('%s/nested/deep/exclude.txt ' , $ this ->tempPath ));
145- touch (sprintf ('%s/vendor/somelib/app.php ' , $ this ->tempPath ));
121+ $ this ->getTemporaryFile ('solution.php ' , 'ENTRYPOINT ' );
122+ $ this ->getTemporaryFile ('some-class.php ' , 'SOME CLASS ' );
123+ $ this ->getTemporaryFile ('exclude.txt ' );
124+ $ this ->getTemporaryFile ('nested/exclude.txt ' );
125+ $ this ->getTemporaryFile ('nested/deep/exclude.txt ' );
126+ $ this ->getTemporaryFile ('vendor/somelib/app.php ' );
146127
147128 $ exclusions = ['exclude.txt ' , 'vendor ' ];
148129
149- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , $ exclusions );
130+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , $ exclusions );
150131
151132 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
152133 $ files = $ solution ->getFiles ();
0 commit comments