From 790d76b361d24e6a477eff314a0d401abc200263 Mon Sep 17 00:00:00 2001 From: core23 Date: Thu, 14 Jul 2016 19:39:05 +0200 Subject: [PATCH 1/2] Added documentation for testing --- Resources/doc/index.rst | 1 + Resources/doc/reference/testing.rst | 68 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 Resources/doc/reference/testing.rst diff --git a/Resources/doc/index.rst b/Resources/doc/index.rst index d6b09cf1..7b321d10 100644 --- a/Resources/doc/index.rst +++ b/Resources/doc/index.rst @@ -17,6 +17,7 @@ Reference Guide reference/advanced_usage reference/cache reference/events + reference/testing Cookbooks --------- diff --git a/Resources/doc/reference/testing.rst b/Resources/doc/reference/testing.rst new file mode 100644 index 00000000..b2b8c8cf --- /dev/null +++ b/Resources/doc/reference/testing.rst @@ -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']); + } + } From d3dbcef3d565cd34f0c21d4c3ca13f8fde21f244 Mon Sep 17 00:00:00 2001 From: core23 Date: Thu, 21 Jul 2016 19:24:43 +0200 Subject: [PATCH 2/2] Added phpdoc to abstract test cases --- Test/AbstractBlockServiceTestCase.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Test/AbstractBlockServiceTestCase.php b/Test/AbstractBlockServiceTestCase.php index 8f7b3e17..0e7fb450 100644 --- a/Test/AbstractBlockServiceTestCase.php +++ b/Test/AbstractBlockServiceTestCase.php @@ -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)); @@ -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(