Skip to content

Commit

Permalink
Added ContentService tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Martynas Sudintas committed Nov 3, 2014
1 parent d97e0e1 commit 50e003a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Service/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getDocumentBySlug($slug)

$results = $this->repository->execute($search);

if (!isset($results)) {
if (count($results) === 0) {
$this->logger && $this->logger->warning(
sprintf("Can not render snippet for '%s' because content was not found.", $slug)
);
Expand Down
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.');
}
}
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.');
}
}

0 comments on commit 50e003a

Please sign in to comment.