ldc-cocotte/tests/Unit/App/Cqs/Actions/Employee/ImportEmployeesInputTest.php

51 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Unit\App\Cqs\Actions\Employee;
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');
$fileObject = new \SplFileObject($filePath, 'r');
// WHEN
$input = new ImportEmployeesInput($fileObject);
// THEN
expect($input->inputStream)->toBe($fileObject);
unlink($filePath);
});
it('throws when stream is not readable', function (): void {
// GIVEN
$unreadable = new class ('/dev/null', 'r') extends \SplFileObject
{
public function isReadable(): bool
{
return false;
}
};
// WHEN + THEN
expect(fn () => new ImportEmployeesInput($unreadable))
->toThrow(\InvalidArgumentException::class, 'Input stream must be readable.');
});
it('throws when input is not a SplFileObject', function (): void {
// GIVEN
$notAStream = 'employees.csv';
// WHEN + THEN
expect(fn () => new ImportEmployeesInput($notAStream)) // @phpstan-ignore argument.type
->toThrow(\TypeError::class);
});
});