Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
cba1db6a7c | ||
![]() |
52d6a45bda | ||
![]() |
e000f2b359 | ||
![]() |
2bf304c5ef | ||
![]() |
fbba022431 | ||
![]() |
1f661a9324 | ||
![]() |
817a27afc3 | ||
![]() |
a338c7cb38 |
13 changed files with 460 additions and 75 deletions
162
.github/workflows/ncc_workflow.yml
vendored
Normal file
162
.github/workflows/ncc_workflow.yml
vendored
Normal file
|
@ -0,0 +1,162 @@
|
|||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '**'
|
||||
release:
|
||||
types: [created]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: php:8.3
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
apt update -yqq
|
||||
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
|
||||
|
||||
- name: Install phive
|
||||
run: |
|
||||
wget -O phive.phar https://phar.io/releases/phive.phar
|
||||
wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
|
||||
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
|
||||
gpg --verify phive.phar.asc phive.phar
|
||||
chmod +x phive.phar
|
||||
mv phive.phar /usr/local/bin/phive
|
||||
|
||||
- name: Install phab
|
||||
run: |
|
||||
phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C
|
||||
|
||||
- name: Install latest version of NCC
|
||||
run: |
|
||||
git clone https://git.n64.cc/nosial/ncc.git
|
||||
cd ncc
|
||||
make redist
|
||||
NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
|
||||
if [ -z "$NCC_DIR" ]; then
|
||||
echo "NCC build directory not found"
|
||||
exit 1
|
||||
fi
|
||||
php "$NCC_DIR/INSTALL" --auto
|
||||
cd .. && rm -rf ncc
|
||||
|
||||
- name: Build project
|
||||
run: |
|
||||
ncc build --config release --log-level debug
|
||||
|
||||
- name: Upload build artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: TempFile_build
|
||||
path: build/release/net.nosial.tempfile.ncc
|
||||
|
||||
check-phpunit:
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
phpunit-exists: ${{ steps.check.outputs.phpunit-exists }}
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
- name: Check for phpunit.xml
|
||||
id: check
|
||||
run: |
|
||||
if [ -f phpunit.xml ]; then
|
||||
echo "phpunit-exists=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "phpunit-exists=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
test:
|
||||
needs: [build, check-phpunit]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: php:8.3
|
||||
if: needs.check-phpunit.outputs.phpunit-exists == 'true'
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Download build artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: TempFile_build
|
||||
path: TempFile_build # Adjust this to download the artifact directly under 'TempFile_build'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
apt update -yqq
|
||||
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
|
||||
curl -sSLf -o /usr/local/bin/install-php-extensions https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions
|
||||
chmod +x /usr/local/bin/install-php-extensions
|
||||
install-php-extensions zip
|
||||
|
||||
- name: Install phive
|
||||
run: |
|
||||
wget -O phive.phar https://phar.io/releases/phive.phar
|
||||
wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
|
||||
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
|
||||
gpg --verify phive.phar.asc phive.phar
|
||||
chmod +x phive.phar
|
||||
mv phive.phar /usr/local/bin/phive
|
||||
|
||||
- name: Install phab
|
||||
run: |
|
||||
phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C
|
||||
|
||||
- name: Install latest version of NCC
|
||||
run: |
|
||||
git clone https://git.n64.cc/nosial/ncc.git
|
||||
cd ncc
|
||||
make redist
|
||||
NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
|
||||
if [ -z "$NCC_DIR" ]; then
|
||||
echo "NCC build directory not found"
|
||||
exit 1
|
||||
fi
|
||||
php "$NCC_DIR/INSTALL" --auto
|
||||
cd .. && rm -rf ncc
|
||||
|
||||
- name: Install NCC packages
|
||||
run: |
|
||||
ncc package install --package="TempFile_build/net.nosial.tempfile.ncc" --build-source --reinstall -y --log-level debug
|
||||
|
||||
- name: Run PHPUnit tests
|
||||
run: |
|
||||
wget https://phar.phpunit.de/phpunit-11.3.phar
|
||||
php phpunit-11.3.phar --configuration phpunit.xml
|
||||
|
||||
release:
|
||||
needs: [build, test]
|
||||
permissions: write-all
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: php:8.3
|
||||
if: github.event_name == 'release'
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Download build artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: TempFile_build
|
||||
path: TempFile_build
|
||||
|
||||
- name: Upload to GitHub Release
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
files: |
|
||||
TempFile_build/net.nosial.tempfile.ncc
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
build/
|
||||
/.phpunit.result.cache
|
||||
/.idea/php-test-framework.xml
|
||||
|
|
7
.idea/php.xml
generated
7
.idea/php.xml
generated
|
@ -15,10 +15,15 @@
|
|||
<path value="/etc/ncc" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.2" />
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.3" />
|
||||
<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>
|
||||
|
|
23
CHANGELOG.md
23
CHANGELOG.md
|
@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.2.0] - 2024-09-29
|
||||
|
||||
This update introduces an update for ncc 2.1+ & PHP 8.3 compatibility.
|
||||
|
||||
### Added
|
||||
- Added PHPUnit tests for TempFile class
|
||||
|
||||
### Changed
|
||||
- Refactor build system and add CI pipeline
|
||||
- Refactor TempFile class and improve error handling
|
||||
- Update PHP language level to 8.3
|
||||
- Refactor TempFile class filed types and improves filename validation
|
||||
- Updated .gitignore file
|
||||
|
||||
|
||||
## [1.1.0] - 2023-02-26
|
||||
|
||||
### Changed
|
||||
|
||||
- Replaced `$extension` parameter with `$options` parameter in `\TempFile > TempFile > __construct()`
|
||||
so that it's possible to specify the file extension and the file name prefix.
|
||||
|
||||
|
||||
## [1.0.0] - 2023-02-25
|
||||
|
||||
### Added
|
||||
|
|
27
Makefile
27
Makefile
|
@ -1,8 +1,23 @@
|
|||
release:
|
||||
ncc build --config="release"
|
||||
# Variables
|
||||
CONFIG ?= release
|
||||
LOG_LEVEL = debug
|
||||
OUTDIR = build/$(CONFIG)
|
||||
PACKAGE = $(OUTDIR)/net.nosial.tempfile.ncc
|
||||
|
||||
install:
|
||||
ncc package install --package="build/release/net.nosial.tempfile.ncc" --skip-dependencies --reinstall -y
|
||||
# Default Target
|
||||
all: build
|
||||
|
||||
uninstall:
|
||||
ncc package uninstall -y --package="net.nosial.tempfile"
|
||||
# Build Steps
|
||||
build:
|
||||
ncc build --config=$(CONFIG) --log-level $(LOG_LEVEL)
|
||||
|
||||
install: build
|
||||
ncc package install --package=$(PACKAGE) --skip-dependencies --build-source --reinstall -y --log-level $(LOG_LEVEL)
|
||||
|
||||
test: build
|
||||
phpunit
|
||||
|
||||
clean:
|
||||
rm -rf build
|
||||
|
||||
.PHONY: all build install test clean
|
36
README.md
36
README.md
|
@ -3,6 +3,18 @@
|
|||
TempFile is a very simple library used for creating temporary files without having to write code to delete them
|
||||
once you're done with them.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
<!-- TOC -->
|
||||
* [TempFile](#tempfile)
|
||||
* [Table of Contents](#table-of-contents)
|
||||
* [Installation](#installation)
|
||||
* [Compiling from source](#compiling-from-source)
|
||||
* [Usage](#usage)
|
||||
* [Options](#options)
|
||||
* [License](#license)
|
||||
<!-- TOC -->
|
||||
|
||||
## Installation
|
||||
|
||||
The library can be installed using ncc:
|
||||
|
@ -44,15 +56,20 @@ make release
|
|||
|
||||
## Usage
|
||||
|
||||
Just create a class object, optionally specifying a file extension to use (without the dot). And that's all, an
|
||||
Exception will be thrown if the file could not be created.
|
||||
Just create a class object, optionally specifying options you'd like to use.
|
||||
|
||||
```php
|
||||
require_once('ncc');
|
||||
import('net.nosial.tempfile');
|
||||
|
||||
$file1 = new TempFile();
|
||||
$file2 = new TempFile('txt');
|
||||
$file1 = new TempFile\TempFile();
|
||||
$file2 = new TempFile\TempFile([
|
||||
TempFile\Options::Extension => 'txt',
|
||||
TempFile\Options::Prefix => 'prefix_',
|
||||
TempFile\Options::Suffix => '_suffix',
|
||||
TempFile\Options::RandomLength => 8,
|
||||
TempFile\Options::Directory => '/tmp',
|
||||
]);
|
||||
```
|
||||
|
||||
You can obtain the file path by using the `getFilepath()` method or by using the object as a string.
|
||||
|
@ -66,6 +83,17 @@ Files are automatically deleted when the object is destroyed, if for some reason
|
|||
properly called, a shutdown function is automatically registered to delete all the temporary files that were
|
||||
created.
|
||||
|
||||
### Options
|
||||
|
||||
The following options are available:
|
||||
|
||||
- `TempFile\Options::Extension` - The file extension to use, defaults to 'tmp',
|
||||
- `TempFile\Options::Prefix` - The prefix to use for the file name, empty by default,
|
||||
- `TempFile\Options::Suffix` - The suffix to use for the file name, empty by default,
|
||||
- `TempFile\Options::RandomLength` - The length of the random string to use for the file name, defaults to 16,
|
||||
- `TempFile\Options::Directory` - The directory to create the file in, defaults to the system's temporary directory.
|
||||
- `TempFile\Options::Filename` - The filename to use, if specified, the random string will not be used.
|
||||
|
||||
## License
|
||||
|
||||
This library is licensed under the MIT license, see the LICENSE file
|
||||
|
|
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>
|
13
project.json
13
project.json
|
@ -12,7 +12,7 @@
|
|||
"package": "net.nosial.tempfile",
|
||||
"copyright": "Copyright (c) 2022-2023 Nosial",
|
||||
"description": "TempFile is a PHP library for creating temporary files.",
|
||||
"version": "1.0.0",
|
||||
"version": "1.2.0",
|
||||
"uuid": "910f98fe-b4c9-11ed-b13f-fdc283a6db6d"
|
||||
},
|
||||
"build": {
|
||||
|
@ -21,7 +21,16 @@
|
|||
"configurations": [
|
||||
{
|
||||
"name": "release",
|
||||
"output_path": "build/release"
|
||||
"build_type": "ncc",
|
||||
"output": "build/release/%ASSEMBLY.PACKAGE%.ncc"
|
||||
},
|
||||
{
|
||||
"name": "debug",
|
||||
"build_type": "ncc",
|
||||
"output": "build/debug/%ASSEMBLY.PACKAGE%.ncc",
|
||||
"define_constants": {
|
||||
"DEBUG": "1"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
22
src/TempFile/Options.php
Normal file
22
src/TempFile/Options.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace TempFile;
|
||||
|
||||
abstract class Options
|
||||
{
|
||||
const Extension = 'extension';
|
||||
const Filename = 'filename';
|
||||
const Prefix = 'prefix';
|
||||
const Suffix = 'suffix';
|
||||
const RandomLength = 'random_length';
|
||||
const Directory = 'directory';
|
||||
|
||||
Const All = [
|
||||
self::Extension,
|
||||
self::Filename,
|
||||
self::Prefix,
|
||||
self::Suffix,
|
||||
self::RandomLength,
|
||||
self::Directory,
|
||||
];
|
||||
}
|
|
@ -1,61 +1,126 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace TempFile;
|
||||
|
||||
use Exception;
|
||||
use ncc\Runtime;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
|
||||
class TempFile
|
||||
{
|
||||
private const string RANDOM_CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
/**
|
||||
* An array of temporary files to be deleted on shutdown
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private static $temporary_files = [];
|
||||
private static array $temporary_files = [];
|
||||
|
||||
/**
|
||||
* Indicates whether the shutdown handler has been registered
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private static $shutdown_handler_registered = false;
|
||||
private static bool $shutdown_handler_registered = false;
|
||||
|
||||
/**
|
||||
* The extension of the temporary file
|
||||
* @var string
|
||||
*/
|
||||
private string $filename;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $filepath;
|
||||
|
||||
/**
|
||||
* Create a new temporary file with optional options:
|
||||
* - extension: The extension of the file
|
||||
* - filename: The filename of the file
|
||||
* - prefix: The prefix of the file name
|
||||
* - suffix: The suffix of the file name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $extension;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $filename;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $filepath;
|
||||
|
||||
/**
|
||||
* Create a new temporary file with a given extension (default: tmp)
|
||||
*
|
||||
* @param string|null $extension
|
||||
* @param array|null $options
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(?string $extension=null)
|
||||
public function __construct(?array $options=[])
|
||||
{
|
||||
if($extension === null)
|
||||
/** @var string|int $value */
|
||||
foreach($options as $option => $value)
|
||||
{
|
||||
$extension = 'tmp';
|
||||
if(!is_string($value) && !is_int($value))
|
||||
{
|
||||
throw new InvalidArgumentException(sprintf('The value for option %s must be a string or int, got %s', $option, gettype($value)));
|
||||
}
|
||||
|
||||
$this->extension = ltrim($extension, '.');
|
||||
$this->filename = $this->randomString() . '.' . $this->extension;
|
||||
$this->filepath = $this->getTempDir() . DIRECTORY_SEPARATOR . $this->filename;
|
||||
if(!in_array($option, Options::All))
|
||||
{
|
||||
throw new InvalidArgumentException(sprintf('The option %s is not valid', $option));
|
||||
}
|
||||
}
|
||||
|
||||
if(!isset($options[Options::Extension]))
|
||||
{
|
||||
$options[Options::Extension] = 'tmp';
|
||||
}
|
||||
|
||||
if(!isset($options[Options::RandomLength]))
|
||||
{
|
||||
$options[Options::RandomLength] = 8;
|
||||
}
|
||||
|
||||
if(isset($options[Options::Filename]))
|
||||
{
|
||||
$this->filename = $options[Options::Filename];
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->filename = self::randomString((int)$options[Options::RandomLength]);
|
||||
}
|
||||
|
||||
if(isset($options[Options::Prefix]))
|
||||
{
|
||||
$this->filename = $options[Options::Prefix] . $this->filename;
|
||||
}
|
||||
|
||||
if(isset($options[Options::Suffix]))
|
||||
{
|
||||
$this->filename = $this->filename . $options[Options::Suffix];
|
||||
}
|
||||
|
||||
$this->filename .= '.' . $options[Options::Extension];
|
||||
$replaced = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $this->filename);
|
||||
|
||||
if($replaced === false)
|
||||
{
|
||||
throw new InvalidArgumentException('The filename contains invalid characters');
|
||||
}
|
||||
|
||||
if(is_array($replaced))
|
||||
{
|
||||
$replaced = implode('', $replaced);
|
||||
}
|
||||
|
||||
$this->filename = $replaced;
|
||||
|
||||
if(isset($options[Options::Directory]))
|
||||
{
|
||||
if(!file_exists($options[Options::Directory]) || !is_dir($options[Options::Directory]))
|
||||
{
|
||||
throw new InvalidArgumentException(sprintf('The directory %s does not exist or is not a a valid path', $options[Options::Directory]));
|
||||
}
|
||||
if(!is_writable($options[Options::Directory]))
|
||||
{
|
||||
throw new InvalidArgumentException(sprintf('The directory %s is not writable', $options[Options::Directory]));
|
||||
}
|
||||
|
||||
$this->filepath = $options[Options::Directory] . DIRECTORY_SEPARATOR . $this->filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->filepath = self::getTempDir() . DIRECTORY_SEPARATOR . $this->filename;
|
||||
}
|
||||
|
||||
if(!file_exists($this->filepath))
|
||||
{
|
||||
|
@ -68,7 +133,6 @@
|
|||
}
|
||||
|
||||
self::$temporary_files[] = $this->filepath;
|
||||
|
||||
if(!self::$shutdown_handler_registered && function_exists('register_shutdown_function'))
|
||||
{
|
||||
register_shutdown_function([self::class, 'shutdownHandler']);
|
||||
|
@ -79,17 +143,19 @@
|
|||
/**
|
||||
* Generates a random string of a given length
|
||||
*
|
||||
* @param int $length
|
||||
* @return string
|
||||
*/
|
||||
private function randomString(): string
|
||||
private static function randomString(int $length=8): string
|
||||
{
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$charactersLength = strlen($characters);
|
||||
$charactersLength = strlen(self::RANDOM_CHARACTERS);
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < 32; $i++)
|
||||
|
||||
for ($i = 0; $i < $length; $i++)
|
||||
{
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
$randomString .= self::RANDOM_CHARACTERS[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
|
@ -99,7 +165,7 @@
|
|||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getTempDir(): string
|
||||
private static function getTempDir(): string
|
||||
{
|
||||
if(function_exists('sys_get_temp_dir'))
|
||||
{
|
||||
|
@ -119,15 +185,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return Runtime::getDataPath('net.nosial.tempfile');
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
unset($e);
|
||||
}
|
||||
|
||||
$local_tmp = getcwd() . DIRECTORY_SEPARATOR . 'temp';
|
||||
|
||||
if(is_writeable(getcwd()))
|
||||
|
@ -140,7 +197,7 @@
|
|||
return $local_tmp;
|
||||
}
|
||||
|
||||
throw new Exception('Unable to find a suitable temporary directory');
|
||||
throw new RuntimeException('Unable to find a suitable temporary directory');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
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,14 +0,0 @@
|
|||
<?php
|
||||
|
||||
require 'ncc';
|
||||
import('net.nosial.tempfile');
|
||||
|
||||
$temp = new \TempFile\TempFile('bin');
|
||||
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