mediaNotFound example

$updated = $this->mediaRepository->search(new Criteria([$png->getId()])$context)->get($png->getId());
        static::assertInstanceOf(MediaEntity::class$updated);
        static::assertIsString($updated->getFileName());
        static::assertStringEndsWith($longFileName$updated->getFileName());
        static::assertTrue($this->getPublicFilesystem()->has($this->urlGenerator->getRelativeMediaUrl($updated)));
    }

    public function testRenameMediaThrowsExceptionIfMediaDoesNotExist(): void
    {
        $id = Uuid::randomHex();
        $this->expectException(MediaException::class);
        $this->expectExceptionMessage(MediaException::mediaNotFound($id)->getMessage());

        $context = Context::createDefaultContext();
        $this->fileSaver->renameMedia($id, 'new file destination', $context);
    }

    public function testRenameMediaThrowsExceptionIfMediaHasNoFileAttached(): void
    {
        $id = Uuid::randomHex();

        $this->expectException(MediaException::class);
        $this->expectExceptionMessage(MediaException::missingFile($id)->getMessage());

        
static::assertEquals(Response::HTTP_BAD_REQUEST, $exception->getStatusCode());
        static::assertEquals(MediaException::MEDIA_ILLEGAL_FILE_NAME, $exception->getErrorCode());
        static::assertEquals('Provided filename "illegal-filename" is not permitted: cause', $exception->getMessage());
        static::assertEquals(['fileName' => $fileName, 'cause' => $cause]$exception->getParameters());
    }

    public function testMediaNotFound(): void
    {
        $mediaId = 'media-id';

        $exception = MediaException::mediaNotFound($mediaId);

        static::assertEquals(Response::HTTP_NOT_FOUND, $exception->getStatusCode());
        static::assertEquals(MediaException::MEDIA_NOT_FOUND, $exception->getErrorCode());
        static::assertEquals('Media for id media-id not found.', $exception->getMessage());
        static::assertEquals(['mediaId' => $mediaId]$exception->getParameters());
    }

    public function testInvalidFile(): void
    {
        $cause = 'cause';

        

    private function findMediaById(string $mediaId, Context $context): MediaEntity
    {
        $criteria = new Criteria([$mediaId]);
        $criteria->addAssociation('mediaFolder');
        /** @var MediaEntity|null $currentMedia */
        $currentMedia = $this->mediaRepository
            ->search($criteria$context)
            ->get($mediaId);

        if ($currentMedia === null) {
            throw MediaException::mediaNotFound($mediaId);
        }

        return $currentMedia;
    }

    /** * @throws MediaException */
    private function validateFileName(string $destination): string
    {
        $destination = rtrim($destination);
        

    private function findMediaById(string $mediaId, Context $context): MediaEntity
    {
        $criteria = new Criteria([$mediaId]);
        $criteria->addAssociation('mediaFolder');

        $currentMedia = $this->mediaRepository
            ->search($criteria$context)
            ->get($mediaId);

        if (!$currentMedia instanceof MediaEntity) {
            throw MediaException::mediaNotFound($mediaId);
        }

        return $currentMedia;
    }
}
Home | Imprint | This part of the site doesn't use cookies.