104 lines
2.8 KiB
PHP
104 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Unit\Shared\Filesystem;
|
||
|
|
|
||
|
|
use Shared\Filesystem\TemporaryFileException;
|
||
|
|
use Shared\Filesystem\TemporaryFileFactory;
|
||
|
|
|
||
|
|
covers(TemporaryFileFactory::class);
|
||
|
|
|
||
|
|
it('creates a rewound temporary file with sanitized extension from bytes', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$factory = new TemporaryFileFactory();
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$file = $factory->newWithBytes('hello world', 'report.csv!@#');
|
||
|
|
$content = $file->fread(100);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($file->getFilename())->toEndWith('.csv')
|
||
|
|
->and($content)->toBe('hello world');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('creates a temporary file with empty filename and empty content', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$factory = new TemporaryFileFactory();
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$file = $factory->newWithBytes('', '');
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($file->getSize())->toBe(0)
|
||
|
|
->and($file->fread(10))->toBe('');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('copies multi-chunk SplFileObject even when source cursor is not at offset 0', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$factory = new TemporaryFileFactory();
|
||
|
|
$payload = str_repeat('A', 9500);
|
||
|
|
$source = $factory->newWithBytes($payload, 'large.txt');
|
||
|
|
$source->fseek(5000);
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$copy = $factory->newWithContentOf($source);
|
||
|
|
$copiedBytes = '';
|
||
|
|
while (! $copy->eof()) {
|
||
|
|
$chunk = $copy->fread(4096);
|
||
|
|
if ($chunk !== false) {
|
||
|
|
$copiedBytes .= $chunk;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($copiedBytes)->toBe($payload);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('creates a temporary file from stream resource rewinding non-zero cursor', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$factory = new TemporaryFileFactory();
|
||
|
|
$stream = tmpfile();
|
||
|
|
expect($stream)->not->toBeFalse();
|
||
|
|
assert(is_resource($stream));
|
||
|
|
|
||
|
|
fwrite($stream, 'stream-content');
|
||
|
|
fseek($stream, 6);
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$file = $factory->newWithStream($stream, 'upload.txt');
|
||
|
|
$content = $file->fread(100);
|
||
|
|
fclose($stream);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($file->getFilename())->toEndWith('.txt')
|
||
|
|
->and($content)->toBe('stream-content');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws TemporaryFileException when newWithStream receives non-resource', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$factory = new TemporaryFileFactory();
|
||
|
|
|
||
|
|
// WHEN & THEN
|
||
|
|
expect(static fn (): \SplFileObject => $factory->newWithStream('not-a-stream')) // @phpstan-ignore argument.type
|
||
|
|
->toThrow(TemporaryFileException::class);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('deletes tracked temporary files on factory destruction', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$factory = new TemporaryFileFactory();
|
||
|
|
$file = $factory->newWithBytes('temporary', 'cleanup.txt');
|
||
|
|
$path = $file->getRealPath();
|
||
|
|
expect($path)->not->toBeFalse();
|
||
|
|
assert(is_string($path));
|
||
|
|
expect(is_file($path))->toBeTrue();
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$factory->__destruct();
|
||
|
|
unset($file, $factory);
|
||
|
|
clearstatcache(true, $path);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect(is_file($path))->toBeFalse();
|
||
|
|
});
|