Add PHPUnit tests for TempFile class
This commit is contained in:
parent
2bf304c5ef
commit
e000f2b359
5 changed files with 81 additions and 17 deletions
5
.idea/php.xml
generated
5
.idea/php.xml
generated
|
@ -19,6 +19,11 @@
|
|||
<component name="PhpStanOptionsConfiguration">
|
||||
<option name="transferred" value="true" />
|
||||
</component>
|
||||
<component name="PhpUnit">
|
||||
<phpunit_settings>
|
||||
<PhpUnitSettings load_method="PHPUNIT_PHAR" custom_loader_path="$USER_HOME$/phar/phpunit.phar" phpunit_phar_path="$USER_HOME$/phar/phpunit.phar" />
|
||||
</phpunit_settings>
|
||||
</component>
|
||||
<component name="PsalmOptionsConfiguration">
|
||||
<option name="transferred" value="true" />
|
||||
</component>
|
||||
|
|
3
bootstrap.php
Normal file
3
bootstrap.php
Normal file
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
require 'ncc';
|
||||
import('net.nosial.tempfile');
|
11
phpunit.xml
Normal file
11
phpunit.xml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<phpunit bootstrap="bootstrap.php">
|
||||
<testsuites>
|
||||
<testsuite name="TempFile Test Suite">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1"/>
|
||||
<server name="KERNEL_DIR" value="app/"/>
|
||||
</php>
|
||||
</phpunit>
|
62
tests/TempFile/TempFileTest.php
Normal file
62
tests/TempFile/TempFileTest.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace TempFile;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for the TempFile class.
|
||||
*/
|
||||
class TempFileTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Tests the __construct method of TempFile class.
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
// Test with default options.
|
||||
$tempFile = new TempFile(null);
|
||||
$this->assertTrue(is_file($tempFile->getFilepath()));
|
||||
$this->assertStringEndsWith('.tmp', $tempFile->getFilename());
|
||||
|
||||
// Test with custom options.
|
||||
$customOptions = [
|
||||
Options::Extension => 'txt',
|
||||
Options::Filename => 'testfile',
|
||||
Options::Prefix => 'prefix_',
|
||||
Options::Suffix => '_suffix',
|
||||
Options::RandomLength => 5,
|
||||
Options::Directory => sys_get_temp_dir(),
|
||||
];
|
||||
$tempFile = new TempFile($customOptions);
|
||||
$this->assertStringEndsWith('.txt', $tempFile->getFilename());
|
||||
$this->assertStringStartsWith('prefix_', $tempFile->getFilename());
|
||||
$this->assertStringEndsWith('_suffix.txt', $tempFile->getFilename());
|
||||
$this->assertSame($customOptions[Options::Directory], dirname($tempFile->getFilepath()));
|
||||
|
||||
// Test when a non-string and non-integer value is given to any options.
|
||||
$customOptions[Options::Prefix] = [];
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
new TempFile($customOptions);
|
||||
|
||||
// Test when an invalid option is given.
|
||||
$customOptions = ['invalid_option' => 'value'];
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
new TempFile($customOptions);
|
||||
|
||||
// Test when a directory that does not exist is given.
|
||||
$customOptions = [Options::Directory => '/nonexistent/directory'];
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
new TempFile($customOptions);
|
||||
|
||||
// Test when a directory that is not writable is given.
|
||||
$customOptions = [Options::Directory => '/'];
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
new TempFile($customOptions);
|
||||
|
||||
// Test if is writeable
|
||||
$customOptions = [Options::Directory => sys_get_temp_dir()];
|
||||
$tempFile = new TempFile($customOptions);
|
||||
$this->assertTrue(is_writable($tempFile->getFilepath()));
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
require 'ncc';
|
||||
import('net.nosial.tempfile');
|
||||
|
||||
$temp = new \TempFile\TempFile([
|
||||
\TempFile\Options::Extension => 'txt',
|
||||
\TempFile\Options::Filename => 'test',
|
||||
]);
|
||||
print(sprintf('Tempfile: %s', $temp->getFilepath()) . PHP_EOL);
|
||||
|
||||
file_put_contents($temp, 'Hello, world!');
|
||||
print(sprintf('Filesize: %s', filesize($temp->getFilepath())) . PHP_EOL);
|
||||
|
||||
sleep(10);
|
||||
print('Exiting...' . PHP_EOL);
|
||||
exit(0);
|
Loading…
Add table
Reference in a new issue