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 21b3689
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"psr/cache": "^1.0",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0",
"sonata-project/block-bundle": "^3.21 || ^4.2",
"sonata-project/block-bundle": "^3.21 || ^4.5.1",
"sonata-project/form-extensions": "^1.0",
"symfony/config": "^4.2 || ^5.0",
"symfony/dependency-injection": "^4.2 || ^5.0",
Expand Down
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
18 changes: 18 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,23 @@ 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);
$blockService->execute($blockContext, $response);
}

public function testGetMetadata(): void
{
$blockService = new ShariffShareBlockService($this->twig);
Expand Down

0 comments on commit 21b3689

Please sign in to comment.