Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve testing coverage for src/Paginator/ #1494

Merged
merged 1 commit into from Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}