Skip to content

Commit

Permalink
feat: added getFilenameWithoutExtension and Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKoehnen committed Nov 17, 2022
1 parent 019abbc commit f2bde98
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function getBasename(): string
return basename($this->filePath);
}

public function getFilenameWithoutExtension(): string
{
return pathinfo($this->getPath(), PATHINFO_FILENAME);
}

public function getExtension(): string
{
return pathinfo($this->getPath(), PATHINFO_EXTENSION);
Expand Down
36 changes: 27 additions & 9 deletions src/Test/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class FileTest extends TestCase
public function setUp(): void
{
$this->root = vfsStream::setup(self::TEST_PATH);
file_put_contents($this->root->url().'/test', 'testContent');
$this->readableFile = new File($this->root->url().'/test', 'r');
file_put_contents($this->root->url().'/test.txt', 'testContent');
$this->readableFile = new File($this->root->url().'/test.txt', 'r');
}

public function testConstruct(): void
Expand All @@ -38,7 +38,7 @@ public function testDestruct(): void

public function testGetBasename(): void
{
$this->assertEquals('test', $this->readableFile->getBasename());
$this->assertEquals('test.txt', $this->readableFile->getBasename());
}

public function testGetContent(): void
Expand All @@ -48,7 +48,25 @@ public function testGetContent(): void

public function testGetPath(): void
{
$this->assertEquals($this->root->url().'/test', $this->readableFile->getPath());
$this->assertEquals($this->root->url().'/test.txt', $this->readableFile->getPath());
}

public function testGetExtension(): void
{
$this->assertEquals('txt', $this->readableFile->getExtension());
}

public function testGetFilenameWithoutExtension(): void
{
$this->assertEquals(
'test',
$this->readableFile->getFilenameWithoutExtension()
);
}

public function testToString(): void
{
$this->assertEquals($this->root->url().'/test.txt', (string)$this->readableFile);
}

public function testGetFileHandle(): void
Expand Down Expand Up @@ -76,12 +94,12 @@ public function testMoveToDifferentFolderAbsolutePath(): void

mkdir($this->root->url().'/testFolder');
$this->readableFile->rename($this->root->url().'/testFolder/');
$this->assertSame($this->root->url().'/testFolder/test', $this->readableFile->getPath());
$this->assertFileExists($this->root->url().'/testFolder/test');
$this->assertSame($this->root->url().'/testFolder/test.txt', $this->readableFile->getPath());
$this->assertFileExists($this->root->url().'/testFolder/test.txt');

$this->readableFile->rename('../testFile');
$this->assertSame($this->root->url().'/testFile', $this->readableFile->getPath());
$this->assertFileExists($this->root->url().'/testFile');
$this->readableFile->rename('../testFile.txt');
$this->assertSame($this->root->url().'/testFile.txt', $this->readableFile->getPath());
$this->assertFileExists($this->root->url().'/testFile.txt');

$newFileHandle = $this->readableFile->getFileHandle();
$this->assertSame('testContent', stream_get_contents($newFileHandle));
Expand Down

0 comments on commit f2bde98

Please sign in to comment.