| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154 |
- <?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)
- );
- }
- /**
- * Test 'Path' class 'getPermissions' method to retrieve the file permissions
- *
- * @return void
- * @throws FileNotFoundException
- */
- public function testGetPermissions(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- chmod($src, 0777);
- $path = new Path($src);
- $this->assertEquals(
- 777,
- $path->getPermissions()
- );
- }
- /**
- * Test 'Path' class 'getPermissions' method to retrieve file permissions
- * @throws FileNotFoundException
- */
- public function testGetPermissionsAlt(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- chmod($src, 0755);
- $path = new Path($src);
- $this->assertEquals(
- 755,
- $path->getPermissions()
- );
- }
- /**
- * Test 'Path' class 'getPermissions' method to retrieve file permissions
- * @throws FileNotFoundException
- */
- public function testGetPermissionsFileNotExists(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $path = new Path($src);
- $this->expectException(FileNotFoundException::class);
- $this->expectExceptionMessage("File or dir does not exist : " . $src);
- $path->getPermissions();
- }
- /**
- * Test 'Path' class 'setPermissions' method to change the permissions of a file
- * @throws FileNotFoundException
- */
- public function testSetPermissions(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- chmod($src, 0777);
- $path = new Path($src);
- $result = $path->setPermissions(0666);
- $this->assertTrue($result);
- $this->assertEquals(
- '0666',
- substr(sprintf('%o', fileperms($src)), -4)
- );
- }
- /**
- * Test 'Path' class 'setPermissions' method when file does not exist
- *
- * @throws FileNotFoundException If the file does not exist
- */
- public function testSetPermissionsFileNotExists(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $path = new Path($src);
- $this->expectException(FileNotFoundException::class);
- $this->expectExceptionMessage("File or dir does not exist : " . $src);
- $path->setPermissions(777);
- }
- public function testExistsExistingFile(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- touch($src);
- $path = new Path($src);
- $this->assertTrue(
- $path->exists()
- );
- }
- public function testExistsExistingDir(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo";
- mkdir($src);
- $path = new Path($src);
- $this->assertTrue(
- $path->exists()
- );
- }
- public function testExistsNonExistingFile(): void
- {
- $src = self::TEMP_TEST_DIR . "/foo.txt";
- $path = new Path($src);
- $this->assertFalse(
- $path->exists()
- );
- }
- public function testGlob(): void
- {
- $src = self::TEMP_TEST_DIR;
- touch($src . "/foo.txt");
- touch($src . "/bar.txt");
- touch($src . "/pic.png");
- $path = new Path($src);
- $results = [];
- foreach ($path->glob('*.txt') as $filename) {
- $results[] = (string)$filename;
- }
- $this->assertEquals(
- ['bar.txt', 'foo.txt'],
- $results
- );
- }
- }
|