From b1bee8a16eaf9fd7ed7584b6a08196b8d2ba1a6c Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Fri, 26 Jun 2026 08:13:46 +0300 Subject: [PATCH] test pdf use case --- backend/tests/Fakes/FakeStoredFileReader.php | 30 ++++++ .../Controllers/ElementControllerTest.php | 38 ++++++++ .../Element/UseCases/GetElementPdfTest.php | 97 +++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 backend/tests/Fakes/FakeStoredFileReader.php create mode 100644 backend/tests/Unit/Element/UseCases/GetElementPdfTest.php diff --git a/backend/tests/Fakes/FakeStoredFileReader.php b/backend/tests/Fakes/FakeStoredFileReader.php new file mode 100644 index 0000000..cfe377f --- /dev/null +++ b/backend/tests/Fakes/FakeStoredFileReader.php @@ -0,0 +1,30 @@ + + */ + 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; + } +} diff --git a/backend/tests/Unit/Controllers/ElementControllerTest.php b/backend/tests/Unit/Controllers/ElementControllerTest.php index 1ebcb9c..99c3a1c 100644 --- a/backend/tests/Unit/Controllers/ElementControllerTest.php +++ b/backend/tests/Unit/Controllers/ElementControllerTest.php @@ -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'); diff --git a/backend/tests/Unit/Element/UseCases/GetElementPdfTest.php b/backend/tests/Unit/Element/UseCases/GetElementPdfTest.php new file mode 100644 index 0000000..bd7c0ee --- /dev/null +++ b/backend/tests/Unit/Element/UseCases/GetElementPdfTest.php @@ -0,0 +1,97 @@ +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', + )); + } +}