Skip to content

Commit 3594725

Browse files
committed
Use utils
1 parent e99ab19 commit 3594725

File tree

5 files changed

+91
-135
lines changed

5 files changed

+91
-135
lines changed

test/BaseTest.php

Lines changed: 9 additions & 3 deletions
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,12 @@ public function getTemporaryFile(string $filename): string
3334
}
3435

3536
@mkdir(dirname($file), 0777, true);
36-
touch($file);
37+
38+
if ($content !== null) {
39+
file_put_contents($file, $content);
40+
} else {
41+
touch($file);
42+
}
3743

3844
return $file;
3945
}

test/Solution/DirectorySolutionTest.php

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,36 @@
44

55
use InvalidArgumentException;
66
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
7+
use PhpSchool\PhpWorkshop\Utils\Path;
78
use PhpSchool\PhpWorkshop\Utils\System;
8-
use PHPUnit\Framework\TestCase;
9+
use PhpSchool\PhpWorkshopTest\BaseTest;
910
use 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();

test/Solution/InTempSolutionMapperTest.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@
77
use PhpSchool\PhpWorkshop\Solution\InTempSolutionMapper;
88
use PhpSchool\PhpWorkshop\Utils\Path;
99
use PhpSchool\PhpWorkshop\Utils\System;
10-
use PHPUnit\Framework\TestCase;
10+
use PhpSchool\PhpWorkshopTest\BaseTest;
11+
use Symfony\Component\Filesystem\Filesystem;
1112

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

1926
$mappedFile = InTempSolutionMapper::mapFile($filePath);
2027

2128
self::assertFileExists($mappedFile);
2229
self::assertNotSame($filePath, $mappedFile);
23-
self::assertStringContainsString(System::tempDir(), $mappedFile);
30+
self::assertStringContainsString(System::tempDir('php-school'), $mappedFile);
2431
}
2532

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

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

3940
self::assertDirectoryExists($mappedDir);
4041
self::assertDirectoryExists(Path::join($mappedDir, 'innerDir'));
4142
self::assertFileExists(Path::join($mappedDir, 'test.file'));
4243
self::assertFileExists(Path::join($mappedDir, 'innerDir', 'test.file'));
43-
self::assertNotSame($tempDir, $mappedDir);
44-
self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedDir);
44+
self::assertNotSame($this->getTemporaryDirectory(), $mappedDir);
45+
self::assertStringContainsString(System::tempDir('php-school'), $mappedDir);
4546
}
4647

4748
public function testMappingIsDeterministicTempDir(): void
4849
{
49-
$filePath = System::tempDir('test.file');
50-
touch($filePath);
50+
$filePath = $this->getTemporaryFile('test.file');
5151

5252
$dirName = bin2hex(random_bytes(10));
53-
$tempDir = System::tempDir($dirName);
54-
@mkdir($tempDir);
53+
$tempDir = Path::join($this->getTemporaryDirectory(), $dirName);
54+
mkdir($tempDir);
5555

5656
$fileHash = md5($filePath);
5757
$dirHash = md5($tempDir);
@@ -62,24 +62,24 @@ public function testMappingIsDeterministicTempDir(): void
6262
);
6363

6464
self::assertNotSame(
65-
InTempSolutionMapper::mapDirectory($tempDir),
66-
Path::join(System::tempDir(), 'php-school', $dirHash, $dirName)
65+
InTempSolutionMapper::mapDirectory($this->getTemporaryDirectory()),
66+
System::tempDir(Path::join('php-school', $dirHash, dirname($dirName)))
6767
);
6868
}
6969

7070
public function testContentsAreNotOverwroteIfExists(): void
7171
{
72-
$filePath = System::tempDir('test.file');
73-
file_put_contents($filePath, 'Old contents');
72+
$filePath = $this->getTemporaryFile('test.file', 'Old contents');
7473

7574
$dirName = bin2hex(random_bytes(10));
76-
$tempDir = System::tempDir($dirName);
77-
mkdir($tempDir);
78-
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');
7978

80-
$tempFilePath = Path::join(System::tempDir(), 'php-school', md5($filePath), 'test.file');
81-
$tempDirPath = Path::join(System::tempDir(), '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));
8281

82+
mkdir(dirName($tempFilePath), 0777, true);
8383
file_put_contents($tempFilePath, 'Fresh contents');
8484
mkdir($tempDirPath, 0777, true);
8585
file_put_contents(Path::join($tempDirPath, 'test.file'), 'Fresh contents');

test/Solution/SingleFileSolutionTest.php

Lines changed: 3 additions & 9 deletions
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)