Completed
Push — master ( 9f9f6e...f57fd0 )
by Karel
06:27
created

IDObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Transformer;
4
5
use Elastica\Document;
6
use Elastica\Result;
7
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection;
8
9
class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var \FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection
13
     */
14
    protected $collection;
15
    protected $transformers = array();
16
17
    protected function collectionSetup()
18
    {
19
        $transformer1 = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
20
        $transformer1->expects($this->any())
21
            ->method('getObjectClass')
22
            ->will($this->returnValue('FOS\ElasticaBundle\Tests\Transformer\POPO'));
23
24
        $transformer1->expects($this->any())
25
            ->method('getIdentifierField')
26
            ->will($this->returnValue('id'));
27
28
        $transformer2 = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
29
        $transformer2->expects($this->any())
30
            ->method('getObjectClass')
31
            ->will($this->returnValue('FOS\ElasticaBundle\Tests\Transformer\POPO2'));
32
33
        $transformer2->expects($this->any())
34
            ->method('getIdentifierField')
35
            ->will($this->returnValue('id'));
36
37
        $this->collection = new ElasticaToModelTransformerCollection($this->transformers = array(
38
            'type1' => $transformer1,
39
            'type2' => $transformer2,
40
        ));
41
    }
42
43
    public function testGetObjectClass()
44
    {
45
        $this->collectionSetup();
46
47
        $objectClasses = $this->collection->getObjectClass();
48
        $this->assertEquals(array(
49
            'type1' => 'FOS\ElasticaBundle\Tests\Transformer\POPO',
50
            'type2' => 'FOS\ElasticaBundle\Tests\Transformer\POPO2',
51
        ), $objectClasses);
52
    }
53
54
    public function testTransformDelegatesToTransformers()
55
    {
56
        $this->collectionSetup();
57
58
        $document1 = new Document(123, array('data' => 'lots of data'), 'type1');
59
        $document2 = new Document(124, array('data' => 'not so much data'), 'type2');
60
        $result1 = new POPO(123, 'lots of data');
61
        $result2 = new POPO2(124, 'not so much data');
62
63
        $this->transformers['type1']->expects($this->once())
64
            ->method('transform')
65
            ->with(array($document1))
66
            ->will($this->returnValue(array($result1)));
67
68
        $this->transformers['type2']->expects($this->once())
69
            ->method('transform')
70
            ->with(array($document2))
71
            ->will($this->returnValue(array($result2)));
72
73
        $results = $this->collection->transform(array($document1, $document2));
74
75
        $this->assertEquals(array(
76
            $result1,
77
            $result2,
78
        ), $results);
79
    }
80
81
    public function testTransformOrder()
82
    {
83
        $this->collectionSetup();
84
85
        $document1 = new Document(123, array('data' => 'lots of data'), 'type1');
86
        $document2 = new Document(124, array('data' => 'not so much data'), 'type1');
87
        $result1 = new POPO(123, 'lots of data');
88
        $result2 = new POPO2(124, 'not so much data');
89
90
        $this->transformers['type1']->expects($this->once())
91
         ->method('transform')
92
         ->with(array($document1, $document2))
93
         ->will($this->returnValue(array($result1, $result2)));
94
95
        $results = $this->collection->transform(array($document1, $document2));
96
97
        $this->assertEquals(array(
98
            $result1,
99
            $result2,
100
        ), $results);
101
    }
102
103
    public function testTransformOrderWithIdAsObject()
104
    {
105
        $this->collectionSetup();
106
107
        $id1 = 'yo';
108
        $id2 = 'lo';
109
        $idObject1 = new IDObject($id1);
110
        $idObject2 = new IDObject($id2);
111
        $document1 = new Document($idObject1, array('data' => 'lots of data'), 'type1');
112
        $document2 = new Document($idObject2, array('data' => 'not so much data'), 'type1');
113
        $result1 = new POPO($idObject1, 'lots of data');
114
        $result2 = new POPO2($idObject2, 'not so much data');
115
116
        $this->transformers['type1']->expects($this->once())
117
         ->method('transform')
118
         ->with(array($document1, $document2))
119
         ->will($this->returnValue(array($result1, $result2)));
120
121
        $results = $this->collection->transform(array($document1, $document2));
122
123
        $this->assertEquals(array(
124
            $result1,
125
            $result2,
126
        ), $results);
127
    }
128
129
    public function testGetIdentifierFieldReturnsAMapOfIdentifiers()
130
    {
131
        $collection = new ElasticaToModelTransformerCollection(array());
132
        $identifiers = $collection->getIdentifierField();
133
        $this->assertInternalType('array', $identifiers);
134
        $this->assertEmpty($identifiers);
135
136
        $this->collectionSetup();
137
        $identifiers = $this->collection->getIdentifierField();
138
        $this->assertInternalType('array', $identifiers);
139
        $this->assertEquals(array('type1' => 'id', 'type2' => 'id'), $identifiers);
140
    }
141
142
    public function elasticaResults()
143
    {
144
        $result = new Result(array('_id' => 123, '_type' => 'type1'));
145
        $transformedObject = new POPO(123, array());
146
147
        return array(
148
            array(
149
                $result, $transformedObject,
150
            ),
151
        );
152
    }
153
154
    /**
155
     * @dataProvider elasticaResults
156
     */
157
    public function testHybridTransformDecoratesResultsWithHybridResultObjects($result, $transformedObject)
158
    {
159
        $transformer = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
160
        $transformer->expects($this->any())->method('getIdentifierField')->will($this->returnValue('id'));
161
162
        $transformer
163
            ->expects($this->any())
164
            ->method('transform')
165
            ->will($this->returnValue(array($transformedObject)));
166
167
        $collection = new ElasticaToModelTransformerCollection(array('type1' => $transformer));
168
169
        $hybridResults = $collection->hybridTransform(array($result));
170
171
        $this->assertInternalType('array', $hybridResults);
172
        $this->assertNotEmpty($hybridResults);
173
        $this->assertContainsOnlyInstancesOf('FOS\ElasticaBundle\HybridResult', $hybridResults);
174
175
        $hybridResult = array_pop($hybridResults);
176
        $this->assertEquals($result, $hybridResult->getResult());
177
        $this->assertEquals($transformedObject, $hybridResult->getTransformed());
178
    }
179
}
180
181
class POPO
182
{
183
    public $id;
184
    public $data;
185
186
    /**
187
     * @param mixed $id
188
     */
189
    public function __construct($id, $data)
190
    {
191
        $this->data = $data;
192
        $this->id = $id;
193
    }
194
195
    public function getId()
196
    {
197
        return $this->id;
198
    }
199
}
200
201
class POPO2 extends POPO
202
{
203
}
204
205
class IDObject
206
{
207
    protected $id;
208
209
    /**
210
     * @param int|string $id
211
     */
212
    public function __construct($id)
213
    {
214
        $this->id = $id;
215
    }
216
217
    public function __toString()
218
    {
219
        return (string) $this->id;
220
    }
221
}
222