From c6bd0b242d97de8b841b24bafadeb2951679cf2b Mon Sep 17 00:00:00 2001 From: Michael Woodward Date: Thu, 22 Apr 2021 13:17:40 +0100 Subject: [PATCH 1/2] Add System Util for path stuff :smiley: --- src/Exercise/TemporaryDirectoryTrait.php | 8 ++---- src/Utils/Path.php | 2 +- src/Utils/System.php | 26 +++++++++++++++++ test/Util/SystemTest.php | 36 ++++++++++++++++++++++++ test/Utils/PathTest.php | 10 +++++++ 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src/Utils/System.php create mode 100644 test/Util/SystemTest.php 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')); + } +} \ No newline at end of file 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/', '') + ); } } From d33784de6e4e3e67120894eb504cfd45551cedf6 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Thu, 22 Apr 2021 13:37:11 +0100 Subject: [PATCH 2/2] cs --- test/Util/SystemTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Util/SystemTest.php b/test/Util/SystemTest.php index d1971848..bd24706f 100644 --- a/test/Util/SystemTest.php +++ b/test/Util/SystemTest.php @@ -33,4 +33,4 @@ public function testTempDirWithPath(): void $expect = sprintf('%s/%s', realpath(sys_get_temp_dir()), 'test'); self::assertSame($expect, System::tempDir('test')); } -} \ No newline at end of file +}