Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Throw LogicException when rendering block without template
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Mar 9, 2021
1 parent 445af59 commit f8d8b90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Block/Service/ShariffShareBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Block/Service/ShariffShareBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f8d8b90

Please sign in to comment.