Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation for testing #322

Merged
merged 2 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you include the CustomBlockService


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
*/
Copy link
Member

@OskarStark OskarStark Jul 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please do the phpdoc change at least in a seperate commit? thank you @core23 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 👍

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