diff --git a/src/Exercise/TemporaryDirectoryTrait.php b/src/Exercise/TemporaryDirectoryTrait.php index 49a450a4..1bb9fd90 100644 --- a/src/Exercise/TemporaryDirectoryTrait.php +++ b/src/Exercise/TemporaryDirectoryTrait.php @@ -4,6 +4,8 @@ namespace PhpSchool\PhpWorkshop\Exercise; +use PhpSchool\PhpWorkshop\Utils\System; + /** * Helper trait to use in exercises to get a temporary path * for IO stuff. @@ -18,10 +20,6 @@ trait TemporaryDirectoryTrait */ public function getTemporaryPath(): string { - return sprintf( - '%s/%s', - str_replace('\\', '/', (string) realpath(sys_get_temp_dir())), - str_replace('\\', '_', __CLASS__) - ); + return System::tempDir(str_replace('\\', '_', __CLASS__)); } } diff --git a/src/Utils/Path.php b/src/Utils/Path.php index fa47065e..d3a4db77 100644 --- a/src/Utils/Path.php +++ b/src/Utils/Path.php @@ -12,7 +12,7 @@ public static function join(string $base, string ...$parts): string [rtrim($base, '/')], array_map(function (string $part) { return trim($part, '/'); - }, $parts) + }, array_filter($parts)) ) ); } diff --git a/src/Utils/System.php b/src/Utils/System.php new file mode 100644 index 00000000..ada58ec1 --- /dev/null +++ b/src/Utils/System.php @@ -0,0 +1,26 @@ +expectException(RuntimeException::class); + $this->expectExceptionMessage('Failed to get realpath of "non_existing_file.txt"'); + + System::realpath('non_existing_file.txt'); + } + + public function testRealpathReturnsFullPath(): void + { + self::assertSame(realpath(__DIR__), System::realpath(__DIR__)); + } + + public function testTempDir(): void + { + self::assertSame(realpath(sys_get_temp_dir()), System::tempDir()); + } + + public function testTempDirWithPath(): void + { + $expect = sprintf('%s/%s', realpath(sys_get_temp_dir()), 'test'); + self::assertSame($expect, System::tempDir('test')); + } +} diff --git a/test/Utils/PathTest.php b/test/Utils/PathTest.php index 7201305a..d75eb811 100644 --- a/test/Utils/PathTest.php +++ b/test/Utils/PathTest.php @@ -43,5 +43,15 @@ public function testJoin(): void '/some/path/some-folder/file.txt', Path::join('/some/path/', '/some-folder/', '/file.txt') ); + + $this->assertEquals( + '/some/path', + Path::join('/some/path/') + ); + + $this->assertEquals( + '/some/path', + Path::join('/some/path/', '') + ); } }