Completed
Pull Request — master (#1051)
by Karel
19:44 queued 09:44
created

testAnExceptionIsThrownWhenTheNumberOfFoundObjectsIsLessThanTheNumberOfResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
cc 1
eloc 13
nc 1
nop 2
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Doctrine;
4
5
use Elastica\Result;
6
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
7
use FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer;
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
    private function createMockPropertyAccessor()
65
    {
66
        $callback = function ($object, $identifier) {
67
            return $object->$identifier;
68
        };
69
70
        $propertyAccessor = $this->getMock('Symfony\Component\PropertyAccess\PropertyAccessorInterface');
71
        $propertyAccessor
72
            ->expects($this->any())
73
            ->method('getValue')
74
            ->with($this->isType('object'), $this->isType('string'))
75
            ->will($this->returnCallback($callback));
76
77
        return $propertyAccessor;
78
    }
79
80
    /**
81
     * @param array $options
82
     *
83
     * @return \PHPUnit_Framework_MockObject_MockObject|\FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer
84
     */
85
    private function createMockTransformer($options = array())
86
    {
87
        $objectClass = 'FOS\ElasticaBundle\Tests\Doctrine\Foo';
88
        $propertyAccessor = $this->createMockPropertyAccessor();
89
90
        $transformer = $this->getMockForAbstractClass(
91
            'FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer',
92
            array(null, $objectClass, $options)
93
        );
94
95
        $transformer->setPropertyAccessor($propertyAccessor);
96
97
        return $transformer;
98
    }
99
100
    public function testObjectClassCanBeSet()
101
    {
102
        $transformer = $this->createMockTransformer();
103
        $this->assertEquals('FOS\ElasticaBundle\Tests\Doctrine\Foo', $transformer->getObjectClass());
104
    }
105
106
    public function resultsWithMatchingObjects()
107
    {
108
        $elasticaResults = $doctrineObjects = array();
109
        for ($i=1; $i<4; $i++) {
110
            $elasticaResults[] = new Result(array('_id' => $i, 'highlight' => array('foo')));
111
            $doctrineObjects[] = new Foo($i);
112
        }
113
114
        return array(
115
            array($elasticaResults, $doctrineObjects)
116
        );
117
    }
118
119
    /**
120
     * @dataProvider resultsWithMatchingObjects
121
     */
122
    public function testObjectsAreTransformedByFindingThemByTheirIdentifiers($elasticaResults, $doctrineObjects)
123
    {
124
        $transformer = $this->createMockTransformer();
125
126
        $transformer
127
            ->expects($this->once())
128
            ->method('findByIdentifiers')
129
            ->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean'))
130
            ->will($this->returnValue($doctrineObjects));
131
132
        $transformedObjects = $transformer->transform($elasticaResults);
133
134
        $this->assertEquals($doctrineObjects, $transformedObjects);
135
    }
136
137
    /**
138
     * @dataProvider resultsWithMatchingObjects
139
     */
140
    public function testAnExceptionIsThrownWhenTheNumberOfFoundObjectsIsLessThanTheNumberOfResults(
141
        $elasticaResults,
142
        $doctrineObjects
0 ignored issues
show
Unused Code introduced by
The parameter $doctrineObjects is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    ) {
144
        $transformer = $this->createMockTransformer();
145
146
        $transformer
147
            ->expects($this->once())
148
            ->method('findByIdentifiers')
149
            ->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean'))
150
            ->will($this->returnValue(array()));
151
152
        $this->setExpectedException(
153
            '\RuntimeException',
154
            'Cannot find corresponding Doctrine objects for all Elastica results.'
155
        );
156
157
        $transformer->transform($elasticaResults);
158
    }
159
160
    /**
161
     * @dataProvider resultsWithMatchingObjects
162
     */
163
    public function testAnExceptionIsNotThrownWhenTheNumberOfFoundObjectsIsLessThanTheNumberOfResultsIfOptionSet(
164
        $elasticaResults,
165
        $doctrineObjects
0 ignored issues
show
Unused Code introduced by
The parameter $doctrineObjects is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
166
    ) {
167
        $transformer = $this->createMockTransformer(array('ignore_missing' => true));
168
169
        $transformer
170
            ->expects($this->once())
171
            ->method('findByIdentifiers')
172
            ->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean'))
173
            ->will($this->returnValue(array()));
174
175
        $results = $transformer->transform($elasticaResults);
176
177
        $this->assertEquals(array(), $results);
178
    }
179
180
    /**
181
     * @dataProvider resultsWithMatchingObjects
182
     */
183
    public function testHighlightsAreSetOnTransformedObjects($elasticaResults, $doctrineObjects)
184
    {
185
        $transformer = $this->createMockTransformer();
186
187
        $transformer
188
            ->expects($this->once())
189
            ->method('findByIdentifiers')
190
            ->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean'))
191
            ->will($this->returnValue($doctrineObjects));
192
193
        $results = $transformer->transform($elasticaResults);
194
195
        foreach($results as $result) {
196
            $this->assertInternalType('array', $result->highlights);
197
            $this->assertNotEmpty($result->highlights);
198
        }
199
    }
200
201
    /**
202
     * @dataProvider resultsWithMatchingObjects
203
     */
204
    public function testResultsAreSortedByIdentifier($elasticaResults, $doctrineObjects)
205
    {
206
        rsort($doctrineObjects);
207
208
        $transformer = $this->createMockTransformer();
209
210
        $transformer
211
            ->expects($this->once())
212
            ->method('findByIdentifiers')
213
            ->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean'))
214
            ->will($this->returnValue($doctrineObjects));
215
216
        $results = $transformer->transform($elasticaResults);
217
218
        $this->assertSame($doctrineObjects[2], $results[0]);
219
        $this->assertSame($doctrineObjects[1], $results[1]);
220
        $this->assertSame($doctrineObjects[0], $results[2]);
221
    }
222
223
    /**
224
     * @dataProvider resultsWithMatchingObjects
225
     */
226
    public function testHybridTransformReturnsDecoratedResults($elasticaResults, $doctrineObjects)
227
    {
228
        $transformer = $this->createMockTransformer();
229
230
        $transformer
231
            ->expects($this->once())
232
            ->method('findByIdentifiers')
233
            ->with($this->equalTo(array(1, 2, 3)), $this->isType('boolean'))
234
            ->will($this->returnValue($doctrineObjects));
235
236
        $results = $transformer->hybridTransform($elasticaResults);
237
238
        $this->assertNotEmpty($results);
239
240
        foreach ($results as $key => $result) {
241
            $this->assertInstanceOf('FOS\ElasticaBundle\HybridResult', $result);
242
            $this->assertSame($elasticaResults[$key], $result->getResult());
243
            $this->assertSame($doctrineObjects[$key], $result->getTransformed());
244
        }
245
    }
246
247
    public function testIdentifierFieldDefaultsToId()
248
    {
249
        $transformer = $this->createMockTransformer();
250
        $this->assertEquals('id', $transformer->getIdentifierField());
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