Skip to content

Commit

Permalink
Merge pull request #1494 from Labstep/testing/improve-paginator-coverage
Browse files Browse the repository at this point in the history
Improve testing coverage for src/Paginator/
  • Loading branch information
XWB committed Jan 28, 2019
2 parents 6a01018 + 03bf188 commit 61ff1f9
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 11 deletions.
49 changes: 49 additions & 0 deletions tests/Unit/Paginator/HybridPaginatorAdapterTest.php
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\Tests\Unit\Event;

use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
use Elastica\Query;
use Elastica\SearchableInterface;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use FOS\ElasticaBundle\Tests\Unit\UnitTestHelper;

class HybridPaginatorAdapterTest extends UnitTestHelper
{
protected function mockHybridPaginatorAdapter($args)
{
$mock = $this
->getMockBuilder(HybridPaginatorAdapter::class)
->setConstructorArgs($args)
->setMethods(['getElasticaResults'])
->getMock();

$resultSet = $this->mockResultSet();

$mock
->expects($this->exactly(1))
->method('getElasticaResults')
->willReturn($resultSet);

return $mock;
}

public function testGetResults()
{
$searchable = $this->mockSearchable();
$query = new Query();
$transformer = $this->mockElasticaToModelTransformer();

$adapter = $this->mockHybridPaginatorAdapter([$searchable, $query, $transformer]);
$adapter->getResults(0, 0);
}
}
45 changes: 45 additions & 0 deletions tests/Unit/Paginator/HybridPartialResultsTest.php
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\Tests\Unit\Event;

use FOS\ElasticaBundle\Paginator\HybridPartialResults;
use Elastica\ResultSet;
use FOS\ElasticaBundle\Tests\Unit\UnitTestHelper;

class HybridPartialResultsTest extends UnitTestHelper
{
protected function mockResultSet()
{
$mock = $this
->getMockBuilder(ResultSet::class)
->disableOriginalConstructor()
->getMock();

$mock
->expects($this->exactly(1))
->method('getResults')
->willReturn([]);

return $mock;
}

public function testToArray()
{
$transformer = $this->mockElasticaToModelTransformer();

$resultSet = $this->mockResultSet();

$results = new HybridPartialResults($resultSet, $transformer);

$results->toArray();
}
}
14 changes: 3 additions & 11 deletions tests/Unit/Paginator/RawPaginatorAdapterTest.php
Expand Up @@ -16,11 +16,11 @@
use Elastica\ResultSet;
use Elastica\SearchableInterface;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use FOS\ElasticaBundle\Tests\Unit\UnitTestHelper;

class RawPaginatorAdapterTest extends TestCase
class RawPaginatorAdapterTest extends UnitTestHelper
{
private function mockResultSet()
protected function mockResultSet()
{
$methods = ['getTotalHits', 'getAggregations', 'getSuggests', 'getMaxScore'];
$mock = $this
Expand All @@ -31,14 +31,6 @@ private function mockResultSet()
return $mock;
}

private function mockSearchable()
{
$mock = $this
->getMockBuilder(SearchableInterface::class)
->getMock();
return $mock;
}

private function createAdapterWithSearch($methodName, $value)
{
$resultSet = $this->mockResultSet();
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/UnitTestHelper.php
Expand Up @@ -11,6 +11,9 @@

namespace FOS\ElasticaBundle\Tests\Unit;

use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use Elastica\SearchableInterface;
use Elastica\ResultSet;
use PHPUnit\Framework\TestCase;

class UnitTestHelper extends TestCase
Expand All @@ -29,4 +32,29 @@ protected function getProtectedProperty($object, string $property)

return $reflectionProperty->getValue($object);
}

protected function mockElasticaToModelTransformer()
{
$mock = $this
->getMockBuilder(ElasticaToModelTransformerInterface::class)
->getMock();
return $mock;
}

protected function mockSearchable()
{
$mock = $this
->getMockBuilder(SearchableInterface::class)
->getMock();
return $mock;
}

protected function mockResultSet()
{
$mock = $this
->getMockBuilder(ResultSet::class)
->disableOriginalConstructor()
->getMock();
return $mock;
}
}

0 comments on commit 61ff1f9

Please sign in to comment.