/
RawPaginatorAdapterTest.php
122 lines (103 loc) · 3.51 KB
/
RawPaginatorAdapterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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\RawPaginatorAdapter;
use Elastica\Query;
use Elastica\ResultSet;
use Elastica\SearchableInterface;
use InvalidArgumentException;
use FOS\ElasticaBundle\Tests\Unit\UnitTestHelper;
class RawPaginatorAdapterTest extends UnitTestHelper
{
protected function mockResultSet()
{
$methods = ['getTotalHits', 'getAggregations', 'getSuggests', 'getMaxScore'];
$mock = $this
->getMockBuilder(ResultSet::class)
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
return $mock;
}
private function createAdapterWithSearch($methodName, $value)
{
$resultSet = $this->mockResultSet();
$resultSet
->expects($this->exactly(1))
->method($methodName)
->willReturn($value);
$query = new Query();
$options = [];
$searchable = $this->mockSearchable();
$searchable
->expects($this->exactly(1))
->method('search')
->with($query)
->willReturn($resultSet);
$adapter = new RawPaginatorAdapter($searchable, $query, $options);
return $adapter;
}
private function createAdapterWithCount($totalHits, $querySize = null)
{
$query = new Query();
if ($querySize) {
$query->setParam('size', $querySize);
}
$options = [];
$searchable = $this->mockSearchable();
$searchable
->expects($this->exactly(1))
->method('count')
->willReturn($totalHits);
$adapter = new RawPaginatorAdapter($searchable, $query, $options);
return $adapter;
}
public function testGetTotalHits()
{
$adapter = $this->createAdapterWithCount(123);
$this->assertEquals(123, $adapter->getTotalHits());
$adapter = $this->createAdapterWithCount(123, 100);
$this->assertEquals(100, $adapter->getTotalHits());
}
public function testGetTotalHitsGenuineTotal()
{
$adapter = $this->createAdapterWithCount(123);
$this->assertEquals(123, $adapter->getTotalHits(true));
$adapter = $this->createAdapterWithCount(123, 100);
$this->assertEquals(123, $adapter->getTotalHits(true));
}
public function testGetAggregations()
{
$value = [];
$adapter = $this->createAdapterWithSearch('getAggregations', $value);
$this->assertEquals($value, $adapter->getAggregations());
}
public function testGetSuggests()
{
$value = [];
$adapter = $this->createAdapterWithSearch('getSuggests', $value);
$this->assertEquals($value, $adapter->getSuggests());
}
public function testGetMaxScore()
{
$value = 1.0;
$adapter = $this->createAdapterWithSearch('getMaxScore', $value);
$this->assertEquals($value, $adapter->getMaxScore());
}
public function testGetQuery()
{
$resultSet = $this->mockResultSet();
$query = new Query();
$options = [];
$searchable = $this->mockSearchable($query);
$adapter = new RawPaginatorAdapter($searchable, $query, $options);
$this->assertEquals($query, $adapter->getQuery());
}
}