elementRepository = new FakeElementRepository(); $this->fileUploader = new FakeFileUploader(); $this->useCase = new UpdateIconImage( $this->elementRepository, $this->fileUploader, ); } /** * @return array{ * fileContents: string, * fileOriginalName: string, * fileMimeType: string, * fileSizeBytes: int * } */ private function imageArgs(): array { return [ 'fileContents' => 'binary-image-bytes', 'fileOriginalName' => 'icon.png', 'fileMimeType' => 'image/png', 'fileSizeBytes' => 1024, ]; } public function testUploadsIconImageAndStoresPath(): void { $element = $this->createElement(); $updatedElement = $this->useCase->execute( new UpdateIconImageRequest( ...$this->imageArgs(), id: $element->getId(), ) ); $this->assertSame( 'element-icons/fake-icon.png', $updatedElement->getIconImageUrl(), ); $this->assertSame( 'element-icons/fake-icon.png', $this->elementRepository ->find($element->getId()) ->getIconImageUrl(), ); $this->assertSame( 'element-icons', $this->fileUploader->uploads[0]['folder'], ); } public function testNullIdThrowsBadRequest(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('id is required'); $this->useCase->execute( new UpdateIconImageRequest( ...$this->imageArgs(), id: null, ) ); } public function testNullIconImageThrowsBadRequest(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('icon image is required'); $this->useCase->execute( new UpdateIconImageRequest( id: 1, fileContents: null, fileOriginalName: null, fileMimeType: null, fileSizeBytes: null, ) ); } public function testDisallowedMimeThrowsBadRequest(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage( 'icon image must be a jpeg, png or webp image', ); $this->useCase->execute( new UpdateIconImageRequest( id: 1, fileContents: 'not-an-image', fileOriginalName: 'icon.pdf', fileMimeType: 'application/pdf', fileSizeBytes: 1024, ) ); } public function testOversizedIconImageThrowsBadRequest(): void { $this->expectException(BadRequestException::class); $this->expectExceptionMessage('icon image must be 5MB or smaller'); $this->useCase->execute( new UpdateIconImageRequest( id: 1, fileContents: 'big', fileOriginalName: 'icon.png', fileMimeType: 'image/png', fileSizeBytes: 6 * 1024 * 1024, ) ); } public function testMissingElementThrowsNotFound(): void { $this->expectException(NotFoundException::class); $this->expectExceptionMessage('Element not found'); $this->useCase->execute( new UpdateIconImageRequest( ...$this->imageArgs(), 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, )); } }