1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pagerfanta\Tests\Adapter; |
4
|
|
|
|
5
|
|
|
use Pagerfanta\Adapter\ElasticaAdapter; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class ElasticaAdapterTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ElasticaAdapter |
12
|
|
|
*/ |
13
|
|
|
private $adapter; |
14
|
|
|
private $resultSet; |
15
|
|
|
private $searchable; |
16
|
|
|
private $query; |
17
|
|
|
private $options; |
18
|
|
|
|
19
|
|
|
protected function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->query = $this->getMockBuilder('Elastica\\Query')->disableOriginalConstructor()->getMock(); |
22
|
|
|
$this->resultSet = $this->getMockBuilder('Elastica\\ResultSet')->disableOriginalConstructor()->getMock(); |
23
|
|
|
$this->searchable = $this->getMockBuilder('Elastica\\SearchableInterface')->disableOriginalConstructor()->getMock(); |
24
|
|
|
|
25
|
|
|
$this->options = array("option1" => "value1", "option2" => "value2"); |
26
|
|
|
|
27
|
|
|
$this->adapter = new ElasticaAdapter($this->searchable, $this->query, $this->options); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetResultSet() |
31
|
|
|
{ |
32
|
|
|
$this->assertNull($this->adapter->getResultSet()); |
33
|
|
|
|
34
|
|
|
$this->searchable->expects($this->any()) |
35
|
|
|
->method('search') |
36
|
|
|
->with($this->query, array('from' => 0, 'size' => 1, 'option1' => 'value1', 'option2' => 'value2')) |
37
|
|
|
->will($this->returnValue($this->resultSet)); |
38
|
|
|
|
39
|
|
|
$this->adapter->getSlice(0, 1); |
40
|
|
|
|
41
|
|
|
$this->assertSame($this->resultSet, $this->adapter->getResultSet()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetSlice() |
45
|
|
|
{ |
46
|
|
|
$this->searchable->expects($this->any()) |
47
|
|
|
->method('search') |
48
|
|
|
->with($this->query, array('from' => 10, 'size' => 30, 'option1' => 'value1', 'option2' => 'value2')) |
49
|
|
|
->will($this->returnValue($this->resultSet)); |
50
|
|
|
|
51
|
|
|
$resultSet = $this->adapter->getSlice(10, 30); |
52
|
|
|
|
53
|
|
|
$this->assertSame($this->resultSet, $resultSet); |
54
|
|
|
$this->assertSame($this->resultSet, $this->adapter->getResultSet()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the number of results before search, use count() method if resultSet is empty |
59
|
|
|
*/ |
60
|
|
|
public function testGetNbResultsBeforeSearch() |
61
|
|
|
{ |
62
|
|
|
$this->searchable->expects($this->once()) |
63
|
|
|
->method('count') |
64
|
|
|
->with($this->query) |
65
|
|
|
->willReturn(100); |
66
|
|
|
|
67
|
|
|
$this->assertSame(100, $this->adapter->getNbResults()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the number of results after search, use getTotalHits() method if resultSet is not empty |
72
|
|
|
*/ |
73
|
|
|
public function testGetNbResultsAfterSearch() |
74
|
|
|
{ |
75
|
|
|
$adapter = new ElasticaAdapter($this->searchable, $this->query, [], 30); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$this->searchable->expects($this->once()) |
78
|
|
|
->method('search') |
79
|
|
|
->with($this->query, array('from' => 10, 'size' => 30)) |
80
|
|
|
->will($this->returnValue($this->resultSet)); |
81
|
|
|
|
82
|
|
|
$this->resultSet->expects($this->once()) |
83
|
|
|
->method('getTotalHits') |
84
|
|
|
->will($this->returnValue(100)); |
85
|
|
|
|
86
|
|
|
$adapter->getSlice(10, 30); |
87
|
|
|
|
88
|
|
|
$this->assertSame(30, $adapter->getNbResults()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGetNbResultsWithMaxResultsSet() |
92
|
|
|
{ |
93
|
|
|
$adapter = new ElasticaAdapter($this->searchable, $this->query, [], 10); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$this->searchable->expects($this->once()) |
96
|
|
|
->method('count') |
97
|
|
|
->with($this->query) |
98
|
|
|
->willReturn(100); |
99
|
|
|
|
100
|
|
|
$this->assertSame(10, $adapter->getNbResults()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: