| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006 |
- <?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";
- // TODO: consider using sys_get_temp_dir()
- 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);
- touch($srcContent);
- mkdir($dst);
- Path::copy_dir($src, $dst);
- $this->assertTrue(
- file_exists($dst . DIRECTORY_SEPARATOR . "foo.txt")
- );
- }
- /**
- * 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();
- }
- /**
- * Test 'Path' class 'copy_dir' method to copy a file
- *
- * @return void
- * @throws FileExistsException
- * @throws FileNotFoundException
- */
- public function testCopyWithFile(): 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 = new Path($srcContent);
- $path->copy($dst);
- $this->assertTrue(
- file_exists($dst . DIRECTORY_SEPARATOR . "foo.txt")
- );
- }
- /**
- * Test 'Path' class 'copy_dir' method to copy a directory
- *
- * @return void
- * @throws FileExistsException
- * @throws FileNotFoundException
- */
- public function testCopyWithDir(): 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 = new Path($src);
- $path->copy($dst);
- $this->assertTrue(
- file_exists($srcContent)
- );
- }
- /**
- * Test 'Path' class 'copy' method when the source file does not exist.
- *
- * @return void
- * @throws FileExistsException
- * @throws FileNotFoundException
- */
- public function testCopyFileNotExists(): 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 = new Path($src);
- $path->copy($dst);
- $this->assertTrue(
- file_exists($srcContent)
- );
- }
- /**
- * Test 'Path' class 'copy' method when the source file does not exist.
- *
- * @return void
- * @throws FileExistsException
- * @throws FileNotFoundException
- */
- public function testCopyDirDestAlreadyExists(): void
- {
- $src = self::TEMP_TEST_DIR . "/some_dir";
- $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
- $dst = self::TEMP_TEST_DIR . "/some_other_dir";
- $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
- mkdir($src);
- touch($srcContent);
- mkdir($dst);
- mkdir($dstContent);
- $this->expectException(FileExistsException::class);
- $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
- $path = new Path($srcContent);
- $path->copy($dst);
- }
- /**
- * Test 'Path' class 'copy' method when the source file does not exist.
- *
- * @return void
- * @throws FileExistsException
- * @throws FileNotFoundException
- */
- public function testCopyFileDestAlreadyExists(): void
- {
- $src = self::TEMP_TEST_DIR . "/some_dir";
- $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
- $dst = self::TEMP_TEST_DIR . "/some_other_dir";
- $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
- mkdir($src);
- touch($srcContent);
- mkdir($dst);
- touch($dstContent);
- $this->expectException(FileExistsException::class);
- $this->expectExceptionMessage("File or dir already exists : " . $dstContent);
- $path = new Path($srcContent);
- $path->copy($dst);
- }
- /**
- * Test 'Path' class 'move' method to move a file from source directory to destination directory
- * @throws FileExistsException
- */
- public function testMoveWithFile(): void
- {
- $src = self::TEMP_TEST_DIR . "/some_dir";
- $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
- $dst = self::TEMP_TEST_DIR . "/some_other_dir";
- $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
- mkdir($src);
- touch($srcContent);
- mkdir($dst);
- $path = new Path($srcContent);
- $path->move($dst);
- $this->assertFalse(
- file_exists($srcContent)
- );
- $this->assertTrue(
- file_exists($dstContent)
- );
- }
- /**
- * Test 'Path' class 'move' method to move a directory to a different location
- *
- * @return void
- * @throws FileExistsException
- */
- public function testMoveWithDir(): void
- {
- $src = self::TEMP_TEST_DIR . "/some_dir";
- $srcContent = $src . DIRECTORY_SEPARATOR . "foo.txt";
- $dst = self::TEMP_TEST_DIR . "/some_other_dir";
- $dstContent = $dst . DIRECTORY_SEPARATOR . "foo.txt";
- mkdir($src);
- touch($srcContent);
- $path = new Path($src);
- $path->move($dst);
- $this->assertFalse(
- file_exists($srcContent)
- );
- $this->assertTrue(
- file_exists($dstContent)
- );
- }
- /**
- * Test 'Path' class 'touch' method to create a file that does not exist
- *
- * @return void
- */
- public function testTouchFileDoesNotExist(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $path = new Path($src);
- $this->assertFalse(is_file($src));
- $path->touch();
- $this->assertTrue(is_file($src));
- }
- public function testTouchFileExistsNothingChange(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- $this->assertTrue(is_file($src));
- $path = new Path($src);
- $path->touch();
- $this->assertTrue(is_file($src));
- }
- public function testTouchFileExistsUpdateMtimeWithInt(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- $path = new Path($src);
- $timestamp = 1000;
- $path->touch($timestamp);
- $this->assertEquals(
- $timestamp,
- filemtime($src)
- );
- }
- public function testTouchFileExistsUpdateMtimeWithDatetime(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- $path = new Path($src);
- $dateTime = new \DateTime("2000-01-01");
- $path->touch($dateTime);
- $this->assertEquals(
- $dateTime->getTimestamp(),
- filemtime($src)
- );
- }
- public function testTouchFileExistsUpdateAtimeWithInt(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- $path = new Path($src);
- $timestamp = 1000;
- $path->touch($timestamp, $timestamp);
- $this->assertEquals(
- $timestamp,
- fileatime($src)
- );
- }
- public function testTouchFileExistsUpdateAtimeWithDatetime(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- $path = new Path($src);
- $dateTime = new \DateTime("2000-01-01");
- $path->touch($dateTime, $dateTime);
- $this->assertEquals(
- $dateTime->getTimestamp(),
- fileatime($src)
- );
- }
- public function testLastModified(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $dateTime = new \DateTime("2000-01-01");
- touch($src, $dateTime->getTimestamp());
- $path = new Path($src);
- $this->assertEquals(
- $dateTime->getTimestamp(),
- $path->lastModified()
- );
- }
- /**
- * Test 'Path' class 'size' method to get the size of the file
- *
- * @return void
- * @throws FileNotFoundException
- */
- public function testSize(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- file_put_contents($src, "nova");
- $path = new Path($src);
- $this->assertEquals(
- 4,
- $path->size()
- );
- }
- /**
- * Test 'Path' class 'size' method to get the size of the file
- *
- * @return void
- * @throws FileNotFoundException
- */
- public function testSizeNotExistingFile(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $path = new Path($src);
- $this->expectException(FileNotFoundException::class);
- $this->expectExceptionMessage("File does not exist : " . $src);
- $path->size();
- }
- public function testParent(): void
- {
- $this->assertEquals(
- '/foo/bar',
- (new Path('/foo/bar/baz'))->parent()
- );
- $this->assertEquals(
- '/foo/bar',
- (new Path('/foo/bar/baz.txt'))->parent()
- );
- $this->assertEquals(
- '/',
- (new Path('/foo'))->parent()
- );
- }
- /**
- * Test 'Path' class 'getContent' method to retrieve the content of a file
- *
- * @return void
- * @throws FileNotFoundException
- */
- public function testGetContent(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- file_put_contents($src, "nova");
- $path = new Path($src);
- $this->assertEquals(
- "nova",
- $path->getContent()
- );
- }
- /**
- * Test 'Path' class 'getContent' method to get the content of a non-existing file
- *
- * @throws FileNotFoundException If the file does not exist
- */
- public function testGetContentNotExistingFile(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $path = new Path($src);
- $this->expectException(FileNotFoundException::class);
- $this->expectExceptionMessage("File does not exist : " . $src);
- $path->getContent();
- }
- public function testPutContent(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $path = new Path($src);
- $path->putContent("ocarina");
- $this->assertEquals(
- "ocarina",
- file_get_contents($src)
- );
- }
- public function testAppendContent(): void {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- file_put_contents($src, "oca");
- $path = new Path($src);
- $path->appendContent("rina");
- $this->assertEquals(
- "ocarina",
- file_get_contents($src)
- );
- }
- }
|