|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Finder; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Query; |
|
6
|
|
|
use FOS\ElasticaBundle\Finder\TransformedFinder; |
|
7
|
|
|
|
|
8
|
|
|
class TransformedFinderTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
private function createMockTransformer($transformMethod) |
|
11
|
|
|
{ |
|
12
|
|
|
$transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface'); |
|
13
|
|
|
|
|
14
|
|
|
$transformer |
|
15
|
|
|
->expects($this->once()) |
|
16
|
|
|
->method($transformMethod) |
|
17
|
|
|
->with(array()); |
|
18
|
|
|
|
|
19
|
|
|
return $transformer; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private function createMockFinderForSearch($transformer, $query, $limit) |
|
23
|
|
|
{ |
|
24
|
|
|
$searchable = $this->getMock('Elastica\SearchableInterface'); |
|
25
|
|
|
|
|
26
|
|
|
$finder = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
|
27
|
|
|
->setConstructorArgs(array($searchable, $transformer)) |
|
28
|
|
|
->setMethods(array('search')) |
|
29
|
|
|
->getMock(); |
|
30
|
|
|
|
|
31
|
|
|
$finder |
|
32
|
|
|
->expects($this->once()) |
|
33
|
|
|
->method('search') |
|
34
|
|
|
->with($query, $limit) |
|
35
|
|
|
->will($this->returnValue(array())); |
|
36
|
|
|
|
|
37
|
|
|
return $finder; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function createMockResultSet() |
|
41
|
|
|
{ |
|
42
|
|
|
$resultSet = $this |
|
43
|
|
|
->getMockBuilder('Elastica\ResultSet') |
|
44
|
|
|
->disableOriginalConstructor() |
|
45
|
|
|
->setMethods(array('getResults')) |
|
46
|
|
|
->getMock(); |
|
47
|
|
|
|
|
48
|
|
|
$resultSet->expects($this->once())->method('getResults')->will($this->returnValue(array())); |
|
49
|
|
|
|
|
50
|
|
|
return $resultSet; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testFindMethodTransformsSearchResults() |
|
54
|
|
|
{ |
|
55
|
|
|
$transformer = $this->createMockTransformer('transform'); |
|
56
|
|
|
$query = Query::create(''); |
|
57
|
|
|
$limit = 10; |
|
58
|
|
|
|
|
59
|
|
|
$finder = $this->createMockFinderForSearch($transformer, $query, $limit); |
|
60
|
|
|
|
|
61
|
|
|
$finder->find($query, $limit); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testFindHybridMethodTransformsSearchResults() |
|
65
|
|
|
{ |
|
66
|
|
|
$transformer = $this->createMockTransformer('hybridTransform'); |
|
67
|
|
|
$query = Query::create(''); |
|
68
|
|
|
$limit = 10; |
|
69
|
|
|
|
|
70
|
|
|
$finder = $this->createMockFinderForSearch($transformer, $query, $limit); |
|
71
|
|
|
|
|
72
|
|
|
$finder->findHybrid($query, $limit); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testMoreLikeThisTransformsSearchResultsFromIndex() |
|
76
|
|
|
{ |
|
77
|
|
|
$searchable = $this |
|
78
|
|
|
->getMockBuilder('Elastica\Type') |
|
79
|
|
|
->disableOriginalConstructor() |
|
80
|
|
|
->setMethods(array('moreLikeThis')) |
|
81
|
|
|
->getMock(); |
|
82
|
|
|
|
|
83
|
|
|
$searchable->expects($this->once()) |
|
84
|
|
|
->method('moreLikeThis') |
|
85
|
|
|
->with($this->isInstanceOf('Elastica\Document'), $this->isType('array'), $this->isType('array')) |
|
86
|
|
|
->will($this->returnValue($this->createMockResultSet())); |
|
87
|
|
|
|
|
88
|
|
|
$transformer = $this->createMockTransformer('transform'); |
|
89
|
|
|
|
|
90
|
|
|
$finder = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
|
91
|
|
|
->setConstructorArgs(array($searchable, $transformer)) |
|
92
|
|
|
->setMethods(array('search')) |
|
93
|
|
|
->getMock(); |
|
94
|
|
|
|
|
95
|
|
|
$finder->moreLikeThis(1); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testSearchMethodCreatesAQueryAndReturnsResultsFromSearchableDependency() |
|
99
|
|
|
{ |
|
100
|
|
|
$searchable = $this->getMock('Elastica\SearchableInterface'); |
|
101
|
|
|
$transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface'); |
|
102
|
|
|
|
|
103
|
|
|
$searchable->expects($this->once()) |
|
104
|
|
|
->method('search') |
|
105
|
|
|
->with($this->isInstanceOf('Elastica\Query')) |
|
106
|
|
|
->will($this->returnValue($this->createMockResultSet())); |
|
107
|
|
|
|
|
108
|
|
|
$finder = new TransformedFinder($searchable, $transformer); |
|
109
|
|
|
|
|
110
|
|
|
$method = new \ReflectionMethod($finder, 'search'); |
|
111
|
|
|
$method->setAccessible(true); |
|
112
|
|
|
|
|
113
|
|
|
$results = $method->invoke($finder, '', 10); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertInternalType('array', $results); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testFindPaginatedReturnsAConfiguredPagerfantaObject() |
|
119
|
|
|
{ |
|
120
|
|
|
$searchable = $this->getMock('Elastica\SearchableInterface'); |
|
121
|
|
|
$transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface'); |
|
122
|
|
|
|
|
123
|
|
|
$finder = new TransformedFinder($searchable, $transformer); |
|
124
|
|
|
|
|
125
|
|
|
$pagerfanta = $finder->findPaginated(''); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertInstanceOf('Pagerfanta\Pagerfanta', $pagerfanta); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|