44
55use InvalidArgumentException ;
66use 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 ;
99use Symfony \Component \Filesystem \Filesystem ;
1010
11- class DirectorySolutionTest extends TestCase
11+ class DirectorySolutionTest extends BaseTest
1212{
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-
2413 public function tearDown (): void
2514 {
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 ( );
2918 }
3019
3120 public function testExceptionIsThrownIfEntryPointDoesNotExist (): void
3221 {
33- touch ( sprintf ( ' %s/ some-class.php ', $ this -> tempPath ) );
22+ $ this -> getTemporaryFile ( ' some-class.php ' );
3423
3524 $ this ->expectException (InvalidArgumentException::class);
3625 $ this ->expectExceptionMessageMatches ('/Entry point: "solution.php" does not exist in: ".*"/ ' );
3726
38- DirectorySolution::fromDirectory ($ this ->tempPath );
27+ DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
3928 }
4029
4130 public function testWithDefaultEntryPoint (): void
4231 {
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 ' );
4534
46- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
35+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
4736
4837 self ::assertFalse ($ solution ->hasComposerFile ());
4938 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -56,10 +45,10 @@ public function testWithDefaultEntryPoint(): void
5645
5746 public function testWithManualEntryPoint (): void
5847 {
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 ' );
6150
62- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , [], 'index.php ' );
51+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , [], 'index.php ' );
6352
6453 self ::assertFalse ($ solution ->hasComposerFile ());
6554 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -72,11 +61,11 @@ public function testWithManualEntryPoint(): void
7261
7362 public function testHasComposerFileReturnsTrueIfPresent (): void
7463 {
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 ' );
7867
79- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
68+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
8069
8170 self ::assertTrue ($ solution ->hasComposerFile ());
8271 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
@@ -89,13 +78,13 @@ public function testHasComposerFileReturnsTrueIfPresent(): void
8978
9079 public function testWithExceptions (): void
9180 {
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 ' );
9584
9685 $ exclusions = ['exclude.txt ' ];
9786
98- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , $ exclusions );
87+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , $ exclusions );
9988
10089 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
10190 $ files = $ solution ->getFiles ();
@@ -107,16 +96,13 @@ public function testWithExceptions(): void
10796
10897 public function testWithNestedDirectories (): void
10998 {
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 ' );
118104
119- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath );
105+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () );
120106
121107 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
122108 $ files = $ solution ->getFiles ();
@@ -131,21 +117,16 @@ public function testWithNestedDirectories(): void
131117
132118 public function testExceptionsWithNestedDirectories (): void
133119 {
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 ' );
145126
146127 $ exclusions = ['exclude.txt ' , 'vendor ' ];
147128
148- $ solution = DirectorySolution::fromDirectory ($ this ->tempPath , $ exclusions );
129+ $ solution = DirectorySolution::fromDirectory ($ this ->getTemporaryDirectory () , $ exclusions );
149130
150131 self ::assertSame ('ENTRYPOINT ' , file_get_contents ($ solution ->getEntryPoint ()));
151132 $ files = $ solution ->getFiles ();
0 commit comments