elementRepo = new FakeElementRepository(); $this->updateElement = new UpdateElement( new UpdateTitle($this->elementRepo), new UpdateDescription($this->elementRepo), new UpdateIconImageUrl($this->elementRepo), new UpdateRichText($this->elementRepo), new UpdatePdfPath($this->elementRepo), new UpdateYoutubeUrl($this->elementRepo), $this->elementRepo, ); } public function testUpdatesContentFields(): void { $set = $this->createSet(1, 'Baderech'); $element = $this->createElement( $set, 'Original title', 'Original description', '/assets/original-icon.png', '
Original rich text
', '/assets/pdfs/original.pdf', 'https://www.youtube.com/watch?v=yHx-r4p6hHU&t=1s', null, ); $updatedElement = $this->updateElement->execute( new UpdateElementRequest( id: $element->getId(), title: 'Updated title', description: 'Updated description', iconImageUrl: '/assets/updated-icon.png', richText: 'Updated rich text
', pdfPath: '/assets/pdfs/updated.pdf', youtubeUrl: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', ) ); $this->assertSame($element->getId(), $updatedElement->getId()); $this->assertSame('Updated title', $updatedElement->getTitle()); $this->assertSame( 'Updated description', $updatedElement->getDescription(), ); $this->assertSame( '/assets/updated-icon.png', $updatedElement->getIconImageUrl(), ); $this->assertSame( 'Updated rich text
', $updatedElement->getRichText(), ); $this->assertSame( '/assets/pdfs/updated.pdf', $updatedElement->getPdfPath(), ); $this->assertSame( 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', $updatedElement->getYoutubeUrl(), ); $persistedElement = $this->elementRepo->find($element->getId()); $this->assertInstanceOf(Element::class, $persistedElement); $this->assertSame('Updated title', $persistedElement->getTitle()); } public function testConvertsEmptyNullableFieldsToNull(): void { $set = $this->createSet(1, 'Baderech'); $element = $this->createElement( $set, 'Original title', 'Original description', '/assets/original-icon.png', 'Original rich text
', '/assets/pdfs/original.pdf', 'https://www.youtube.com/watch?v=yHx-r4p6hHU&t=1s', null, ); $updatedElement = $this->updateElement->execute( new UpdateElementRequest( id: $element->getId(), title: 'Updated title', description: 'Updated description', iconImageUrl: '', richText: 'Updated rich text
', pdfPath: '', youtubeUrl: '', ) ); $this->assertNull($updatedElement->getIconImageUrl()); $this->assertNull($updatedElement->getPdfPath()); $this->assertNull($updatedElement->getYoutubeUrl()); } public function testUpdatesOnlySuppliedFields(): void { $set = $this->createSet(1, 'Baderech'); $element = $this->createElement( $set, 'Original title', 'Original description', '/assets/original-icon.png', 'Original rich text
', '/assets/pdfs/original.pdf', 'https://www.youtube.com/watch?v=yHx-r4p6hHU&t=1s', null, ); $updatedElement = $this->updateElement->execute( new UpdateElementRequest( id: $element->getId(), title: 'Updated title', description: null, iconImageUrl: null, richText: null, pdfPath: null, youtubeUrl: null, ) ); $this->assertSame('Updated title', $updatedElement->getTitle()); $this->assertSame( 'Original description', $updatedElement->getDescription(), ); $this->assertSame( '/assets/original-icon.png', $updatedElement->getIconImageUrl(), ); $this->assertSame( 'Original rich text
', $updatedElement->getRichText(), ); $this->assertSame( '/assets/pdfs/original.pdf', $updatedElement->getPdfPath(), ); $this->assertSame( 'https://www.youtube.com/watch?v=yHx-r4p6hHU&t=1s', $updatedElement->getYoutubeUrl(), ); } public function testThrowsWhenIdMissing(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('id is required'); $this->updateElement->execute(new UpdateElementRequest( id: null, title: 'Updated title', description: 'Updated description', iconImageUrl: null, richText: 'Updated rich text
', pdfPath: null, youtubeUrl: null, )); } public function testThrowsWhenNothingProvided(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('nothing to update'); $this->updateElement->execute(new UpdateElementRequest( id: 1, title: null, description: null, iconImageUrl: null, richText: null, pdfPath: null, youtubeUrl: null, )); } public function testThrowsWhenTitleIsBlank(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('title is required'); $this->updateElement->execute(new UpdateElementRequest( id: 1, title: '', description: 'Updated description', iconImageUrl: null, richText: 'Updated rich text
', pdfPath: null, youtubeUrl: null, )); } public function testThrowsWhenElementDoesNotExist(): void { $this->expectException(NotFoundException::class); $this->expectExceptionMessage('Element not found'); $this->updateElement->execute(new UpdateElementRequest( id: 999, title: 'Updated title', description: 'Updated description', iconImageUrl: null, richText: 'Updated rich text
', pdfPath: null, youtubeUrl: null, )); } private function createSet(int $id, string $name): DomainSet { return new DomainSet( id: $id, name: $name, description: "$name description", iconImageUrl: '/assets/baderech-icon.png', ); } private function createElement( DomainSet $set, string $title, string $description, ?string $iconImageUrl, string $richText, ?string $pdfPath, ?string $youtubeUrl, ?Element $parentElement, ): Element { return $this->elementRepo->create(new CreateElementDto( set: $set, title: $title, description: $description, iconImageUrl: $iconImageUrl, richText: $richText, pdfPath: $pdfPath, youtubeUrl: $youtubeUrl, parentElement: $parentElement, )); } }