175 lines
4.9 KiB
PHP
175 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Element\UseCases;
|
|
|
|
use App\Element\CreateElementDto;
|
|
use App\Element\Element;
|
|
use App\Element\UseCases\UpdateElement\UpdateShortPdf;
|
|
use App\Element\UseCases\UpdateElement\UpdateShortPdfRequest;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\NotFoundException;
|
|
use App\Set\Set as DomainSet;
|
|
use Tests\Fakes\FakeElementRepository;
|
|
use Tests\Fakes\FakeFileUploader;
|
|
use Tests\TestCase;
|
|
|
|
class UpdateShortPdfTest extends TestCase
|
|
{
|
|
private FakeElementRepository $elementRepository;
|
|
|
|
private FakeFileUploader $fileUploader;
|
|
|
|
private UpdateShortPdf $useCase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->elementRepository = new FakeElementRepository();
|
|
$this->fileUploader = new FakeFileUploader();
|
|
$this->useCase = new UpdateShortPdf(
|
|
$this->elementRepository,
|
|
$this->fileUploader,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array{
|
|
* fileContents: string,
|
|
* fileOriginalName: string,
|
|
* fileMimeType: string,
|
|
* fileSizeBytes: int
|
|
* }
|
|
*/
|
|
private function pdfArgs(): array
|
|
{
|
|
return [
|
|
'fileContents' => 'binary-pdf-bytes',
|
|
'fileOriginalName' => 'baderech.pdf',
|
|
'fileMimeType' => 'application/pdf',
|
|
'fileSizeBytes' => 1024,
|
|
];
|
|
}
|
|
|
|
public function testUploadsShortPdfAndStoresPath(): void
|
|
{
|
|
$element = $this->createElement();
|
|
|
|
$updatedElement = $this->useCase->execute(
|
|
new UpdateShortPdfRequest(
|
|
...$this->pdfArgs(),
|
|
id: $element->getId(),
|
|
)
|
|
);
|
|
|
|
$this->assertSame(
|
|
'element-pdfs/short/fake-baderech.pdf',
|
|
$updatedElement->getShortPdfPath(),
|
|
);
|
|
$this->assertNull($updatedElement->getLongPdfPath());
|
|
$storedElement = $this->elementRepository->find($element->getId());
|
|
$this->assertNotNull($storedElement);
|
|
$this->assertSame(
|
|
'element-pdfs/short/fake-baderech.pdf',
|
|
$storedElement->getShortPdfPath(),
|
|
);
|
|
$this->assertSame(
|
|
'element-pdfs/short',
|
|
$this->fileUploader->uploads[0]['folder'],
|
|
);
|
|
}
|
|
|
|
public function testNullIdThrowsBadRequest(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('id is required');
|
|
|
|
$this->useCase->execute(
|
|
new UpdateShortPdfRequest(
|
|
...$this->pdfArgs(),
|
|
id: null,
|
|
)
|
|
);
|
|
}
|
|
|
|
public function testNullPdfThrowsBadRequest(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('pdf is required');
|
|
|
|
$this->useCase->execute(
|
|
new UpdateShortPdfRequest(
|
|
id: 1,
|
|
fileContents: null,
|
|
fileOriginalName: null,
|
|
fileMimeType: null,
|
|
fileSizeBytes: null,
|
|
)
|
|
);
|
|
}
|
|
|
|
public function testDisallowedMimeThrowsBadRequest(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('pdf must be a pdf file');
|
|
|
|
$this->useCase->execute(
|
|
new UpdateShortPdfRequest(
|
|
id: 1,
|
|
fileContents: 'not-a-pdf',
|
|
fileOriginalName: 'icon.png',
|
|
fileMimeType: 'image/png',
|
|
fileSizeBytes: 1024,
|
|
)
|
|
);
|
|
}
|
|
|
|
public function testOversizedPdfThrowsBadRequest(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('pdf must be 10MB or smaller');
|
|
|
|
$this->useCase->execute(
|
|
new UpdateShortPdfRequest(
|
|
id: 1,
|
|
fileContents: 'big',
|
|
fileOriginalName: 'baderech.pdf',
|
|
fileMimeType: 'application/pdf',
|
|
fileSizeBytes: 11 * 1024 * 1024,
|
|
)
|
|
);
|
|
}
|
|
|
|
public function testMissingElementThrowsNotFound(): void
|
|
{
|
|
$this->expectException(NotFoundException::class);
|
|
$this->expectExceptionMessage('Element not found');
|
|
|
|
$this->useCase->execute(
|
|
new UpdateShortPdfRequest(
|
|
...$this->pdfArgs(),
|
|
id: 999,
|
|
)
|
|
);
|
|
}
|
|
|
|
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,
|
|
));
|
|
}
|
|
}
|