1 | <?php |
||
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() |
||
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) |
||
179 | } |
||
180 | |||
181 | class POPO |
||
182 | { |
||
183 | public $id; |
||
184 | public $data; |
||
185 | |||
222 |