72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
||
|
|
|
||
|
|
use Domain\Models\ValueObjects\CsvImportFile;
|
||
|
|
|
||
|
|
describe('CsvImportFile', function () {
|
||
|
|
|
||
|
|
function getNewCsvImportFile(string $path, array $overrides = []): CsvImportFile
|
||
|
|
{
|
||
|
|
$data = array_merge([
|
||
|
|
'realPath' => $path,
|
||
|
|
'fileContent' => "header\nvalue",
|
||
|
|
], $overrides);
|
||
|
|
|
||
|
|
return new CsvImportFile(...$data);
|
||
|
|
}
|
||
|
|
|
||
|
|
it('can instantiate', function () {
|
||
|
|
// GIVEN
|
||
|
|
$path = tempnam(sys_get_temp_dir(), 'csv');
|
||
|
|
file_put_contents($path, 'dummy');
|
||
|
|
$content = "header\nvalue";
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$vo = new CsvImportFile(
|
||
|
|
realPath: $path,
|
||
|
|
fileContent: $content
|
||
|
|
);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($vo)->toBeInstanceOf(CsvImportFile::class);
|
||
|
|
unlink($path);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws exception if file path is not readable', function () {
|
||
|
|
// GIVEN
|
||
|
|
$invalidPath = '/invalid/path/to/file.csv';
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$action = fn () => getNewCsvImportFile($invalidPath);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($action)->toThrow(\DomainException::class);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws exception if content is empty', function () {
|
||
|
|
// GIVEN
|
||
|
|
$path = tempnam(sys_get_temp_dir(), 'csv');
|
||
|
|
file_put_contents($path, 'dummy');
|
||
|
|
$emptyContent = ' ';
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$action = function () use ($path, $emptyContent) {
|
||
|
|
try {
|
||
|
|
getNewCsvImportFile($path, [
|
||
|
|
'fileContent' => $emptyContent,
|
||
|
|
]);
|
||
|
|
} finally {
|
||
|
|
if (file_exists($path)) {
|
||
|
|
unlink($path);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($action)->toThrow(\DomainException::class);
|
||
|
|
});
|
||
|
|
});
|