Skip to content

Commit

Permalink
Add file.TempFile tests, update file tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
krmgns committed Aug 30, 2024
1 parent b2c3fbb commit 31504bb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
40 changes: 8 additions & 32 deletions file/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ function testConstructor() {

$this->assertInstanceOf(PathObject::class, $file);
$this->assertInstanceOf(Path::class, $file->path);
$this->assertSame($path, $file->path->name);
$this->assertSame($path, $file->getPathName());

// Argument types (string|Path).
$this->assertEquals(new File($path), new File(new Path($path)));

// Temporary files.
$file = new File('', ['temp' => true, 'tempdrop' => true]);
$file = new File('', ['temp' => true, 'tempDrop' => true]);

$this->assertFileExists($file->path->name);
$file->close(); // Removes temp file.
$this->assertFileNotExists($file->path->name);
$this->assertFileExists($file->getPathName());
$this->assertTrue($file->close()); // Delete temp file.
$this->assertFileNotExists($file->getPathName());

try {
new File("");
Expand Down Expand Up @@ -260,30 +260,6 @@ function testGetDirectory() {
$this->assertEquals(new Directory(dirname($path)), $file->getDirectory());
}

function testFromTemp() {
$file = File::fromTemp();

$this->assertInstanceOf(File::class, $file);

$this->assertFileExists($file->path->name);
$file->close(); // Removes temp file.
$this->assertFileNotExists($file->path->name);

try {
$file->delete();
} catch (FileException $e) {
$this->assertSame('No such file or directory', $e->getMessage());
$this->assertSame(error\NoFileError::class, $e->getCause()->getClass());
}

$file = File::fromTemp(drop: false);
$file->close(); // No removal.

$this->assertFileExists($file->path->name);
$this->assertTrue($file->delete());
$this->assertFileNotExists($file->path->name);
}

function testToSource() {
$file = new File($this->util->fileMake());

Expand Down Expand Up @@ -313,7 +289,7 @@ function testGetPath() {
$path = __FILE__;
$file = new File($path);

$this->assertSame($path, $file->path->name);
$this->assertSame($path, $file->getPathName());
$this->assertSame($path, $file->getPath()->getName());
$this->assertEquals(new Path($path), $file->getPath());
}
Expand Down Expand Up @@ -380,12 +356,12 @@ function testPermMethods() {
function testCheckMethods() {
$file = new File($this->util->fileMake());

$this->assertTrue($file->isTemporary());
$this->assertTrue($file->isTemp());
$this->assertFalse($file->isHidden());

$file = new File('/.test');

$this->assertFalse($file->isTemporary());
$this->assertFalse($file->isTemp());
$this->assertTrue($file->isHidden());
}

Expand Down
2 changes: 1 addition & 1 deletion file/PathInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function testCheckMethods() {
$this->assertTrue($info->isWritable());
$this->assertFalse($info->isExecutable());

$this->assertTrue($info->isTemporary());
$this->assertTrue($info->isTemp());
$this->assertFalse($info->isHidden());

$this->assertTrue($info->isAvailable());
Expand Down
41 changes: 41 additions & 0 deletions file/TempFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);
namespace test\froq\file;
use froq\file\{File, TempFile};

class TempFileTest extends \TestCase
{
function testConstructor() {
$file = new TempFile();

$this->assertInstanceOf(File::class, $file);

$this->assertFileExists($file->getPathName());
$this->assertTrue($file->close()); // Delete.
$this->assertFileNotExists($file->getPathName());
$this->assertFalse($file->delete()); // Already done.


$file = new TempFile(drop: false);
$file->close(); // No delete.

$this->assertFileExists($file->getPathName());
$this->assertTrue($file->delete());
$this->assertFileNotExists($file->getPathName());
}

function testConstructorWithOptions() {
$file = new TempFile(options: ['drop' => false]);

$this->assertFileExists($file->getPathName());
$this->assertTrue($file->close()); // No delete.
$this->assertFileExists($file->getPathName());

$this->assertTrue($file->delete()); // Delete.
$this->assertFileNotExists($file->getPathName());

$prefix = 'test-file-';
$file = new TempFile(options: ['prefix' => $prefix]);

$this->assertStringStartsWith($prefix, $file->path->filename);
}
}

0 comments on commit 31504bb

Please sign in to comment.