| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- <?php
- namespace Path\Tests;
- use Path\Exception\FileExistsException;
- use Path\Exception\FileNotFoundException;
- use Path\Path;
- use PHPUnit\Framework\TestCase;
- // TODO: tested args should be both typed Path and string
- class PathTest extends TestCase
- {
- const TEMP_TEST_DIR = __DIR__ . "/temp";
- protected Path $pathClass;
- public function setUp(): void
- {
- mkdir(self::TEMP_TEST_DIR);
- chdir(self::TEMP_TEST_DIR);
- }
- private function rmDirs(string $dir): void {
- // Remove and replace by a proper tempdir method
- foreach(scandir($dir) as $file) {
- if ('.' === $file || '..' === $file) continue;
- if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $this->rmDirs($dir . DIRECTORY_SEPARATOR . $file);
- else unlink($dir . DIRECTORY_SEPARATOR . $file);
- }
- rmdir($dir);
- }
- public function tearDown(): void
- {
- $this->rmDirs(self::TEMP_TEST_DIR);
- chdir(__DIR__);
- }
- public function testToString(): void
- {
- $path = new Path('/foo/bar');
- $this->assertEquals('/foo/bar', $path->__toString());
- }
- /**
- * Test 'join' method.
- */
- public function testJoin(): void
- {
- // One part
- $this->assertEquals(
- '/home/user',
- Path::join('/home', 'user')
- );
- // Multiple parts
- $this->assertEquals(
- '/home/user/documents',
- Path::join('/home', 'user', 'documents')
- );
- // Absolute path passed in $parts
- $this->assertEquals(
- '/user/documents',
- Path::join('home', '/user', 'documents')
- );
- }
- /**
- * Test 'Path' class 'copy_dir' method to copy a directory
- *
- * @return void
- * @throws FileExistsException
- * @throws FileNotFoundException
- */
- public function testCopyDir(): void
- {
- $src = self::TEMP_TEST_DIR . "/some_dir";
- $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
- $dst = self::TEMP_TEST_DIR . "/some_other_dir";
- mkdir($src);
- mkdir($dst);
- touch($srcContent);
- Path::copy_dir($src, $dst);
- $this->assertTrue(
- file_exists($srcContent)
- );
- }
- /**
- * Test 'Path' class 'copy_dir' method when the destination directory does not exist
- *
- * @throws FileNotFoundException|FileExistsException
- */
- public function testCopyDirWhenDestinationDirectoryNotExists(): void
- {
- $src = self::TEMP_TEST_DIR . "/some_dir";
- $dst = self::TEMP_TEST_DIR . "/non_existing_dir";
- mkdir($src);
- touch($src . DIRECTORY_SEPARATOR . "foo.txt");
- $this->expectException(FileNotFoundException::class);
- $this->expectExceptionMessage("Directory does not exist : " . $dst);
- Path::copy_dir($src, $dst);
- }
- /**
- * Test 'Path' class 'copy_dir' method when the destination directory already exists.
- *
- * @throws FileNotFoundException|FileExistsException if the destination directory already exists.
- */
- public function testCopyDirWhenDirectoryAlreadyExistsAtDestination(): void
- {
- $src = self::TEMP_TEST_DIR . "/some_dir";
- $dst = self::TEMP_TEST_DIR . "/other_dir";
- mkdir($src);
- touch($src . DIRECTORY_SEPARATOR . "foo.txt");
- mkdir($dst);
- mkdir($dst . DIRECTORY_SEPARATOR . "some_dir");
- $this->expectException(FileExistsException::class);
- $this->expectExceptionMessage("Directory already exists : " . $dst);
- Path::copy_dir($src, $dst);
- }
- /**
- * Test `eq` method with equal paths.
- *
- * Check that the method returns the correct result when the paths are equal.
- */
- public function testEqWithEqualPaths(): void
- {
- $path = new Path('/foo/bar');
- $this->assertTrue($path->eq('/foo/bar'));
- }
- /**
- * Test `eq` method with different paths.
- *
- * Check that the method returns the correct result when the paths are different.
- */
- public function testEqWithDifferentPaths(): void
- {
- $path = new Path('/foo/bar');
- $this->assertFalse($path->eq('/foo/zzz'));
- }
- /**
- * Test `eq` method with empty path.
- *
- * Check that the method returns the correct result when the path is empty.
- */
- public function testEqWithEmptyPath(): void
- {
- $path = new Path('/foo/bar');
- $this->assertFalse($path->eq(''));
- }
- /**
- * Test the append method of the Path class.
- *
- * @return void
- */
- public function testAppend(): void
- {
- $path = new Path('/foo');
- $this->assertEquals(
- "/foo/bar",
- $path->append('bar')
- );
- // One part
- $this->assertTrue(
- (new Path('/home'))->append('user')->eq('/home/user')
- );
- // Multiple parts
- $this->assertTrue(
- (new Path('/home'))->append('user', 'documents')->eq('/home/user/documents')
- );
- // Absolute path passed in $parts
- $this->assertTrue(
- (new Path('/home'))->append('/user', 'documents')->eq('/user/documents')
- );
- }
- /**
- * Test the abspath method of the Path class.
- *
- * @return void
- */
- public function testAbsPath(): void
- {
- touch(self::TEMP_TEST_DIR . "/foo");
- chdir(self::TEMP_TEST_DIR);
- $this->assertEquals(
- self::TEMP_TEST_DIR . "/foo",
- (new Path('foo'))->abspath()
- );
- }
- /**
- * Test the abspath method of the Path class with a relative path.
- *
- * @return void
- */
- public function testAbsPathWithRelative(): void
- {
- mkdir(self::TEMP_TEST_DIR . "/foo");
- touch(self::TEMP_TEST_DIR . "/bar");
- chdir(self::TEMP_TEST_DIR . "/foo");
- $this->assertEquals(
- self::TEMP_TEST_DIR . "/bar",
- (new Path('../bar'))->abspath()
- );
- }
- /**
- * Test 'Path' class 'access' method to check existence of the file
- */
- public function testAccessCheckExistenceOfFile(): void
- {
- $filePath = self::TEMP_TEST_DIR . "/foo";
- touch($filePath);
- chmod($filePath, 777);
- $result = (new Path('foo'))->access(Path::F_OK);
- $this->assertTrue($result);
- }
- /**
- * Test 'Path' class 'access' method to check existence of the non-existent file
- */
- public function testAccessCheckExistenceOfNonExistingFile(): void
- {
- $result = (new Path('foo'))->access(Path::F_OK);
- $this->assertFalse($result);
- }
- /**
- * Test 'Path' class 'access' method to check read permission of the file
- */
- public function testAccessCheckReadPermissionOfFile(): void
- {
- $filePath = self::TEMP_TEST_DIR . "/foo";
- touch($filePath);
- chmod($filePath, 777);
- $result = (new Path('foo'))->access(Path::R_OK);
- $this->assertTrue($result);
- }
- // /**
- // * Test 'Path' class 'access' method to check read permission of the file (no permission)
- // */
- // public function testAccessCheckReadPermissionOfFileNoRight(): void
- // {
- // $filePath = self::TEMP_TEST_DIR . "/foo";
- // touch($filePath);
- // chmod($filePath, 000);
- //
- // $result = (new Path('foo'))->access(Path::R_OK);
- // $this->assertFalse($result);
- // }
- /**
- * Test 'Path' class 'access' method to check write permission of the file
- */
- public function testAccessCheckWritePermissionOfFile(): void
- {
- $filePath = self::TEMP_TEST_DIR . "/foo";
- touch($filePath);
- chmod($filePath, 777);
- $result = (new Path('foo'))->access(Path::W_OK);
- $this->assertTrue($result);
- }
- // /**
- // * Test 'Path' class 'access' method to check write permission of the file (no permission)
- // */
- // public function testAccessCheckWritePermissionOfFileNoRight(): void
- // {
- // $filePath = self::TEMP_TEST_DIR . "/foo";
- // touch($filePath);
- // chmod($filePath, 000);
- //
- // $result = (new Path('foo'))->access(Path::W_OK);
- // $this->assertFalse($result);
- // }
- /**
- * Test 'Path' class 'access' method to check execute permission of the file
- */
- public function testAccessCheckExecutePermissionOfFile(): void
- {
- $filePath = self::TEMP_TEST_DIR . "/foo";
- touch($filePath);
- chmod($filePath, 777);
- $result = (new Path('foo'))->access(Path::X_OK);
- $this->assertTrue($result);
- }
- /**
- * Test 'Path' class 'access' method to check existence of the file
- */
- public function testAccessCheckExecutePermissionOfFileNoRight(): void
- {
- $filePath = self::TEMP_TEST_DIR . "/foo";
- touch($filePath);
- chmod($filePath, 000);
- $result = (new Path('foo'))->access(Path::X_OK);
- $this->assertFalse($result);
- }
- /**
- * Test 'Path' class 'access' method with an invalid mode parameter
- */
- public function testAccessInvalidModeParameter(): void
- {
- $this->expectException(\RuntimeException::class);
- (new Path('foo'))->access(123);
- }
- /**
- * Test 'Path' class 'atime' method to get the access time of a file
- *
- * @return void
- */
- public function testATime()
- {
- touch(self::TEMP_TEST_DIR . "/foo");
- $atime = (new Path('foo'))->atime();
- $this->assertTrue(abs(time() - strtotime($atime)) <= 60);
- $this->assertMatchesRegularExpression(
- "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/",
- $atime
- );
- }
- /**
- * Test 'Path' class 'isFile' method to check if the file exists
- *
- * @return void
- */
- public function testIsFileOnActualFile(): void
- {
- touch(self::TEMP_TEST_DIR . "/foo");
- $this->assertTrue((new Path('foo'))->isFile());
- }
- /**
- * Test 'Path' class 'isFile' method to check if a non-existent file exists
- *
- * @return void
- */
- public function testIsFileOnNonExistentFile(): void
- {
- $this->assertFalse((new Path('foo'))->isFile());
- }
- /**
- * Test 'Path' class 'isFile' method to check if a file exists on the given directory
- *
- * @return void
- */
- public function testIsFileOnExistentDir(): void
- {
- mkdir(self::TEMP_TEST_DIR . "/some_dir");
- $this->assertFalse((new Path('some_dir'))->isFile());
- }
- /**
- * Test 'Path' class 'isDir' method to check if the path is a directory
- *
- * @return void
- */
- public function testIsDirOnActualDir(): void
- {
- mkdir(self::TEMP_TEST_DIR . "/some_dir");
- $this->assertTrue((new Path('some_dir'))->isDir());
- }
- /**
- * Test 'Path' class 'isDir' method to check if a file's path is a directory
- *
- * @return void
- */
- public function testIsDirOnExistentFile(): void
- {
- touch(self::TEMP_TEST_DIR . "/foo");
- $this->assertFalse((new Path('some_dir'))->isDir());
- }
- /**
- * Test 'Path' class 'isDir' method on a non-existent directory
- *
- * @return void
- */
- public function testIsDirOnNonExistentDir(): void
- {
- $this->assertFalse((new Path('some_dir'))->isDir());
- }
- /**
- * Test 'Path' class 'ext' method to get the extension of the file
- *
- * @return void
- */
- public function testFileExtension(): void
- {
- $this->assertEquals(
- 'txt',
- (new Path("foo.txt"))->ext()
- );
- }
- /**
- * Test 'Path' class 'ext' method to get the extension of the file
- *
- * @return void
- */
- public function testEmptyExtension(): void
- {
- $this->assertEquals(
- '',
- (new Path("foo"))->ext()
- );
- }
- /**
- * Test 'Path' class 'basename' method to get the base name of a file
- *
- * @return void
- */
- public function testBaseName()
- {
- touch(self::TEMP_TEST_DIR . "/foo.txt");
- $this->assertEquals(
- 'foo.txt',
- (new Path("foo.txt"))->basename()
- );
- }
- /**
- * Test 'Path' class 'name' method to get the name of the file without extension
- *
- * @return void
- */
- public function testName()
- {
- touch(self::TEMP_TEST_DIR . "/foo");
- $this->assertEquals(
- 'foo',
- (new Path("foo"))->name()
- );
- }
- /**
- * Test 'Path' class 'name' method to get the name of the file with extension
- *
- * @return void
- */
- public function testNameWithExt()
- {
- touch(self::TEMP_TEST_DIR . "/foo.txt");
- $this->assertEquals(
- 'foo',
- (new Path("foo.txt"))->name()
- );
- }
- /**
- * Test 'Path' class 'mkdir' method to create a directory
- *
- * @return void
- * @throws FileExistsException
- */
- public function testMkDir(): void
- {
- $path = new Path(self::TEMP_TEST_DIR . "/foo");
- $path->mkdir();
- $this->assertTrue($path->isDir());
- }
- /**
- * Test 'Path' class 'mkdir' method when directory already exists
- *
- * @throws FileExistsException If directory already exists
- */
- public function testMkDirExistingDir(): void {
- mkdir(self::TEMP_TEST_DIR . "/foo");
- $path = new Path(self::TEMP_TEST_DIR . "/foo");
- $this->expectException(FileExistsException::class);
- $this->expectExceptionMessage("Directory already exists : " . self::TEMP_TEST_DIR . "/foo");
-
- $path->mkdir();
- }
- /**
- * Test 'Path' class 'mkdir' method to create a directory with existing directory and recursive option
- *
- * @return void
- * @throws FileExistsException
- */
- public function testMkDirExistingDirAndRecursive(): void {
- mkdir(self::TEMP_TEST_DIR . "/foo");
- $path = new Path(self::TEMP_TEST_DIR . "/foo");
- $path->mkdir(0777, true);
- $this->assertTrue($path->isDir());
- }
- /**
- * Test 'Path' class 'mkdir' method to create a directory when a file with the same name already exists
- *
- * @return void
- * @throws FileExistsException When a file with the same name already exists
- */
- public function testMkDirExistingFile(): void {
- touch(self::TEMP_TEST_DIR . "/foo");
- $path = new Path(self::TEMP_TEST_DIR . "/foo");
- $this->expectException(FileExistsException::class);
- $this->expectExceptionMessage("A file with this name already exists : " . self::TEMP_TEST_DIR . "/foo");
- $path->mkdir();
- }
- /**
- * Test 'Path' class 'mkdir' method to create a directory recursively
- *
- * @return void
- * @throws FileExistsException
- */
- public function testMkDirRecursive(): void {
- $path = new Path(self::TEMP_TEST_DIR . "/foo/bar");
- $path->mkdir(0777, true);
- $this->assertTrue($path->isDir());
- }
- /**
- * Test 'Path' class 'delete' method to delete a file.
- *
- * @return void
- * @throws FileNotFoundException
- */
- public function testDeleteFileSuccess(): void
- {
- touch(self::TEMP_TEST_DIR . "/foo");
- $path = new Path(self::TEMP_TEST_DIR . "/foo");
- $this->assertTrue($path->isFile());
- $path->delete();
- $this->assertFalse($path->isFile());
- }
- /**
- * Test 'Path' class 'delete' method to delete a directory successfully
- *
- * @return void
- * @throws FileNotFoundException
- */
- public function testDeleteDirSuccess(): void
- {
- mkdir(self::TEMP_TEST_DIR . "/foo");
- $path = new Path(self::TEMP_TEST_DIR . "/foo");
- $this->assertTrue($path->isDir());
- $path->delete();
- $this->assertFalse($path->isDir());
- }
- /**
- * Test 'Path' class 'delete' method to delete a non-existing file or dir
- *
- * @throws FileNotFoundException When the file does not exist
- */
- public function testDeleteNonExistingFile(): void
- {
- $path = new Path(self::TEMP_TEST_DIR . "/foo");
- $this->assertFalse($path->isDir());
- $this->expectException(FileNotFoundException::class);
- $this->expectExceptionMessage("File does not exist : " . self::TEMP_TEST_DIR . "/foo");
- $path->delete();
- }
- }
|