Version

Documentation » Block

1. Rapid Prototyping with BlockBundle »

« 9. Events

10. Testing

10.1. Test Blocks

Given the following block service:

class CustomBlockService extends AbstractBlockService
{
    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        return $this->renderResponse($blockContext->getTemplate(), [
            'context' => $blockContext,
            'block' => $blockContext->getBlock(),
            'settings' => $blockContext->getSettings(),
        ], $response);
    }

    public function configureSettings(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'foo' => 'bar',
            'attr' => [],
            'template' => false,
        ]);
    }
}

You can write unit tests for block services with the following code:

use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase;

class CustomBlockServiceTest extends AbstractBlockServiceTestCase
{
    public function testDefaultSettings()
    {
        $blockService = new CustomBlockService('foo', $this->twig);
        $blockContext = $this->getBlockContext($blockService);

        $this->assertSettings([
            'foo' => 'bar',
            'attr' => [],
            'template' => false,
        ], $blockContext);
    }

    public function testExecute()
    {
        $blockService = new CustomBlockService('foo', $this->twig);
        $blockContext = $this->getBlockContext($blockService);

        $blockService->execute($blockContext);

        $this->assertSame($blockContext, $this->templating->parameters['context']);
        $this->assertInternalType('array', $this->templating->parameters['settings']);
        $this->assertInstanceOf('Sonata\BlockBundle\Model\BlockInterface', $this->templating->parameters['block']);
        $this->assertSame('bar', $this->templating->parameters['foo']);
    }
}

Found a typo? Something is wrong in this documentation? Just fork and edit it!