From 285abd1a563ff7afe66490ba5941661922357c5f Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Mon, 3 Nov 2014 13:53:15 +0200 Subject: [PATCH] added content extension test with exception --- ...tionTest.php => ContentExtensionsTest.php} | 10 ++-- Tests/Unit/Twig/ContentExtensionTest.php | 51 +++++++++++++++++-- 2 files changed, 52 insertions(+), 9 deletions(-) rename Tests/Functional/Twig/{ContentExtensionsIntegrationTest.php => ContentExtensionsTest.php} (87%) diff --git a/Tests/Functional/Twig/ContentExtensionsIntegrationTest.php b/Tests/Functional/Twig/ContentExtensionsTest.php similarity index 87% rename from Tests/Functional/Twig/ContentExtensionsIntegrationTest.php rename to Tests/Functional/Twig/ContentExtensionsTest.php index b4c4402..9a41e5d 100644 --- a/Tests/Functional/Twig/ContentExtensionsIntegrationTest.php +++ b/Tests/Functional/Twig/ContentExtensionsTest.php @@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; -class ContentExtensionsIntegrationTest extends ElasticsearchTestCase +class ContentExtensionsTest extends ElasticsearchTestCase { /** * {@inheritdoc} @@ -39,11 +39,11 @@ protected function getDataArray() } /** - * Test data provider for testSnippetFunctionIntegration(). + * Test data provider for testSnippetFunction(). * * @return array */ - public function getTestSnippetFunctionIntegrationData() + public function getTestSnippetFunctionData() { $out = array(); @@ -78,9 +78,9 @@ public function getTestSnippetFunctionIntegrationData() * @param string $expectedContent * @param string $strategy * - * @dataProvider getTestSnippetFunctionIntegrationData() + * @dataProvider getTestSnippetFunctionData() */ - public function testSnippetFunctionIntegration($request, $slug, $expectedContent, $strategy = 'esi') + public function testSnippetFunction($request, $slug, $expectedContent, $strategy = 'esi') { // Custom logic for Symfony 2.4+. if ($this->getContainer()->has('request_stack')) { diff --git a/Tests/Unit/Twig/ContentExtensionTest.php b/Tests/Unit/Twig/ContentExtensionTest.php index 9592b30..629368c 100644 --- a/Tests/Unit/Twig/ContentExtensionTest.php +++ b/Tests/Unit/Twig/ContentExtensionTest.php @@ -67,7 +67,7 @@ public function testGetContentsBySlugsFunction() $extension = $this->getContentExtension([$document2, $document]); - $this->assertEquals(array($document, $document2), $extension->getContentsBySlugsFunction(array(1, 2), true)); + $this->assertEquals([$document, $document2], $extension->getContentsBySlugsFunction([1, 2], true)); } /** @@ -80,13 +80,13 @@ public function getTestSnippetFunctionData() $out = array(); // Case #0: inline strategy. - $out[] = array('inline'); + $out[] = ['inline']; // Case #1: esi strategy. - $out[] = array('esi'); + $out[] = ['esi']; // Case #2: ssi strategy. - $out[] = array('ssi'); + $out[] = ['ssi']; return $out; } @@ -127,6 +127,49 @@ public function testSnippetFunction($strategy) $extension->snippetFunction(1); } + /** + * Tests extension when exception is thrown. + */ + public function testSnippetFunctionWithException() + { + $router = $this->getRouterMock(); + $router + ->expects($this->once()) + ->method('generate') + ->with( + '_ongr_plain_cms_snippet', + [ + 'template' => null, + 'slug' => 1 + ] + ); + + $fragmentHandlerMock = $this->getFragmentHandlerMock(); + $fragmentHandlerMock + ->expects($this->at(0)) + ->method('render') + ->with( + $this->anything(), + [] + )->will($this->throwException(new \InvalidArgumentException())); + $fragmentHandlerMock + ->expects($this->at(1)) + ->method('render') + ->with( + $this->anything() + )->will($this->returnValue('content')); + + $extension = new ContentExtension( + $fragmentHandlerMock, + $router, + [], + $this->getManager(), + 'AcmeTestBundle:Content' + ); + + $this->assertEquals('content', $extension->snippetFunction(1)); + } + /** * @param array $results *