Page MenuHomeDevCentral

D2500.id6298.diff
No OneTemporary

D2500.id6298.diff

diff --git a/src/IO/Directory.php b/src/IO/Directory.php
new file mode 100644
--- /dev/null
+++ b/src/IO/Directory.php
@@ -0,0 +1,78 @@
+<?php
+
+namespace Keruald\OmniTools\IO;
+
+class Directory {
+
+ ///
+ /// Constructors
+ ///
+
+ public function __construct (
+ private string $path,
+ ) {}
+
+ ///
+ /// Getters and setters
+ ///
+
+ public function getPath () : string {
+ return $this->path;
+ }
+
+ public function setPath (string $path) : self {
+ $this->path = $path;
+
+ return $this;
+ }
+
+ ///
+ /// Directory properties methods
+ ///
+
+ public function exists () : bool {
+ return is_dir($this->path);
+ }
+
+ public function isReadable () : bool {
+ return is_readable($this->path);
+ }
+
+ public function isWritable () : bool {
+ return is_writable($this->path);
+ }
+
+ /**
+ * @return array<string, string>
+ */
+ public function getPathInfo () : array {
+ return pathinfo($this->path);
+ }
+
+ public function getParentDirectory () : string {
+ return pathinfo($this->path, PATHINFO_DIRNAME);
+ }
+
+ public function getDirectoryName () : string {
+ return pathinfo($this->path, PATHINFO_BASENAME);
+ }
+
+ ///
+ /// Search files
+ ///
+
+ /**
+ * Gets files in the directory matching a specific pattern,
+ * using the PHP glob function.
+ *
+ * @return File[]
+ */
+ public function glob (string $pattern) : array {
+ return array_map(
+ function ($file) {
+ return new File($file);
+ }, glob("$this->path/$pattern")
+ );
+ }
+
+}
diff --git a/tests/IO/DirectoryTest.php b/tests/IO/DirectoryTest.php
new file mode 100644
--- /dev/null
+++ b/tests/IO/DirectoryTest.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\IO;
+
+use Keruald\OmniTools\IO\Directory;
+use Keruald\OmniTools\IO\File;
+
+use Keruald\OmniTools\Tests\WithData;
+use PHPUnit\Framework\TestCase;
+
+class DirectoryTest extends TestCase {
+
+ use WithData;
+
+ const TEST_DIRECTORY = "MockLib";
+
+ private string $path;
+ private Directory $directory;
+
+ protected function setUp () : void {
+ $this->path = $this->getDataPath(self::TEST_DIRECTORY);
+ $this->directory = new Directory($this->path);
+ }
+
+ public function testGlob () : void {
+ $expected = [
+ new File($this->path . '/Bar.php'),
+ new File($this->path . '/Foo.php'),
+ ];
+ $actual = $this->directory->glob("*.php");
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testIsReadable () : void {
+ $this->assertTrue($this->directory->isReadable());
+ }
+
+ public function testIsWritable () : void {
+ $result = tempnam($this->path, "test-write-");
+
+ if ($result === false) {
+ $this->markTestSkipped("Can't test write operation.");
+ }
+
+ $canWrite = str_starts_with($result, $this->path);
+ unlink($result);
+
+ $this->assertSame($canWrite, $this->directory->isWritable());
+ }
+
+ public function testExists () : void {
+ $this->assertTrue($this->directory->exists());
+ }
+
+ public function testExistsWhenItDoesNot () : void {
+ $directory = new Directory("/nonexistent");
+ $this->assertFalse($directory->exists());
+ }
+
+ public function testExistsWhenItMatchesFile () : void {
+ $path = $this->getDataPath(self::TEST_DIRECTORY . "/Foo.php");
+ $directory = new Directory($path);
+
+ $this->assertFalse($directory->exists());
+ }
+
+ public function testSetPath () : void {
+ $this->directory->setPath("/bar/foo");
+ $this->assertEquals("/bar/foo", $this->directory->getPath());
+ }
+
+ public function testGetPath () : void {
+ $this->assertEquals($this->path, $this->directory->getPath());
+ }
+
+ public function testGetDirectoryName () : void {
+ $actual = $this->directory->getDirectoryName();
+ $this->assertEquals(self::TEST_DIRECTORY, $actual);
+ }
+
+ public function testGetPathInfo () : void {
+ $expected = [
+ 'dirname' => $this->getDataDirectory(),
+ 'basename' => self::TEST_DIRECTORY,
+ 'filename' => self::TEST_DIRECTORY,
+ ];
+
+ $this->assertEquals($expected, $this->directory->getPathInfo());
+ }
+
+ public function testGetParentDirectory () : void {
+ $actual = $this->directory->getParentDirectory();
+ $this->assertEquals($this->getDataDirectory(), $actual);
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 30, 13:34 (19 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2272930
Default Alt Text
D2500.id6298.diff (4 KB)

Event Timeline