-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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']); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
@@ -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( | ||
|
There was a problem hiding this comment.
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