test pdf use case

This commit is contained in:
Yisroel Baum 2026-06-26 08:13:46 +03:00
parent 208c683dac
commit b1bee8a16e
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 165 additions and 0 deletions

View file

@ -0,0 +1,30 @@
<?php
namespace Tests\Fakes;
use App\Shared\Files\StoredFileReader;
class FakeStoredFileReader implements StoredFileReader
{
/**
* @var array<string, string>
*/
private array $filesByPath = [];
/**
* @var string[]
*/
public array $readPaths = [];
public function put(string $path, string $contents): void
{
$this->filesByPath[$path] = $contents;
}
public function read(string $path): ?string
{
$this->readPaths[] = $path;
return $this->filesByPath[$path] ?? null;
}
}

View file

@ -8,6 +8,7 @@ use App\Element\Element;
use App\Element\UseCases\CreateChildElement\CreateChildElement;
use App\Element\UseCases\DeleteChildElement\DeleteChildElement;
use App\Element\UseCases\GetElement\GetElement;
use App\Element\UseCases\GetElementPdf\GetElementPdf;
use App\Element\UseCases\ReorderChildElements\ReorderChildElements;
use App\Element\UseCases\UpdateElement\UpdateDescription;
use App\Element\UseCases\UpdateElement\UpdateElement;
@ -25,6 +26,7 @@ use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeFileUploader;
use Tests\Fakes\FakeStoredFileReader;
use Tests\TestCase;
class ElementControllerTest extends TestCase
@ -35,10 +37,13 @@ class ElementControllerTest extends TestCase
private FakeFileUploader $fileUploader;
private FakeStoredFileReader $storedFileReader;
protected function setUp(): void
{
$this->elementRepo = new FakeElementRepository();
$this->fileUploader = new FakeFileUploader();
$this->storedFileReader = new FakeStoredFileReader();
$getElement = new GetElement($this->elementRepo);
$updateElement = new UpdateElement(
new UpdateTitle($this->elementRepo),
@ -68,6 +73,7 @@ class ElementControllerTest extends TestCase
new DeleteChildElement($this->elementRepo, $this->fileUploader),
new ReorderChildElements($this->elementRepo),
$updateElement,
new GetElementPdf($this->storedFileReader),
$this->fileUploader,
);
}
@ -244,6 +250,38 @@ class ElementControllerTest extends TestCase
);
}
public function testShowPdfReturnsPdfResponse(): void
{
$this->storedFileReader->put(
'element-pdfs/short/baderech.pdf',
'pdf-bytes',
);
$response = $this->controller->showPdf('short', 'baderech.pdf');
$this->assertEquals(200, $response->getStatusCode());
$this->assertSame('pdf-bytes', $response->getContent());
$this->assertSame(
'application/pdf',
$response->headers->get('Content-Type'),
);
$this->assertSame(
'inline; filename="baderech.pdf"',
$response->headers->get('Content-Disposition'),
);
}
public function testShowPdfReturns404WhenPdfDoesNotExist(): void
{
$response = $this->controller->showPdf('short', 'missing.pdf');
$this->assertEquals(404, $response->getStatusCode());
$this->assertSame(
['error' => 'PDF not found'],
json_decode($response->getContent(), true),
);
}
public function testUpdateWithIconImageReturnsElementPayload(): void
{
$set = $this->createSet(1, 'Baderech');

View file

@ -0,0 +1,97 @@
<?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\FakeStoredFileReader;
use Tests\TestCase;
class GetElementPdfTest extends TestCase
{
private FakeStoredFileReader $storedFileReader;
private GetElementPdf $useCase;
protected function setUp(): void
{
$this->storedFileReader = new FakeStoredFileReader();
$this->useCase = new GetElementPdf($this->storedFileReader);
}
public function testReturnsShortPdfFile(): void
{
$this->storedFileReader->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->storedFileReader->readPaths,
);
}
public function testReturnsLongPdfFile(): void
{
$this->storedFileReader->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->storedFileReader->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',
));
}
}