-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #322 from core23/doc/test
Added documentation for testing
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters