242 lines
7.7 KiB
PHP
242 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Element\UseCases;
|
|
|
|
use App\Element\CreateElementDto;
|
|
use App\Element\Element;
|
|
use App\Element\UseCases\UpdateElement\UpdateDescription;
|
|
use App\Element\UseCases\UpdateElement\UpdateDescriptionRequest;
|
|
use App\Element\UseCases\UpdateElement\UpdateIconImageUrl;
|
|
use App\Element\UseCases\UpdateElement\UpdateIconImageUrlRequest;
|
|
use App\Element\UseCases\UpdateElement\UpdateLongPdfPath;
|
|
use App\Element\UseCases\UpdateElement\UpdateLongPdfPathRequest;
|
|
use App\Element\UseCases\UpdateElement\UpdateShortPdfPath;
|
|
use App\Element\UseCases\UpdateElement\UpdateShortPdfPathRequest;
|
|
use App\Element\UseCases\UpdateElement\UpdateRichText;
|
|
use App\Element\UseCases\UpdateElement\UpdateRichTextRequest;
|
|
use App\Element\UseCases\UpdateElement\UpdateTitle;
|
|
use App\Element\UseCases\UpdateElement\UpdateTitleRequest;
|
|
use App\Element\UseCases\UpdateElement\UpdateYoutubeUrl;
|
|
use App\Element\UseCases\UpdateElement\UpdateYoutubeUrlRequest;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Set\Set as DomainSet;
|
|
use Tests\Fakes\FakeElementRepository;
|
|
use Tests\Fakes\FakeFileUploader;
|
|
use Tests\TestCase;
|
|
|
|
class UpdateElementFieldsTest extends TestCase
|
|
{
|
|
private FakeElementRepository $elementRepo;
|
|
|
|
private FakeFileUploader $fileUploader;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->elementRepo = new FakeElementRepository();
|
|
$this->fileUploader = new FakeFileUploader();
|
|
}
|
|
|
|
public function testUpdateTitleUpdatesOnlyTitle(): void
|
|
{
|
|
$element = $this->createFilledElement();
|
|
$updateTitle = new UpdateTitle($this->elementRepo);
|
|
|
|
$updatedElement = $updateTitle->execute(new UpdateTitleRequest(
|
|
id: $element->getId(),
|
|
title: 'Updated title',
|
|
));
|
|
|
|
$this->assertSame('Updated title', $updatedElement->getTitle());
|
|
$this->assertSame(
|
|
$element->getDescription(),
|
|
$updatedElement->getDescription(),
|
|
);
|
|
$this->assertSame(
|
|
$element->getRichText(),
|
|
$updatedElement->getRichText(),
|
|
);
|
|
}
|
|
|
|
public function testUpdateTitleRejectsBlankTitle(): void
|
|
{
|
|
$this->createFilledElement();
|
|
$updateTitle = new UpdateTitle($this->elementRepo);
|
|
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('title is required');
|
|
|
|
$updateTitle->execute(new UpdateTitleRequest(id: 1, title: ''));
|
|
}
|
|
|
|
public function testUpdateDescriptionUpdatesOnlyDescription(): void
|
|
{
|
|
$element = $this->createFilledElement();
|
|
$updateDescription = new UpdateDescription($this->elementRepo);
|
|
|
|
$updatedElement = $updateDescription->execute(
|
|
new UpdateDescriptionRequest(
|
|
id: $element->getId(),
|
|
description: 'Updated description',
|
|
)
|
|
);
|
|
|
|
$this->assertSame(
|
|
'Updated description',
|
|
$updatedElement->getDescription(),
|
|
);
|
|
$this->assertSame($element->getTitle(), $updatedElement->getTitle());
|
|
$this->assertSame(
|
|
$element->getYoutubeUrl(),
|
|
$updatedElement->getYoutubeUrl(),
|
|
);
|
|
}
|
|
|
|
public function testUpdateIconImageUrlClearsEmptyString(): void
|
|
{
|
|
$element = $this->createFilledElement();
|
|
$updateIconImageUrl = new UpdateIconImageUrl(
|
|
$this->elementRepo,
|
|
$this->fileUploader,
|
|
);
|
|
|
|
$updatedElement = $updateIconImageUrl->execute(
|
|
new UpdateIconImageUrlRequest(
|
|
id: $element->getId(),
|
|
iconImageUrl: '',
|
|
)
|
|
);
|
|
|
|
$this->assertNull($updatedElement->getIconImageUrl());
|
|
$this->assertSame($element->getTitle(), $updatedElement->getTitle());
|
|
$this->assertSame(
|
|
['/assets/original-icon.png'],
|
|
$this->fileUploader->deletedPaths,
|
|
);
|
|
}
|
|
|
|
public function testUpdateRichTextUpdatesOnlyRichText(): void
|
|
{
|
|
$element = $this->createFilledElement();
|
|
$updateRichText = new UpdateRichText($this->elementRepo);
|
|
|
|
$updatedElement = $updateRichText->execute(
|
|
new UpdateRichTextRequest(
|
|
id: $element->getId(),
|
|
richText: '<p>Updated rich text</p>',
|
|
)
|
|
);
|
|
|
|
$this->assertSame(
|
|
'<p>Updated rich text</p>',
|
|
$updatedElement->getRichText(),
|
|
);
|
|
$this->assertSame(
|
|
$element->getDescription(),
|
|
$updatedElement->getDescription(),
|
|
);
|
|
}
|
|
|
|
public function testUpdateShortPdfPathClearsEmptyString(): void
|
|
{
|
|
$element = $this->createFilledElement();
|
|
$updateShortPdfPath = new UpdateShortPdfPath(
|
|
$this->elementRepo,
|
|
$this->fileUploader,
|
|
);
|
|
|
|
$updatedElement = $updateShortPdfPath->execute(
|
|
new UpdateShortPdfPathRequest(
|
|
id: $element->getId(),
|
|
shortPdfPath: '',
|
|
)
|
|
);
|
|
|
|
$this->assertNull($updatedElement->getShortPdfPath());
|
|
$this->assertSame(
|
|
'/assets/pdfs/original-long.pdf',
|
|
$updatedElement->getLongPdfPath(),
|
|
);
|
|
$this->assertSame($element->getTitle(), $updatedElement->getTitle());
|
|
$this->assertSame(
|
|
['/assets/pdfs/original.pdf'],
|
|
$this->fileUploader->deletedPaths,
|
|
);
|
|
}
|
|
|
|
public function testUpdateLongPdfPathClearsEmptyString(): void
|
|
{
|
|
$element = $this->createFilledElement();
|
|
$updateLongPdfPath = new UpdateLongPdfPath(
|
|
$this->elementRepo,
|
|
$this->fileUploader,
|
|
);
|
|
|
|
$updatedElement = $updateLongPdfPath->execute(
|
|
new UpdateLongPdfPathRequest(
|
|
id: $element->getId(),
|
|
longPdfPath: '',
|
|
)
|
|
);
|
|
|
|
$this->assertSame(
|
|
'/assets/pdfs/original.pdf',
|
|
$updatedElement->getShortPdfPath(),
|
|
);
|
|
$this->assertNull($updatedElement->getLongPdfPath());
|
|
$this->assertSame($element->getTitle(), $updatedElement->getTitle());
|
|
$this->assertSame(
|
|
['/assets/pdfs/original-long.pdf'],
|
|
$this->fileUploader->deletedPaths,
|
|
);
|
|
}
|
|
|
|
public function testUpdateYoutubeUrlClearsEmptyString(): void
|
|
{
|
|
$element = $this->createFilledElement();
|
|
$updateYoutubeUrl = new UpdateYoutubeUrl($this->elementRepo);
|
|
|
|
$updatedElement = $updateYoutubeUrl->execute(
|
|
new UpdateYoutubeUrlRequest(
|
|
id: $element->getId(),
|
|
youtubeUrl: '',
|
|
)
|
|
);
|
|
|
|
$this->assertNull($updatedElement->getYoutubeUrl());
|
|
$this->assertSame($element->getTitle(), $updatedElement->getTitle());
|
|
}
|
|
|
|
private function createFilledElement(): Element
|
|
{
|
|
return $this->createElementWithMedia(
|
|
'/assets/original-icon.png',
|
|
'/assets/pdfs/original.pdf',
|
|
'/assets/pdfs/original-long.pdf',
|
|
);
|
|
}
|
|
|
|
private function createElementWithMedia(
|
|
?string $iconImageUrl,
|
|
?string $shortPdfPath,
|
|
?string $longPdfPath,
|
|
): Element {
|
|
$set = new DomainSet(
|
|
id: 1,
|
|
name: 'Baderech',
|
|
description: 'Baderech description',
|
|
iconImageUrl: '/assets/baderech-icon.png',
|
|
);
|
|
|
|
return $this->elementRepo->create(new CreateElementDto(
|
|
set: $set,
|
|
title: 'Original title',
|
|
description: 'Original description',
|
|
iconImageUrl: $iconImageUrl,
|
|
richText: '<p>Original rich text</p>',
|
|
shortPdfPath: $shortPdfPath,
|
|
longPdfPath: $longPdfPath,
|
|
youtubeUrl: 'https://www.youtube.com/watch?v=yHx-r4p6hHU',
|
|
parentElement: null,
|
|
));
|
|
}
|
|
}
|