Skip to content

Commit

Permalink
Merge pull request ongr-archive#5 from martiis/issue_2
Browse files Browse the repository at this point in the history
removed getDataForSnippet method, content service and extension tests
  • Loading branch information
saimaz committed Nov 6, 2014
2 parents eba29c3 + 285abd1 commit d1dccd3
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 46 deletions.
4 changes: 3 additions & 1 deletion Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Content/plain_cms_snippet.html.twig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ include(template_from_string(content)) }}
{{ include(template_from_string(document.content)) }}
28 changes: 3 additions & 25 deletions Service/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
45 changes: 35 additions & 10 deletions Tests/Functional/Service/ContentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class ContentExtensionsIntegrationTest extends ElasticsearchTestCase
class ContentExtensionsTest extends ElasticsearchTestCase
{
/**
* {@inheritdoc}
Expand All @@ -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();

Expand Down Expand Up @@ -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')) {
Expand Down
98 changes: 98 additions & 0 deletions Tests/Unit/Service/ContentServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* 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.');
}
}
51 changes: 47 additions & 4 deletions Tests/Unit/Twig/ContentExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit d1dccd3

Please sign in to comment.