diff --git a/src/Block/Service/ShariffShareBlockService.php b/src/Block/Service/ShariffShareBlockService.php index f8b940b6..8fc8d57e 100644 --- a/src/Block/Service/ShariffShareBlockService.php +++ b/src/Block/Service/ShariffShareBlockService.php @@ -11,6 +11,7 @@ namespace Nucleos\ShariffBundle\Block\Service; +use LogicException; use Sonata\BlockBundle\Block\BlockContextInterface; use Sonata\BlockBundle\Block\Service\AbstractBlockService; use Sonata\BlockBundle\Block\Service\EditableBlockService; @@ -35,6 +36,10 @@ public function execute(BlockContextInterface $blockContext, ?Response $response 'block' => $blockContext->getBlock(), ]; + if (!\is_string($blockContext->getTemplate())) { + throw new LogicException('Cannot render block without template'); + } + return $this->renderResponse($blockContext->getTemplate(), $parameters, $response); } diff --git a/tests/Block/Service/ShariffShareBlockServiceTest.php b/tests/Block/Service/ShariffShareBlockServiceTest.php index 0a2b2afd..0461d6c1 100755 --- a/tests/Block/Service/ShariffShareBlockServiceTest.php +++ b/tests/Block/Service/ShariffShareBlockServiceTest.php @@ -11,6 +11,7 @@ namespace Nucleos\ShariffBundle\Tests\Block\Service; +use LogicException; use Nucleos\ShariffBundle\Block\Service\ShariffShareBlockService; use Sonata\BlockBundle\Block\BlockContext; use Sonata\BlockBundle\Form\Mapper\FormMapper; @@ -71,6 +72,24 @@ public function testExecute(): void static::assertSame('TWIG_CONTENT', $response->getContent()); } + public function testExecuteWithNullTemplate(): void + { + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Cannot render block without template'); + + $block = new Block(); + + $blockContext = new BlockContext($block, [ + 'template' => null, + ]); + + $response = new Response(); + + $blockService = new ShariffShareBlockService($this->twig); + + static::assertSame($response, $blockService->execute($blockContext, $response)); + } + public function testGetMetadata(): void { $blockService = new ShariffShareBlockService($this->twig);