Skip to content

Commit 57fc76e

Browse files
committed
Use directory utils
1 parent 3eaf6ae commit 57fc76e

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

test/Solution/DirectorySolutionTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace PhpSchool\PhpWorkshopTest\Solution;
44

55
use InvalidArgumentException;
6+
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
67
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
78
use PhpSchool\PhpWorkshop\Utils\Path;
9+
use PhpSchool\PhpWorkshop\Utils\System;
810
use PHPUnit\Framework\TestCase;
911
use Symfony\Component\Filesystem\Filesystem;
1012

@@ -17,14 +19,15 @@ class DirectorySolutionTest extends TestCase
1719

1820
public function setUp(): void
1921
{
20-
$this->tempPath = sprintf('%s/%s', realpath(sys_get_temp_dir()), $this->getName());
22+
$this->tempPath = System::tempDir($this->getName());
2123
@mkdir($this->tempPath);
2224
}
2325

2426
public function tearDown(): void
2527
{
2628
$fileSystem = new Filesystem();
27-
$fileSystem->remove(Path::join(realpath(sys_get_temp_dir()), 'php-school'));
29+
30+
$fileSystem->remove(System::tempDir('php-school'));
2831
$fileSystem->remove($this->tempPath);
2932
}
3033

test/Solution/InTempSolutionMapperTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
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 PhpSchool\PhpWorkshop\Utils\System;
910
use PHPUnit\Framework\TestCase;
1011

1112
class InTempSolutionMapperTest extends TestCase
1213
{
1314
public function testFileMapping(): void
1415
{
15-
$filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file');
16+
$filePath = System::tempDir('test.file');
1617
touch($filePath);
1718

1819
$mappedFile = InTempSolutionMapper::mapFile($filePath);
1920

2021
self::assertFileExists($mappedFile);
2122
self::assertNotSame($filePath, $mappedFile);
22-
self::assertStringContainsString(realpath(sys_get_temp_dir()), $mappedFile);
23+
self::assertStringContainsString(System::tempDir(), $mappedFile);
2324
}
2425

2526
public function testDirectoryMapping(): void
2627
{
27-
$tempDir = Path::join(realpath(sys_get_temp_dir()), bin2hex(random_bytes(10)));
28+
$tempDir = System::tempDir(bin2hex(random_bytes(10)));
2829
$file = Path::join($tempDir, 'test.file');
2930
$inner = Path::join($tempDir, 'innerDir');
3031
$innerFile = Path::join($inner, 'test.file');
@@ -45,39 +46,39 @@ public function testDirectoryMapping(): void
4546

4647
public function testMappingIsDeterministicTempDir(): void
4748
{
48-
$filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file');
49+
$filePath = System::tempDir('test.file');
4950
touch($filePath);
5051

5152
$dirName = bin2hex(random_bytes(10));
52-
$tempDir = Path::join(realpath(sys_get_temp_dir()), $dirName);
53+
$tempDir = System::tempDir($dirName);
5354
@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(
6465
InTempSolutionMapper::mapDirectory($tempDir),
65-
Path::join(realpath(sys_get_temp_dir()), 'php-school', $dirHash, $dirName)
66+
Path::join(System::tempDir(), 'php-school', $dirHash, $dirName)
6667
);
6768
}
6869

6970
public function testContentsAreNotOverwroteIfExists(): void
7071
{
71-
$filePath = Path::join(realpath(sys_get_temp_dir()), 'test.file');
72+
$filePath = System::tempDir('test.file');
7273
file_put_contents($filePath, 'Old contents');
7374

7475
$dirName = bin2hex(random_bytes(10));
75-
$tempDir = Path::join(realpath(sys_get_temp_dir()), $dirName);
76+
$tempDir = System::tempDir($dirName);
7677
mkdir($tempDir);
7778
file_put_contents(Path::join($tempDir, 'test.file'), 'Old contents');
7879

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

8283
file_put_contents($tempFilePath, 'Fresh contents');
8384
mkdir($tempDirPath, 0777, true);

0 commit comments

Comments
 (0)