1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Tests\Doctrine; |
4
|
|
|
|
5
|
|
|
use Elastica\Result; |
6
|
|
|
use FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer; |
7
|
|
|
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; |
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
9
|
|
|
|
10
|
|
|
class AbstractElasticaToModelTransformerTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Doctrine\Common\Persistence\ManagerRegistry|\PHPUnit_Framework_MockObject_MockObject |
14
|
|
|
*/ |
15
|
|
|
protected $registry; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $objectClass = 'stdClass'; |
21
|
|
|
|
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') |
25
|
|
|
->disableOriginalConstructor() |
26
|
|
|
->getMock(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tests if ignore_missing option is properly handled in transformHybrid() method. |
31
|
|
|
*/ |
32
|
|
|
public function testIgnoreMissingOptionDuringTransformHybrid() |
33
|
|
|
{ |
34
|
|
|
$transformer = $this->getMock( |
35
|
|
|
'FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer', |
36
|
|
|
array('findByIdentifiers'), |
37
|
|
|
array($this->registry, $this->objectClass, array('ignore_missing' => true)) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$transformer->setPropertyAccessor(PropertyAccess::createPropertyAccessor()); |
41
|
|
|
|
42
|
|
|
$firstOrmResult = new \stdClass(); |
43
|
|
|
$firstOrmResult->id = 1; |
44
|
|
|
$secondOrmResult = new \stdClass(); |
45
|
|
|
$secondOrmResult->id = 3; |
46
|
|
|
$transformer->expects($this->once()) |
47
|
|
|
->method('findByIdentifiers') |
48
|
|
|
->with(array(1, 2, 3)) |
49
|
|
|
->willReturn(array($firstOrmResult, $secondOrmResult)); |
50
|
|
|
|
51
|
|
|
$firstElasticaResult = new Result(array('_id' => 1)); |
52
|
|
|
$secondElasticaResult = new Result(array('_id' => 2)); |
53
|
|
|
$thirdElasticaResult = new Result(array('_id' => 3)); |
54
|
|
|
|
55
|
|
|
$hybridResults = $transformer->hybridTransform(array($firstElasticaResult, $secondElasticaResult, $thirdElasticaResult)); |
56
|
|
|
|
57
|
|
|
$this->assertCount(2, $hybridResults); |
58
|
|
|
$this->assertEquals($firstOrmResult, $hybridResults[0]->getTransformed()); |
59
|
|
|
$this->assertEquals($firstElasticaResult, $hybridResults[0]->getResult()); |
60
|
|
|
$this->assertEquals($secondOrmResult, $hybridResults[1]->getTransformed()); |
61
|
|
|
$this->assertEquals($thirdElasticaResult, $hybridResults[1]->getResult()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testObjectClassCanBeSet() |
65
|
|
|
{ |
66
|
|
|
$transformer = $this->createMockTransformer(); |
67
|
|
|
$this->assertEquals('FOS\ElasticaBundle\Tests\Doctrine\Foo', $transformer->getObjectClass()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function resultsWithMatchingObjects() |
71
|
|
|
{ |
72
|
|
|
$elasticaResults = $doctrineObjects = array(); |
73
|
|
|
for ($i=1; $i<4; $i++) { |
74
|
|
|
$elasticaResults[] = new Result(array('_id' => $i, 'highlight' => array('foo'))); |
75
|
|
|
$doctrineObjects[] = new Foo($i); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return array( |
79
|
|
|
array($elasticaResults, $doctrineObjects) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @dataProvider resultsWithMatchingObjects |
85
|
|
|
*/ |
86
|
|
|
public function testObjectsAreTransformedByFindingThemByTheirIdentifiers($elasticaResults, $doctrineObjects) |
87
|
|
|
{ |
88
|
|
|
$transformer = $this->createMockTransformer(); |
89
|
|
|
|
90
|
|
|
$transformer |
91
|
|
|
->expects($this->once()) |
92
|
|
|
->method('findByIdentifiers') |
93
|
|
|
->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean')) |
94
|
|
|
->will($this->returnValue($doctrineObjects)); |
95
|
|
|
|
96
|
|
|
$transformedObjects = $transformer->transform($elasticaResults); |
97
|
|
|
|
98
|
|
|
$this->assertEquals($doctrineObjects, $transformedObjects); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @dataProvider resultsWithMatchingObjects |
103
|
|
|
*/ |
104
|
|
|
public function testAnExceptionIsThrownWhenTheNumberOfFoundObjectsIsLessThanTheNumberOfResults( |
105
|
|
|
$elasticaResults, |
106
|
|
|
$doctrineObjects |
|
|
|
|
107
|
|
|
) { |
108
|
|
|
$transformer = $this->createMockTransformer(); |
109
|
|
|
|
110
|
|
|
$transformer |
111
|
|
|
->expects($this->once()) |
112
|
|
|
->method('findByIdentifiers') |
113
|
|
|
->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean')) |
114
|
|
|
->will($this->returnValue(array())); |
115
|
|
|
|
116
|
|
|
$this->setExpectedException( |
117
|
|
|
'\RuntimeException', |
118
|
|
|
'Cannot find corresponding Doctrine objects for all Elastica results.' |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$transformer->transform($elasticaResults); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @dataProvider resultsWithMatchingObjects |
126
|
|
|
*/ |
127
|
|
|
public function testAnExceptionIsNotThrownWhenTheNumberOfFoundObjectsIsLessThanTheNumberOfResultsIfOptionSet( |
128
|
|
|
$elasticaResults, |
129
|
|
|
$doctrineObjects |
|
|
|
|
130
|
|
|
) { |
131
|
|
|
$transformer = $this->createMockTransformer(array('ignore_missing' => true)); |
132
|
|
|
|
133
|
|
|
$transformer |
134
|
|
|
->expects($this->once()) |
135
|
|
|
->method('findByIdentifiers') |
136
|
|
|
->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean')) |
137
|
|
|
->will($this->returnValue(array())); |
138
|
|
|
|
139
|
|
|
$results = $transformer->transform($elasticaResults); |
140
|
|
|
|
141
|
|
|
$this->assertEquals(array(), $results); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @dataProvider resultsWithMatchingObjects |
146
|
|
|
*/ |
147
|
|
|
public function testHighlightsAreSetOnTransformedObjects($elasticaResults, $doctrineObjects) |
148
|
|
|
{ |
149
|
|
|
$transformer = $this->createMockTransformer(); |
150
|
|
|
|
151
|
|
|
$transformer |
152
|
|
|
->expects($this->once()) |
153
|
|
|
->method('findByIdentifiers') |
154
|
|
|
->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean')) |
155
|
|
|
->will($this->returnValue($doctrineObjects)); |
156
|
|
|
|
157
|
|
|
$results = $transformer->transform($elasticaResults); |
158
|
|
|
|
159
|
|
|
foreach($results as $result) { |
160
|
|
|
$this->assertInternalType('array', $result->highlights); |
161
|
|
|
$this->assertNotEmpty($result->highlights); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @dataProvider resultsWithMatchingObjects |
167
|
|
|
*/ |
168
|
|
|
public function testResultsAreSortedByIdentifier($elasticaResults, $doctrineObjects) |
169
|
|
|
{ |
170
|
|
|
rsort($doctrineObjects); |
171
|
|
|
|
172
|
|
|
$transformer = $this->createMockTransformer(); |
173
|
|
|
|
174
|
|
|
$transformer |
175
|
|
|
->expects($this->once()) |
176
|
|
|
->method('findByIdentifiers') |
177
|
|
|
->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean')) |
178
|
|
|
->will($this->returnValue($doctrineObjects)); |
179
|
|
|
|
180
|
|
|
$results = $transformer->transform($elasticaResults); |
181
|
|
|
|
182
|
|
|
$this->assertSame($doctrineObjects[2], $results[0]); |
183
|
|
|
$this->assertSame($doctrineObjects[1], $results[1]); |
184
|
|
|
$this->assertSame($doctrineObjects[0], $results[2]); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @dataProvider resultsWithMatchingObjects |
189
|
|
|
*/ |
190
|
|
|
public function testHybridTransformReturnsDecoratedResults($elasticaResults, $doctrineObjects) |
191
|
|
|
{ |
192
|
|
|
$transformer = $this->createMockTransformer(); |
193
|
|
|
|
194
|
|
|
$transformer |
195
|
|
|
->expects($this->once()) |
196
|
|
|
->method('findByIdentifiers') |
197
|
|
|
->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean')) |
198
|
|
|
->will($this->returnValue($doctrineObjects)); |
199
|
|
|
|
200
|
|
|
$results = $transformer->hybridTransform($elasticaResults); |
201
|
|
|
|
202
|
|
|
$this->assertNotEmpty($results); |
203
|
|
|
|
204
|
|
|
foreach ($results as $key => $result) { |
205
|
|
|
$this->assertInstanceOf('FOS\ElasticaBundle\HybridResult', $result); |
206
|
|
|
$this->assertSame($elasticaResults[$key], $result->getResult()); |
207
|
|
|
$this->assertSame($doctrineObjects[$key], $result->getTransformed()); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testIdentifierFieldDefaultsToId() |
212
|
|
|
{ |
213
|
|
|
$transformer = $this->createMockTransformer(); |
214
|
|
|
$this->assertEquals('id', $transformer->getIdentifierField()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function createMockPropertyAccessor() |
218
|
|
|
{ |
219
|
|
|
$callback = function ($object, $identifier) { |
220
|
|
|
return $object->$identifier; |
221
|
|
|
}; |
222
|
|
|
|
223
|
|
|
$propertyAccessor = $this->getMock('Symfony\Component\PropertyAccess\PropertyAccessorInterface'); |
224
|
|
|
$propertyAccessor |
225
|
|
|
->expects($this->any()) |
226
|
|
|
->method('getValue') |
227
|
|
|
->with($this->isType('object'), $this->isType('string')) |
228
|
|
|
->will($this->returnCallback($callback)); |
229
|
|
|
|
230
|
|
|
return $propertyAccessor; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $options |
235
|
|
|
* |
236
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer |
237
|
|
|
*/ |
238
|
|
|
private function createMockTransformer($options = array()) |
239
|
|
|
{ |
240
|
|
|
$objectClass = 'FOS\ElasticaBundle\Tests\Doctrine\Foo'; |
241
|
|
|
$propertyAccessor = $this->createMockPropertyAccessor(); |
242
|
|
|
|
243
|
|
|
$transformer = $this->getMockForAbstractClass( |
244
|
|
|
'FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer', |
245
|
|
|
array($this->registry, $objectClass, $options) |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
$transformer->setPropertyAccessor($propertyAccessor); |
249
|
|
|
|
250
|
|
|
return $transformer; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
class Foo implements HighlightableModelInterface |
255
|
|
|
{ |
256
|
|
|
public $id; |
257
|
|
|
public $highlights; |
258
|
|
|
|
259
|
|
|
public function __construct($id) |
260
|
|
|
{ |
261
|
|
|
$this->id = $id; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function getId() |
265
|
|
|
{ |
266
|
|
|
return $this->id; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function setElasticHighlights(array $highlights) |
270
|
|
|
{ |
271
|
|
|
$this->highlights = $highlights; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.