Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
cba1db6a7c | ||
![]() |
52d6a45bda | ||
![]() |
e000f2b359 | ||
![]() |
2bf304c5ef | ||
![]() |
fbba022431 | ||
![]() |
1f661a9324 | ||
![]() |
817a27afc3 |
11 changed files with 340 additions and 49 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/
|
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" />
|
<path value="/etc/ncc" />
|
||||||
</include_path>
|
</include_path>
|
||||||
</component>
|
</component>
|
||||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.2" />
|
<component name="PhpProjectSharedConfiguration" php_language_level="8.3" />
|
||||||
<component name="PhpStanOptionsConfiguration">
|
<component name="PhpStanOptionsConfiguration">
|
||||||
<option name="transferred" value="true" />
|
<option name="transferred" value="true" />
|
||||||
</component>
|
</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">
|
<component name="PsalmOptionsConfiguration">
|
||||||
<option name="transferred" value="true" />
|
<option name="transferred" value="true" />
|
||||||
</component>
|
</component>
|
||||||
|
|
16
CHANGELOG.md
16
CHANGELOG.md
|
@ -5,6 +5,21 @@ 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/),
|
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).
|
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
|
## [1.1.0] - 2023-02-26
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -12,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- Replaced `$extension` parameter with `$options` parameter in `\TempFile > TempFile > __construct()`
|
- 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.
|
so that it's possible to specify the file extension and the file name prefix.
|
||||||
|
|
||||||
|
|
||||||
## [1.0.0] - 2023-02-25
|
## [1.0.0] - 2023-02-25
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
27
Makefile
27
Makefile
|
@ -1,8 +1,23 @@
|
||||||
release:
|
# Variables
|
||||||
ncc build --config="release"
|
CONFIG ?= release
|
||||||
|
LOG_LEVEL = debug
|
||||||
|
OUTDIR = build/$(CONFIG)
|
||||||
|
PACKAGE = $(OUTDIR)/net.nosial.tempfile.ncc
|
||||||
|
|
||||||
install:
|
# Default Target
|
||||||
ncc package install --package="build/release/net.nosial.tempfile.ncc" --skip-dependencies --reinstall -y
|
all: build
|
||||||
|
|
||||||
uninstall:
|
# Build Steps
|
||||||
ncc package uninstall -y --package="net.nosial.tempfile"
|
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
|
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",
|
"package": "net.nosial.tempfile",
|
||||||
"copyright": "Copyright (c) 2022-2023 Nosial",
|
"copyright": "Copyright (c) 2022-2023 Nosial",
|
||||||
"description": "TempFile is a PHP library for creating temporary files.",
|
"description": "TempFile is a PHP library for creating temporary files.",
|
||||||
"version": "1.1.0",
|
"version": "1.2.0",
|
||||||
"uuid": "910f98fe-b4c9-11ed-b13f-fdc283a6db6d"
|
"uuid": "910f98fe-b4c9-11ed-b13f-fdc283a6db6d"
|
||||||
},
|
},
|
||||||
"build": {
|
"build": {
|
||||||
|
@ -21,7 +21,16 @@
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "release",
|
"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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,38 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/** @noinspection PhpMissingFieldTypeInspection */
|
|
||||||
|
|
||||||
namespace TempFile;
|
namespace TempFile;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use ncc\Runtime;
|
use RuntimeException;
|
||||||
|
|
||||||
class TempFile
|
class TempFile
|
||||||
{
|
{
|
||||||
|
private const string RANDOM_CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An array of temporary files to be deleted on shutdown
|
* An array of temporary files to be deleted on shutdown
|
||||||
*
|
*
|
||||||
* @var string[]
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
private static $temporary_files = [];
|
private static array $temporary_files = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether the shutdown handler has been registered
|
* Indicates whether the shutdown handler has been registered
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private static $shutdown_handler_registered = false;
|
private static bool $shutdown_handler_registered = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $filename;
|
private string $filename;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $filepath;
|
private string $filepath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new temporary file with optional options:
|
* Create a new temporary file with optional options:
|
||||||
|
@ -50,15 +50,25 @@
|
||||||
foreach($options as $option => $value)
|
foreach($options as $option => $value)
|
||||||
{
|
{
|
||||||
if(!is_string($value) && !is_int($value))
|
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)));
|
throw new InvalidArgumentException(sprintf('The value for option %s must be a string or int, got %s', $option, gettype($value)));
|
||||||
|
}
|
||||||
|
|
||||||
if(!in_array($option, Options::All))
|
if(!in_array($option, Options::All))
|
||||||
|
{
|
||||||
throw new InvalidArgumentException(sprintf('The option %s is not valid', $option));
|
throw new InvalidArgumentException(sprintf('The option %s is not valid', $option));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!isset($options[Options::Extension]))
|
if(!isset($options[Options::Extension]))
|
||||||
|
{
|
||||||
$options[Options::Extension] = 'tmp';
|
$options[Options::Extension] = 'tmp';
|
||||||
|
}
|
||||||
|
|
||||||
if(!isset($options[Options::RandomLength]))
|
if(!isset($options[Options::RandomLength]))
|
||||||
|
{
|
||||||
$options[Options::RandomLength] = 8;
|
$options[Options::RandomLength] = 8;
|
||||||
|
}
|
||||||
|
|
||||||
if(isset($options[Options::Filename]))
|
if(isset($options[Options::Filename]))
|
||||||
{
|
{
|
||||||
|
@ -70,18 +80,40 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset($options[Options::Prefix]))
|
if(isset($options[Options::Prefix]))
|
||||||
|
{
|
||||||
$this->filename = $options[Options::Prefix] . $this->filename;
|
$this->filename = $options[Options::Prefix] . $this->filename;
|
||||||
|
}
|
||||||
|
|
||||||
if(isset($options[Options::Suffix]))
|
if(isset($options[Options::Suffix]))
|
||||||
|
{
|
||||||
$this->filename = $this->filename . $options[Options::Suffix];
|
$this->filename = $this->filename . $options[Options::Suffix];
|
||||||
|
}
|
||||||
|
|
||||||
$this->filename .= '.' . $options[Options::Extension];
|
$this->filename .= '.' . $options[Options::Extension];
|
||||||
$this->filename = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $this->filename);
|
$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(isset($options[Options::Directory]))
|
||||||
{
|
{
|
||||||
if(!file_exists($options[Options::Directory]) || !is_dir($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]));
|
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]))
|
if(!is_writable($options[Options::Directory]))
|
||||||
|
{
|
||||||
throw new InvalidArgumentException(sprintf('The directory %s is not 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;
|
$this->filepath = $options[Options::Directory] . DIRECTORY_SEPARATOR . $this->filename;
|
||||||
}
|
}
|
||||||
|
@ -101,7 +133,6 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$temporary_files[] = $this->filepath;
|
self::$temporary_files[] = $this->filepath;
|
||||||
|
|
||||||
if(!self::$shutdown_handler_registered && function_exists('register_shutdown_function'))
|
if(!self::$shutdown_handler_registered && function_exists('register_shutdown_function'))
|
||||||
{
|
{
|
||||||
register_shutdown_function([self::class, 'shutdownHandler']);
|
register_shutdown_function([self::class, 'shutdownHandler']);
|
||||||
|
@ -117,13 +148,14 @@
|
||||||
*/
|
*/
|
||||||
private static function randomString(int $length=8): string
|
private static function randomString(int $length=8): string
|
||||||
{
|
{
|
||||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
$charactersLength = strlen(self::RANDOM_CHARACTERS);
|
||||||
$charactersLength = strlen($characters);
|
|
||||||
$randomString = '';
|
$randomString = '';
|
||||||
|
|
||||||
for ($i = 0; $i < $length; $i++)
|
for ($i = 0; $i < $length; $i++)
|
||||||
{
|
{
|
||||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
$randomString .= self::RANDOM_CHARACTERS[rand(0, $charactersLength - 1)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $randomString;
|
return $randomString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,15 +185,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return Runtime::getDataPath('net.nosial.tempfile');
|
|
||||||
}
|
|
||||||
catch(Exception $e)
|
|
||||||
{
|
|
||||||
unset($e);
|
|
||||||
}
|
|
||||||
|
|
||||||
$local_tmp = getcwd() . DIRECTORY_SEPARATOR . 'temp';
|
$local_tmp = getcwd() . DIRECTORY_SEPARATOR . 'temp';
|
||||||
|
|
||||||
if(is_writeable(getcwd()))
|
if(is_writeable(getcwd()))
|
||||||
|
@ -174,7 +197,7 @@
|
||||||
return $local_tmp;
|
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,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