Skip to content

Commit

Permalink
Merge pull request #322 from core23/doc/test
Browse files Browse the repository at this point in the history
Added documentation for testing
  • Loading branch information
core23 committed Jul 28, 2016
2 parents 0a88a3e + d3dbcef commit 2d111b8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Reference Guide
reference/advanced_usage
reference/cache
reference/events
reference/testing

Cookbooks
---------
Expand Down
68 changes: 68 additions & 0 deletions Resources/doc/reference/testing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.. index::
double: Test Widgets; Definition

Testing
=======

Test Blocks
~~~~~~~~~~~

Given the following block service:

.. code-block:: php
class CustomBlockService extends AbstractBlockService
{
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
return $this->renderResponse($blockContext->getTemplate(), array(
'context' => $blockContext,
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
), $response);
}
public function configureSettings(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'foo' => 'bar',
'attr' => array(),
'template' => false,
));
}
}
You can write unit tests for block services with the following code.

.. code-block:: php
use Sonata\BlockBundle\Test\AbstractBlockServiceTestCase;
class CustomBlockServiceTest extends AbstractBlockServiceTestCase
{
public function testDefaultSettings()
{
$blockService = new CustomBlockService('foo', $this->templating);
$blockContext = $this->getBlockContext($blockService);
$this->assertSettings(array(
'foo' => bar,
'attr' => array(),
'template' => false,
), $blockContext);
}
public function testExecute()
{
$blockService = new CustomBlockService('foo', $this->templating);
$blockContext = $this->getBlockContext($blockService);
$service->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']);
}
}
13 changes: 13 additions & 0 deletions Test/AbstractBlockServiceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ protected function setUp()
$this->blockContextManager = new BlockContextManager($blockLoader, $this->blockServiceManager);
}

/**
* Create a mocked block service.
*
* @param BlockServiceInterface $blockService A block service
*
* @return BlockContextInterface
*/
protected function getBlockContext(BlockServiceInterface $blockService)
{
$this->blockServiceManager->expects($this->once())->method('get')->will($this->returnValue($blockService));
Expand All @@ -68,6 +75,12 @@ protected function getBlockContext(BlockServiceInterface $blockService)
return $blockContext;
}

/**
* Asserts that the block settings have the expected values.
*
* @param array $expected Expected settings
* @param BlockContextInterface $blockContext BlockContext object
*/
protected function assertSettings(array $expected, BlockContextInterface $blockContext)
{
$completeExpectedOptions = array_merge(array(
Expand Down

0 comments on commit 2d111b8

Please sign in to comment.