2026-11-10 18:08:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:18:42 +01:00
|
|
|
namespace Tests\Unit\App\Cqs\Actions\Employee;
|
|
|
|
|
|
2026-11-10 18:08:41 +01:00
|
|
|
use App\Cqs\Actions\Employee\ImportEmployeesInput;
|
|
|
|
|
|
|
|
|
|
describe('ImportEmployeesInput', function (): void {
|
|
|
|
|
it('accepts a readable stream', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$filePath = tempnam(sys_get_temp_dir(), 'employees-csv-');
|
|
|
|
|
expect($filePath)->not->toBeFalse();
|
|
|
|
|
file_put_contents($filePath, 'header');
|
|
|
|
|
|
2026-11-10 19:29:20 +01:00
|
|
|
$fileObject = new \SplFileObject($filePath, 'r');
|
2026-11-10 18:08:41 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-11-10 19:29:20 +01:00
|
|
|
$input = new ImportEmployeesInput($fileObject);
|
2026-11-10 18:08:41 +01:00
|
|
|
|
|
|
|
|
// THEN
|
2026-11-10 19:29:20 +01:00
|
|
|
expect($input->inputStream)->toBe($fileObject);
|
2026-11-10 18:08:41 +01:00
|
|
|
|
|
|
|
|
unlink($filePath);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws when stream is not readable', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-10 19:29:20 +01:00
|
|
|
$unreadable = new class ('/dev/null', 'r') extends \SplFileObject
|
|
|
|
|
{
|
|
|
|
|
public function isReadable(): bool
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-11-10 18:08:41 +01:00
|
|
|
|
|
|
|
|
// WHEN + THEN
|
2026-11-10 19:29:20 +01:00
|
|
|
expect(fn () => new ImportEmployeesInput($unreadable))
|
2026-11-10 18:08:41 +01:00
|
|
|
->toThrow(\InvalidArgumentException::class, 'Input stream must be readable.');
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-10 19:29:20 +01:00
|
|
|
it('throws when input is not a SplFileObject', function (): void {
|
2026-11-10 18:08:41 +01:00
|
|
|
// GIVEN
|
|
|
|
|
$notAStream = 'employees.csv';
|
|
|
|
|
|
|
|
|
|
// WHEN + THEN
|
2026-11-10 19:29:20 +01:00
|
|
|
expect(fn () => new ImportEmployeesInput($notAStream)) // @phpstan-ignore argument.type
|
|
|
|
|
->toThrow(\TypeError::class);
|
2026-11-10 18:08:41 +01:00
|
|
|
});
|
|
|
|
|
});
|