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

95 lines
2.7 KiB
PHP

<?php
namespace Tests\Unit\Element\UseCases;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\UseCases\UpdateElement\UpdateLongPdf;
use App\Element\UseCases\UpdateElement\UpdateLongPdfRequest;
use App\Exceptions\BadRequestException;
use App\Set\Set as DomainSet;
use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeFileUploader;
use Tests\TestCase;
class UpdateLongPdfTest extends TestCase
{
private FakeElementRepository $elementRepository;
private FakeFileUploader $fileUploader;
private UpdateLongPdf $useCase;
protected function setUp(): void
{
$this->elementRepository = new FakeElementRepository();
$this->fileUploader = new FakeFileUploader();
$this->useCase = new UpdateLongPdf(
$this->elementRepository,
$this->fileUploader,
);
}
public function testUploadsLongPdfAndStoresPath(): void
{
$element = $this->createElement();
$updatedElement = $this->useCase->execute(
new UpdateLongPdfRequest(
id: $element->getId(),
fileContents: 'binary-pdf-bytes',
fileOriginalName: 'baderech-long.pdf',
fileMimeType: 'application/pdf',
fileSizeBytes: 1024,
)
);
$this->assertNull($updatedElement->getShortPdfPath());
$this->assertSame(
'element-pdfs/long/fake-baderech-long.pdf',
$updatedElement->getLongPdfPath(),
);
$this->assertSame(
'element-pdfs/long',
$this->fileUploader->uploads[0]['folder'],
);
}
public function testDisallowedMimeThrowsBadRequest(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('pdf must be a pdf file');
$this->useCase->execute(
new UpdateLongPdfRequest(
id: 1,
fileContents: 'not-a-pdf',
fileOriginalName: 'icon.png',
fileMimeType: 'image/png',
fileSizeBytes: 1024,
)
);
}
private function createElement(): Element
{
$set = new DomainSet(
id: 1,
name: 'Baderech',
description: 'Baderech description',
iconImageUrl: '/assets/baderech-icon.png',
);
return $this->elementRepository->create(new CreateElementDto(
set: $set,
title: 'Original title',
description: 'Original description',
iconImageUrl: null,
richText: '',
shortPdfPath: null,
longPdfPath: null,
youtubeUrl: null,
parentElement: null,
));
}
}