diff --git a/Controller/ContentController.php b/Controller/ContentController.php index 77bf223..974c5df 100644 --- a/Controller/ContentController.php +++ b/Controller/ContentController.php @@ -30,7 +30,9 @@ class ContentController extends Controller */ protected function snippetActionData($slug) { - return $this->get('ongr_content.content_service')->getDataForSnippet($slug); + return [ + 'document' => $this->get('ongr_content.content_service')->getDocumentBySlug($slug) + ]; } /** diff --git a/Resources/views/Content/plain_cms_snippet.html.twig b/Resources/views/Content/plain_cms_snippet.html.twig index a224218..4b444e1 100644 --- a/Resources/views/Content/plain_cms_snippet.html.twig +++ b/Resources/views/Content/plain_cms_snippet.html.twig @@ -1 +1 @@ -{{ include(template_from_string(content)) }} +{{ include(template_from_string(document.content)) }} diff --git a/Service/ContentService.php b/Service/ContentService.php index f561c07..d1d04f6 100644 --- a/Service/ContentService.php +++ b/Service/ContentService.php @@ -50,36 +50,14 @@ public function getDocumentBySlug($slug) $results = $this->repository->execute($search); - if (isset($results)) { - return $results->current(); - } - - return null; - } - - /** - * Returns data for content snippet render action. - * - * @param string $slug - * - * @return array - */ - public function getDataForSnippet($slug) - { - $document = $this->getDocumentBySlug($slug); - - if (!$document) { + if (count($results) === 0) { $this->logger && $this->logger->warning( sprintf("Can not render snippet for '%s' because content was not found.", $slug) ); - return ['content' => '', 'title' => null, 'document' => null]; + return null; } - return [ - 'content' => $document->content, - 'title' => $document->title, - 'document' => $document, - ]; + return $results->current(); } } diff --git a/Tests/Functional/Service/ContentServiceTest.php b/Tests/Functional/Service/ContentServiceTest.php index a3b0a47..0edea2b 100644 --- a/Tests/Functional/Service/ContentServiceTest.php +++ b/Tests/Functional/Service/ContentServiceTest.php @@ -11,20 +11,45 @@ namespace ONGR\ContentBundle\Tests\Functional\Service; -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use ONGR\ContentBundle\Service\ContentService; +use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase; -class ContentServiceTest extends WebTestCase +class ContentServiceTest extends ElasticsearchTestCase { /** - * Test if content service can be created. + * {@inheritdoc} */ - public function testGetContentService() + protected function getDataArray() { - $container = self::createClient()->getContainer(); - $this->assertTrue($container->has('ongr_content.content_service')); - $this->assertInstanceOf( - 'ONGR\\ContentBundle\\Service\\ContentService', - $container->get('ongr_content.content_service') - ); + return [ + 'default' => [ + 'content' => [ + [ + '_id' => 1, + 'slug' => 'foo' + ], + [ + '_id' => 2, + 'slug' => 'baz' + ], + [ + '_id' => 3, + 'slug' => 'awsome' + ] + ] + ] + ]; + } + + /** + * Test if content service can retrieve document by slug. + */ + public function testGetDocumentBySlug() + { + /** @var ContentService $contentService */ + $contentService = $this->getContainer()->get('ongr_content.content_service'); + $document = $contentService->getDocumentBySlug('baz'); + + $this->assertEquals(2, $document->getId(), 'Expected document with Id of 2.'); } } 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/Service/ContentServiceTest.php b/Tests/Unit/Service/ContentServiceTest.php new file mode 100644 index 0000000..e0ae686 --- /dev/null +++ b/Tests/Unit/Service/ContentServiceTest.php @@ -0,0 +1,98 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ContentBundle\Tests\Unit\Service; + +use ONGR\ContentBundle\Service\ContentService; +use ONGR\ElasticsearchBundle\DSL\Query\TermQuery; + +class ContentServiceTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests if method getDocumentBySlug does what is expected. + */ + public function testGetDocumentBySlug() + { + $searchMock = $this->getMock('ElasticsearchBundle\DSL\Search', ['addQuery']); + $searchMock + ->expects($this->once()) + ->method('addQuery') + ->with( + new TermQuery('slug', 'foo'), + 'must' + ); + + $repositoryMock = $this + ->getMockBuilder('ElasticsearchBundle\ORM\Repository') + ->disableOriginalConstructor() + ->setMethods(['execute', 'createSearch']) + ->getMock(); + + $repositoryMock + ->expects($this->once()) + ->method('createSearch') + ->will($this->returnValue($searchMock)); + + $repositoryMock + ->expects($this->once()) + ->method('execute') + ->with() + ->will($this->returnValue(new \ArrayIterator(['first', 'second']))); + + $service = new ContentService($repositoryMock); + + $result = $service->getDocumentBySlug('foo'); + $this->assertEquals('first', $result, 'Expected first result.'); + } + + /** + * Tests if method getDocumentBySlug is logging when no documents found. + */ + public function testGetDocumentBySlugLogger() + { + $searchMock = $this->getMock('ElasticsearchBundle\DSL\Search', ['addQuery']); + $searchMock + ->expects($this->once()) + ->method('addQuery') + ->with( + $this->isInstanceOf('ONGR\ElasticsearchBundle\DSL\Query\TermQuery'), + 'must' + ); + + $repositoryMock = $this + ->getMockBuilder('ElasticsearchBundle\ORM\Repository') + ->disableOriginalConstructor() + ->setMethods(['execute', 'createSearch']) + ->getMock(); + + $repositoryMock + ->expects($this->once()) + ->method('createSearch') + ->will($this->returnValue($searchMock)); + + $repositoryMock + ->expects($this->once()) + ->method('execute') + ->with() + ->will($this->returnValue(new \ArrayIterator([]))); + + $loggerMock = $this->getMock('Psr\\Log\\LoggerInterface'); + $loggerMock + ->expects($this->once()) + ->method('warning') + ->with('Can not render snippet for \'foo\' because content was not found.'); + + $service = new ContentService($repositoryMock); + $service->setLogger($loggerMock); + + $this->assertNull($service->getDocumentBySlug('foo'), 'Result expected to be null.'); + } +} 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 *