Rabbi_Gerzi/backend/tests/Unit/Element/UseCases/GetElementPdfTest.php

97 lines
2.8 KiB
PHP

<?php
namespace Tests\Unit\Element\UseCases;
use App\Element\UseCases\GetElementPdf\GetElementPdf;
use App\Element\UseCases\GetElementPdf\GetElementPdfRequest;
use App\Exceptions\NotFoundException;
use Tests\Fakes\FakeFilesystem;
use Tests\TestCase;
class GetElementPdfTest extends TestCase
{
private FakeFilesystem $filesystem;
private GetElementPdf $useCase;
protected function setUp(): void
{
$this->filesystem = new FakeFilesystem();
$this->useCase = new GetElementPdf($this->filesystem);
}
public function testReturnsShortPdfFile(): void
{
$this->filesystem->put(
'element-pdfs/short/baderech.pdf',
'short-pdf-bytes',
);
$result = $this->useCase->execute(new GetElementPdfRequest(
folder: 'short',
fileName: 'baderech.pdf',
));
$this->assertSame('short-pdf-bytes', $result->getContents());
$this->assertSame('baderech.pdf', $result->getFileName());
$this->assertSame('application/pdf', $result->getMimeType());
$this->assertSame(
['element-pdfs/short/baderech.pdf'],
$this->filesystem->readPaths,
);
}
public function testReturnsLongPdfFile(): void
{
$this->filesystem->put(
'element-pdfs/long/baderech-long.pdf',
'long-pdf-bytes',
);
$result = $this->useCase->execute(new GetElementPdfRequest(
folder: 'long',
fileName: 'baderech-long.pdf',
));
$this->assertSame('long-pdf-bytes', $result->getContents());
$this->assertSame('baderech-long.pdf', $result->getFileName());
$this->assertSame('application/pdf', $result->getMimeType());
$this->assertSame(
['element-pdfs/long/baderech-long.pdf'],
$this->filesystem->readPaths,
);
}
public function testInvalidFolderThrowsNotFound(): void
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('PDF not found');
$this->useCase->execute(new GetElementPdfRequest(
folder: 'archive',
fileName: 'baderech.pdf',
));
}
public function testInvalidFileNameThrowsNotFound(): void
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('PDF not found');
$this->useCase->execute(new GetElementPdfRequest(
folder: 'short',
fileName: '../baderech.pdf',
));
}
public function testMissingStorageFileThrowsNotFound(): void
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('PDF not found');
$this->useCase->execute(new GetElementPdfRequest(
folder: 'short',
fileName: 'missing.pdf',
));
}
}