1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Tests\Finder; |
13
|
|
|
|
14
|
|
|
use Elastica\Query; |
15
|
|
|
use FOS\ElasticaBundle\Finder\TransformedFinder; |
16
|
|
|
|
17
|
|
|
class TransformedFinderTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
private function createMockTransformer($transformMethod) |
20
|
|
|
{ |
21
|
|
|
$transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock(); |
22
|
|
|
|
23
|
|
|
$transformer |
24
|
|
|
->expects($this->once()) |
25
|
|
|
->method($transformMethod) |
26
|
|
|
->with(array()); |
27
|
|
|
|
28
|
|
|
return $transformer; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function createMockFinderForSearch($transformer, $query, $limit) |
32
|
|
|
{ |
33
|
|
|
$searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock(); |
34
|
|
|
|
35
|
|
|
$finder = $this->getMockBuilder('FOS\ElasticaBundle\Finder\TransformedFinder') |
36
|
|
|
->setConstructorArgs(array($searchable, $transformer)) |
37
|
|
|
->setMethods(array('search')) |
38
|
|
|
->getMock(); |
39
|
|
|
|
40
|
|
|
$finder |
41
|
|
|
->expects($this->once()) |
42
|
|
|
->method('search') |
43
|
|
|
->with($query, $limit) |
44
|
|
|
->will($this->returnValue(array())); |
45
|
|
|
|
46
|
|
|
return $finder; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function createMockResultSet() |
50
|
|
|
{ |
51
|
|
|
$resultSet = $this |
52
|
|
|
->getMockBuilder('Elastica\ResultSet') |
53
|
|
|
->disableOriginalConstructor() |
54
|
|
|
->setMethods(array('getResults')) |
55
|
|
|
->getMock(); |
56
|
|
|
|
57
|
|
|
$resultSet->expects($this->once())->method('getResults')->will($this->returnValue(array())); |
58
|
|
|
|
59
|
|
|
return $resultSet; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testFindMethodTransformsSearchResults() |
63
|
|
|
{ |
64
|
|
|
$transformer = $this->createMockTransformer('transform'); |
65
|
|
|
$query = Query::create(''); |
66
|
|
|
$limit = 10; |
67
|
|
|
|
68
|
|
|
$finder = $this->createMockFinderForSearch($transformer, $query, $limit); |
69
|
|
|
|
70
|
|
|
$finder->find($query, $limit); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testFindHybridMethodTransformsSearchResults() |
74
|
|
|
{ |
75
|
|
|
$transformer = $this->createMockTransformer('hybridTransform'); |
76
|
|
|
$query = Query::create(''); |
77
|
|
|
$limit = 10; |
78
|
|
|
|
79
|
|
|
$finder = $this->createMockFinderForSearch($transformer, $query, $limit); |
80
|
|
|
|
81
|
|
|
$finder->findHybrid($query, $limit); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testSearchMethodCreatesAQueryAndReturnsResultsFromSearchableDependency() |
85
|
|
|
{ |
86
|
|
|
$searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock(); |
87
|
|
|
$transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock(); |
88
|
|
|
|
89
|
|
|
$searchable->expects($this->once()) |
90
|
|
|
->method('search') |
91
|
|
|
->with($this->isInstanceOf('Elastica\Query')) |
92
|
|
|
->will($this->returnValue($this->createMockResultSet())); |
93
|
|
|
|
94
|
|
|
$finder = new TransformedFinder($searchable, $transformer); |
95
|
|
|
|
96
|
|
|
$method = new \ReflectionMethod($finder, 'search'); |
97
|
|
|
$method->setAccessible(true); |
98
|
|
|
|
99
|
|
|
$results = $method->invoke($finder, '', 10); |
100
|
|
|
|
101
|
|
|
$this->assertInternalType('array', $results); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testFindPaginatedReturnsAConfiguredPagerfantaObject() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock(); |
107
|
|
|
$transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock(); |
108
|
|
|
|
109
|
|
|
$finder = new TransformedFinder($searchable, $transformer); |
110
|
|
|
|
111
|
|
|
$pagerfanta = $finder->findPaginated(''); |
112
|
|
|
|
113
|
|
|
$this->assertInstanceOf('Pagerfanta\Pagerfanta', $pagerfanta); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testCreatePaginatorAdapter() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock(); |
119
|
|
|
$transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock(); |
120
|
|
|
|
121
|
|
|
$finder = new TransformedFinder($searchable, $transformer); |
122
|
|
|
|
123
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter', $finder->createPaginatorAdapter('')); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testCreateHybridPaginatorAdapter() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$searchable = $this->getMockBuilder('Elastica\SearchableInterface')->getMock(); |
129
|
|
|
$transformer = $this->getMockBuilder('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface')->getMock(); |
130
|
|
|
|
131
|
|
|
$finder = new TransformedFinder($searchable, $transformer); |
132
|
|
|
|
133
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter', $finder->createHybridPaginatorAdapter('')); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.