Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F6562655
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/IO/File.php b/src/IO/File.php
index e382294..96c9f21 100644
--- a/src/IO/File.php
+++ b/src/IO/File.php
@@ -1,74 +1,74 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\IO;
class File {
/**
* @var string
*/
private $path;
///
/// Constructors
///
- public function __construct (string $path = null) {
+ public function __construct (string $path) {
$this->path = $path;
}
/**
* @return static
*/
public static function from (string $path) : self {
return new static($path);
}
///
/// Getters and setters
///
public function getPath () : string {
return $this->path;
}
public function setPath (string $path) : self {
$this->path = $path;
return $this;
}
///
/// File properties methods
///
public function exists () : bool {
return file_exists($this->path);
}
public function isReadable () : bool {
return is_readable($this->path);
}
public function getPathInfo () : array {
return pathinfo($this->path);
}
public function getDirectory () : string {
return pathinfo($this->path, PATHINFO_DIRNAME);
}
public function getFileName () : string {
return pathinfo($this->path, PATHINFO_BASENAME);
}
public function getFileNameWithoutExtension () : string {
return pathinfo($this->path, PATHINFO_FILENAME);
}
public function getExtension () : string {
return pathinfo($this->path, PATHINFO_EXTENSION);
}
}
diff --git a/tests/IO/FileTest.php b/tests/IO/FileTest.php
index cd9e130..3d15e50 100644
--- a/tests/IO/FileTest.php
+++ b/tests/IO/FileTest.php
@@ -1,86 +1,101 @@
<?php
namespace Keruald\OmniTools\Tests\IO;
use Keruald\OmniTools\IO\File;
use Keruald\OmniTools\OS\CurrentOS;
use PHPUnit\Framework\TestCase;
+use TypeError;
+
class FileTest extends TestCase {
///
/// Tests
///
/**
* @dataProvider provideFilesAndDirectories
*/
public function testGetDirectory (string $filename, string $expected) : void {
if (CurrentOS::isPureWindows()) {
$this->markTestSkipped("This test is intended for UNIX systems.");
}
$this->assertSame($expected, File::from($filename)->getDirectory());
}
/**
* @dataProvider provideFilesAndFileNames
*/
public function testGetFileName (string $filename, string $expected) : void {
$this->assertSame($expected, File::from($filename)->getFileName());
}
/**
* @dataProvider provideFilesAndFileNamesWithoutExtension
*/
public function testGetFileNameWithoutExtension (string $filename, string $expected) : void {
$this->assertSame($expected, File::from($filename)->getFileNameWithoutExtension());
}
/**
* @dataProvider provideFilesAndExtensions
*/
public function testGetExtension (string $filename, string $expected) : void {
$this->assertSame($expected, File::from($filename)->getExtension());
}
+ ///
+ /// Issues
+ ///
+
+ /**
+ * @see https://devcentral.nasqueron.org/D2494
+ */
+ public function testNullPathIsNotAllowed () : void {
+ $this->expectException(TypeError::class);
+
+ $file = new File(null);
+ }
+
///
/// Data providers
///
public function provideFilesAndDirectories () : iterable {
yield ['', ''];
yield ['/', '/'];
yield ['/foo', '/'];
yield ['foo/bar', 'foo'];
yield ['foo/', '.'];
yield ['/full/path/to/foo.php', '/full/path/to'];
}
public function provideFilesAndFileNames () : iterable {
yield ['', ''];
yield ['foo', 'foo'];
yield ['foo', 'foo'];
yield ['foo.php', 'foo.php'];
yield ['/full/path/to/foo.php', 'foo.php'];
}
public function provideFilesAndFileNamesWithoutExtension () : iterable {
yield ['', ''];
yield ['foo', 'foo'];
yield ['foo.php', 'foo'];
yield ['/full/path/to/foo.php', 'foo'];
yield ['foo.tar.gz', 'foo.tar'];
}
public function provideFilesAndExtensions () : iterable {
yield ['', ''];
yield ['foo', ''];
yield ['foo.php', 'php'];
yield ['/full/path/to/foo.php', 'php'];
yield ['foo.tar.gz', 'gz'];
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Apr 2, 18:18 (12 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2534389
Default Alt Text
(4 KB)
Attached To
Mode
rKERUALD Keruald libraries development repository
Attached
Detach File
Event Timeline
Log In to Comment