test unified uploads

This commit is contained in:
Yisroel Baum 2026-06-02 16:06:59 +03:00
parent a2c3ffad8b
commit b6da71ecd2
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
6 changed files with 144 additions and 27 deletions

View file

@ -122,7 +122,7 @@ class ElementsEndpointTest extends TestCase
parentElement: null,
));
$response = $this->patchJson("/api/elements/{$element->getId()}", [
$response = $this->postJson("/api/elements/{$element->getId()}", [
'title' => 'Updated title',
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
@ -162,7 +162,7 @@ class ElementsEndpointTest extends TestCase
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->patchJson("/api/elements/{$element->getId()}", [
->postJson("/api/elements/{$element->getId()}", [
'title' => 'Updated title',
'description' => 'Updated description',
'iconImageUrl' => '/assets/updated-icon.png',
@ -212,7 +212,7 @@ class ElementsEndpointTest extends TestCase
));
$response = $this->post(
"/api/elements/{$element->getId()}/icon-image",
"/api/elements/{$element->getId()}",
['iconImage' => $this->iconImageUpload()],
);
@ -247,7 +247,7 @@ class ElementsEndpointTest extends TestCase
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->post(
"/api/elements/{$element->getId()}/icon-image",
"/api/elements/{$element->getId()}",
['iconImage' => $this->iconImageUpload()],
);
@ -290,7 +290,7 @@ class ElementsEndpointTest extends TestCase
));
$response = $this->post(
"/api/elements/{$element->getId()}/pdf",
"/api/elements/{$element->getId()}",
['pdf' => $this->pdfUpload()],
);
@ -325,7 +325,7 @@ class ElementsEndpointTest extends TestCase
$response = $this->withCredentials()
->withUnencryptedCookie('auth_token', 'valid-token')
->post(
"/api/elements/{$element->getId()}/pdf",
"/api/elements/{$element->getId()}",
['pdf' => $this->pdfUpload()],
);
@ -369,9 +369,7 @@ class ElementsEndpointTest extends TestCase
private function iconImageUpload(): UploadedFile
{
$fixturePath = __DIR__
. '/../../../frontend/rabbi_gerzi/public/assets/'
. 'baderech-haavodah-icon.png';
$fixturePath = __DIR__ . '/../fixtures/icon.png';
return new UploadedFile(
$fixturePath,
@ -384,9 +382,7 @@ class ElementsEndpointTest extends TestCase
private function pdfUpload(): UploadedFile
{
$fixturePath = __DIR__
. '/../../../frontend/rabbi_gerzi/public/assets/pdfs/'
. 'baderech.pdf';
$fixturePath = __DIR__ . '/../fixtures/baderech.pdf';
return new UploadedFile(
$fixturePath,

View file

@ -42,11 +42,6 @@ class ElementControllerTest extends TestCase
new UpdateRichText($this->elementRepo),
new UpdatePdfPath($this->elementRepo),
new UpdateYoutubeUrl($this->elementRepo),
$this->elementRepo,
);
$this->controller = new ElementController(
$getElement,
$updateElement,
new UpdateElementIconImage(
$this->elementRepo,
$this->fileUploader,
@ -55,6 +50,11 @@ class ElementControllerTest extends TestCase
$this->elementRepo,
$this->fileUploader,
),
$this->elementRepo,
);
$this->controller = new ElementController(
$getElement,
$updateElement,
$this->fileUploader,
);
}
@ -155,7 +155,7 @@ class ElementControllerTest extends TestCase
);
}
public function testUploadIconImageReturnsElementPayload(): void
public function testUpdateWithIconImageReturnsElementPayload(): void
{
$set = $this->createSet(1, 'Baderech');
$element = $this->createElement(
@ -168,9 +168,7 @@ class ElementControllerTest extends TestCase
null,
null,
);
$fixturePath = __DIR__
. '/../../../../frontend/rabbi_gerzi/public/assets/'
. 'baderech-haavodah-icon.png';
$fixturePath = __DIR__ . '/../../fixtures/icon.png';
$request = new Request([], [], [], [], [
'iconImage' => new UploadedFile(
$fixturePath,
@ -181,7 +179,7 @@ class ElementControllerTest extends TestCase
),
]);
$response = $this->controller->uploadIconImage(
$response = $this->controller->update(
$element->getId(),
$request,
);
@ -199,7 +197,7 @@ class ElementControllerTest extends TestCase
);
}
public function testUploadPdfReturnsElementPayload(): void
public function testUpdateWithPdfReturnsElementPayload(): void
{
$set = $this->createSet(1, 'Baderech');
$element = $this->createElement(
@ -212,9 +210,7 @@ class ElementControllerTest extends TestCase
null,
null,
);
$fixturePath = __DIR__
. '/../../../../frontend/rabbi_gerzi/public/assets/pdfs/'
. 'baderech.pdf';
$fixturePath = __DIR__ . '/../../fixtures/baderech.pdf';
$request = new Request([], [], [], [], [
'pdf' => new UploadedFile(
$fixturePath,
@ -225,7 +221,7 @@ class ElementControllerTest extends TestCase
),
]);
$response = $this->controller->uploadPdf(
$response = $this->controller->update(
$element->getId(),
$request,
);

View file

@ -7,6 +7,8 @@ use App\Element\Element;
use App\Element\UseCases\UpdateElement\UpdateDescription;
use App\Element\UseCases\UpdateElement\UpdateIconImageUrl;
use App\Element\UseCases\UpdateElement\UpdateElement;
use App\Element\UseCases\UpdateElement\UpdateElementIconImage;
use App\Element\UseCases\UpdateElement\UpdateElementPdf;
use App\Element\UseCases\UpdateElement\UpdateElementRequest;
use App\Element\UseCases\UpdateElement\UpdatePdfPath;
use App\Element\UseCases\UpdateElement\UpdateRichText;
@ -16,17 +18,21 @@ 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 UpdateElementTest extends TestCase
{
private FakeElementRepository $elementRepo;
private FakeFileUploader $fileUploader;
private UpdateElement $updateElement;
protected function setUp(): void
{
$this->elementRepo = new FakeElementRepository();
$this->fileUploader = new FakeFileUploader();
$this->updateElement = new UpdateElement(
new UpdateTitle($this->elementRepo),
new UpdateDescription($this->elementRepo),
@ -34,6 +40,14 @@ class UpdateElementTest extends TestCase
new UpdateRichText($this->elementRepo),
new UpdatePdfPath($this->elementRepo),
new UpdateYoutubeUrl($this->elementRepo),
new UpdateElementIconImage(
$this->elementRepo,
$this->fileUploader,
),
new UpdateElementPdf(
$this->elementRepo,
$this->fileUploader,
),
$this->elementRepo,
);
}
@ -61,6 +75,14 @@ class UpdateElementTest extends TestCase
richText: '<p>Updated rich text</p>',
pdfPath: '/assets/pdfs/updated.pdf',
youtubeUrl: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
iconImageContents: null,
iconImageOriginalName: null,
iconImageMimeType: null,
iconImageSizeBytes: null,
pdfContents: null,
pdfOriginalName: null,
pdfMimeType: null,
pdfSizeBytes: null,
)
);
@ -115,6 +137,14 @@ class UpdateElementTest extends TestCase
richText: '<p>Updated rich text</p>',
pdfPath: '',
youtubeUrl: '',
iconImageContents: null,
iconImageOriginalName: null,
iconImageMimeType: null,
iconImageSizeBytes: null,
pdfContents: null,
pdfOriginalName: null,
pdfMimeType: null,
pdfSizeBytes: null,
)
);
@ -146,6 +176,14 @@ class UpdateElementTest extends TestCase
richText: null,
pdfPath: null,
youtubeUrl: null,
iconImageContents: null,
iconImageOriginalName: null,
iconImageMimeType: null,
iconImageSizeBytes: null,
pdfContents: null,
pdfOriginalName: null,
pdfMimeType: null,
pdfSizeBytes: null,
)
);
@ -172,6 +210,58 @@ class UpdateElementTest extends TestCase
);
}
public function testUpdatesUploadedFiles(): void
{
$set = $this->createSet(1, 'Baderech');
$element = $this->createElement(
$set,
'Original title',
'Original description',
null,
'<p>Original rich text</p>',
null,
null,
null,
);
$updatedElement = $this->updateElement->execute(
new UpdateElementRequest(
id: $element->getId(),
title: null,
description: null,
iconImageUrl: null,
richText: null,
pdfPath: null,
youtubeUrl: null,
iconImageContents: 'binary-image-bytes',
iconImageOriginalName: 'icon.png',
iconImageMimeType: 'image/png',
iconImageSizeBytes: 1024,
pdfContents: 'binary-pdf-bytes',
pdfOriginalName: 'baderech.pdf',
pdfMimeType: 'application/pdf',
pdfSizeBytes: 1024,
)
);
$this->assertSame(
'element-icons/fake-icon.png',
$updatedElement->getIconImageUrl(),
);
$this->assertSame(
'element-pdfs/fake-baderech.pdf',
$updatedElement->getPdfPath(),
);
$this->assertSame(
'element-icons',
$this->fileUploader->uploads[0]['folder'],
);
$this->assertSame(
'element-pdfs',
$this->fileUploader->uploads[1]['folder'],
);
}
public function testThrowsWhenIdMissing(): void
{
$this->expectException(BadRequestException::class);
@ -185,6 +275,14 @@ class UpdateElementTest extends TestCase
richText: '<p>Updated rich text</p>',
pdfPath: null,
youtubeUrl: null,
iconImageContents: null,
iconImageOriginalName: null,
iconImageMimeType: null,
iconImageSizeBytes: null,
pdfContents: null,
pdfOriginalName: null,
pdfMimeType: null,
pdfSizeBytes: null,
));
}
@ -201,6 +299,14 @@ class UpdateElementTest extends TestCase
richText: null,
pdfPath: null,
youtubeUrl: null,
iconImageContents: null,
iconImageOriginalName: null,
iconImageMimeType: null,
iconImageSizeBytes: null,
pdfContents: null,
pdfOriginalName: null,
pdfMimeType: null,
pdfSizeBytes: null,
));
}
@ -217,6 +323,14 @@ class UpdateElementTest extends TestCase
richText: '<p>Updated rich text</p>',
pdfPath: null,
youtubeUrl: null,
iconImageContents: null,
iconImageOriginalName: null,
iconImageMimeType: null,
iconImageSizeBytes: null,
pdfContents: null,
pdfOriginalName: null,
pdfMimeType: null,
pdfSizeBytes: null,
));
}
@ -233,6 +347,14 @@ class UpdateElementTest extends TestCase
richText: '<p>Updated rich text</p>',
pdfPath: null,
youtubeUrl: null,
iconImageContents: null,
iconImageOriginalName: null,
iconImageMimeType: null,
iconImageSizeBytes: null,
pdfContents: null,
pdfOriginalName: null,
pdfMimeType: null,
pdfSizeBytes: null,
));
}

BIN
backend/tests/fixtures/baderech.pdf vendored Normal file

Binary file not shown.

BIN
backend/tests/fixtures/icon.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB