Skip to content

Commit 173b6d4

Browse files
authored
Merge pull request #210 from php-school/tmp-utils
Use directory utils
2 parents 3eaf6ae + 8941bff commit 173b6d4

5 files changed

+92
-138
lines changed

test/BaseTest.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpSchool\PhpWorkshopTest;
66

77
use PhpSchool\PhpWorkshop\Utils\Path;
8+
use PhpSchool\PhpWorkshop\Utils\System;
89
use PHPUnit\Framework\TestCase;
910
use Symfony\Component\Filesystem\Filesystem;
1011

@@ -15,7 +16,7 @@ abstract class BaseTest extends TestCase
1516
public function getTemporaryDirectory(): string
1617
{
1718
if (!$this->tempDirectory) {
18-
$tempDirectory = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName());
19+
$tempDirectory = System::tempDir($this->getName());
1920
mkdir($tempDirectory, 0777, true);
2021

2122
$this->tempDirectory = realpath($tempDirectory);
@@ -24,7 +25,7 @@ public function getTemporaryDirectory(): string
2425
return $this->tempDirectory;
2526
}
2627

27-
public function getTemporaryFile(string $filename): string
28+
public function getTemporaryFile(string $filename, string $content = null): string
2829
{
2930
$file = Path::join($this->getTemporaryDirectory(), $filename);
3031

@@ -33,7 +34,10 @@ public function getTemporaryFile(string $filename): string
3334
}
3435

3536
@mkdir(dirname($file), 0777, true);
36-
touch($file);
37+
38+
$content !== null
39+
? file_put_contents($file, $content)
40+
: touch($file);
3741

3842
return $file;
3943
}

test/Solution/DirectorySolutionTest.php

+35-54
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,35 @@
44

55
use InvalidArgumentException;
66
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;
99
use 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();

test/Solution/InTempSolutionMapperTest.php

+32-31
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,84 @@
22

33
declare(strict_types=1);
44

5-
namespace Solution;
5+
namespace PhpSchool\PhpWorkshopTest\Solution;
66

77
use PhpSchool\PhpWorkshop\Solution\InTempSolutionMapper;
88
use PhpSchool\PhpWorkshop\Utils\Path;
9-
use PHPUnit\Framework\TestCase;
9+
use PhpSchool\PhpWorkshop\Utils\System;
10+
use PhpSchool\PhpWorkshopTest\BaseTest;
11+
use Symfony\Component\Filesystem\Filesystem;
1012

11-
class InTempSolutionMapperTest extends TestCase
13+
class InTempSolutionMapperTest extends BaseTest
1214
{
15+
public function tearDown(): void
16+
{
17+
(new Filesystem())->remove(System::tempDir('php-school'));
18+
19+
parent::tearDown();
20+
}
21+
1322
public function testFileMapping(): void
1423
{
15-
$filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file');
16-
touch($filePath);
24+
$filePath = $this->getTemporaryFile('test.file');
1725

1826
$mappedFile = InTempSolutionMapper::mapFile($filePath);
1927

2028
self::assertFileExists($mappedFile);
2129
self::assertNotSame($filePath, $mappedFile);
22-
self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedFile);
30+
self::assertStringContainsString(System::tempDir('php-school'), $mappedFile);
2331
}
2432

2533
public function testDirectoryMapping(): void
2634
{
27-
$tempDir = Path::join(realpath(sys_get_temp_dir()), bin2hex(random_bytes(10)));
28-
$file = Path::join($tempDir, 'test.file');
29-
$inner = Path::join($tempDir, 'innerDir');
30-
$innerFile = Path::join($inner, 'test.file');
31-
@mkdir($tempDir);
32-
touch($file);
33-
@mkdir($inner);
34-
touch($innerFile);
35+
$this->getTemporaryFile('test.file');
36+
$this->getTemporaryFile('innerDir/test.file');
3537

36-
$mappedDir = InTempSolutionMapper::mapDirectory($tempDir);
38+
$mappedDir = InTempSolutionMapper::mapDirectory($this->getTemporaryDirectory());
3739

3840
self::assertDirectoryExists($mappedDir);
3941
self::assertDirectoryExists(Path::join($mappedDir, 'innerDir'));
4042
self::assertFileExists(Path::join($mappedDir, 'test.file'));
4143
self::assertFileExists(Path::join($mappedDir, 'innerDir', 'test.file'));
42-
self::assertNotSame($tempDir, $mappedDir);
43-
self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedDir);
44+
self::assertNotSame($this->getTemporaryDirectory(), $mappedDir);
45+
self::assertStringContainsString(System::tempDir('php-school'), $mappedDir);
4446
}
4547

4648
public function testMappingIsDeterministicTempDir(): void
4749
{
48-
$filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file');
49-
touch($filePath);
50+
$filePath = $this->getTemporaryFile('test.file');
5051

5152
$dirName = bin2hex(random_bytes(10));
52-
$tempDir = Path::join(realpath(sys_get_temp_dir()), $dirName);
53-
@mkdir($tempDir);
53+
$tempDir = Path::join($this->getTemporaryDirectory(), $dirName);
54+
mkdir($tempDir);
5455

5556
$fileHash = md5($filePath);
5657
$dirHash = md5($tempDir);
5758

5859
self::assertSame(
5960
InTempSolutionMapper::mapFile($filePath),
60-
Path::join(realpath(sys_get_temp_dir()), 'php-school', $fileHash, 'test.file')
61+
Path::join(System::tempDir(), 'php-school', $fileHash, 'test.file')
6162
);
6263

6364
self::assertNotSame(
64-
InTempSolutionMapper::mapDirectory($tempDir),
65-
Path::join(realpath(sys_get_temp_dir()), 'php-school', $dirHash, $dirName)
65+
InTempSolutionMapper::mapDirectory($this->getTemporaryDirectory()),
66+
System::tempDir(Path::join('php-school', $dirHash, dirname($dirName)))
6667
);
6768
}
6869

6970
public function testContentsAreNotOverwroteIfExists(): void
7071
{
71-
$filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file');
72-
file_put_contents($filePath, 'Old contents');
72+
$filePath = $this->getTemporaryFile('test.file', 'Old contents');
7373

7474
$dirName = bin2hex(random_bytes(10));
75-
$tempDir = Path::join(realpath(sys_get_temp_dir()), $dirName);
76-
mkdir($tempDir);
77-
file_put_contents(Path::join($tempDir, 'test.file'), 'Old contents');
75+
$tempDir = Path::join($this->getTemporaryDirectory(), $dirName);
76+
77+
$this->getTemporaryFile(Path::join($dirName, 'test.file'), 'Old contents');
7878

79-
$tempFilePath = Path::join(realpath(sys_get_temp_dir()), 'php-school', md5($filePath), 'test.file');
80-
$tempDirPath = Path::join(realpath(sys_get_temp_dir()), 'php-school', md5($tempDir), $dirName);
79+
$tempFilePath = System::tempDir(Path::join('php-school', md5($filePath), 'test.file'));
80+
$tempDirPath = System::tempDir(Path::join('php-school', md5($tempDir), $dirName));
8181

82+
mkdir(dirName($tempFilePath), 0777, true);
8283
file_put_contents($tempFilePath, 'Fresh contents');
8384
mkdir($tempDirPath, 0777, true);
8485
file_put_contents(Path::join($tempDirPath, 'test.file'), 'Fresh contents');

test/Solution/SingleFileSolutionTest.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@
33
namespace PhpSchool\PhpWorkshopTest\Solution;
44

55
use PhpSchool\PhpWorkshop\Solution\SingleFileSolution;
6-
use PHPUnit\Framework\TestCase;
6+
use PhpSchool\PhpWorkshopTest\BaseTest;
77

8-
class SingleFileSolutionTest extends TestCase
8+
class SingleFileSolutionTest extends BaseTest
99
{
1010
public function testGetters(): void
1111
{
12-
$tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName());
13-
$filePath = sprintf('%s/test.file', $tempPath);
14-
15-
@mkdir($tempPath, 0775, true);
16-
file_put_contents($filePath, 'FILE CONTENTS');
12+
$filePath = $this->getTemporaryFile('test.file', 'FILE CONTENTS');
1713

1814
$solution = SingleFileSolution::fromFile($filePath);
1915

2016
self::assertSame('FILE CONTENTS', file_get_contents($solution->getEntryPoint()));
2117
self::assertFalse($solution->hasComposerFile());
2218
self::assertCount(1, $solution->getFiles());
2319
self::assertSame('FILE CONTENTS', file_get_contents($solution->getFiles()[0]->__toString()));
24-
unlink($filePath);
25-
rmdir($tempPath);
2620
}
2721
}

0 commit comments

Comments
 (0)