Completed
Push — master ( cd4d3e...4fea37 )
by Karel
06:37
created

ResetterTest::testResetIndexWithDifferentName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Index;
4
5
use Elastica\Exception\ResponseException;
6
use Elastica\Request;
7
use Elastica\Response;
8
use Elastica\Type;
9
use Elastica\Type\Mapping;
10
use FOS\ElasticaBundle\Configuration\IndexConfig;
11
use FOS\ElasticaBundle\Configuration\TypeConfig;
12
use FOS\ElasticaBundle\Elastica\Index;
13
use FOS\ElasticaBundle\Event\IndexResetEvent;
14
use FOS\ElasticaBundle\Event\TypeResetEvent;
15
use FOS\ElasticaBundle\Index\AliasProcessor;
16
use FOS\ElasticaBundle\Index\Resetter;
17
18
class ResetterTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var Resetter
22
     */
23
    private $resetter;
24
25
    private $aliasProcessor;
26
    private $configManager;
27
    private $dispatcher;
28
    private $elasticaClient;
29
    private $indexManager;
30
    private $mappingBuilder;
31
32
    public function testResetAllIndexes()
33
    {
34
        $indexName = 'index1';
35
        $indexConfig = new IndexConfig($indexName, array(), array());
36
        $this->mockIndex($indexName, $indexConfig);
37
38
        $this->configManager->expects($this->once())
39
            ->method('getIndexNames')
40
            ->will($this->returnValue(array($indexName)));
41
42
        $this->dispatcherExpects(array(
43
            array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
44
            array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
45
        ));
46
47
        $this->elasticaClient->expects($this->exactly(2))
48
            ->method('request')
49
            ->withConsecutive(
50
                array('index1/', 'DELETE'),
51
                array('index1/', 'PUT', array(), array())
52
            );
53
54
        $this->resetter->resetAllIndexes();
55
    }
56
57
    public function testResetIndex()
58
    {
59
        $indexConfig = new IndexConfig('index1', array(), array());
60
        $this->mockIndex('index1', $indexConfig);
61
62
        $this->dispatcherExpects(array(
63
            array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
64
            array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
65
        ));
66
67
        $this->elasticaClient->expects($this->exactly(2))
68
            ->method('request')
69
            ->withConsecutive(
70
                array('index1/', 'DELETE'),
71
                array('index1/', 'PUT', array(), array())
72
            );
73
74
        $this->resetter->resetIndex('index1');
75
    }
76
77
    public function testResetIndexWithDifferentName()
78
    {
79
        $indexConfig = new IndexConfig('index1', array(), array(
80
            'elasticSearchName' => 'notIndex1'
81
        ));
82
        $this->mockIndex('index1', $indexConfig);
83
        $this->dispatcherExpects(array(
84
            array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
85
            array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
86
        ));
87
88
        $this->elasticaClient->expects($this->exactly(2))
89
            ->method('request')
90
            ->withConsecutive(
91
                array('index1/', 'DELETE'),
92
                array('index1/', 'PUT', array(), array())
93
            );
94
95
        $this->resetter->resetIndex('index1');
96
    }
97
98
    public function testResetIndexWithDifferentNameAndAlias()
99
    {
100
        $indexConfig = new IndexConfig('index1', array(), array(
101
            'elasticSearchName' => 'notIndex1',
102
            'useAlias' => true
103
        ));
104
        $index = $this->mockIndex('index1', $indexConfig);
105
        $this->dispatcherExpects(array(
106
            array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')),
107
            array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent'))
108
        ));
109
110
        $this->aliasProcessor->expects($this->once())
111
            ->method('switchIndexAlias')
112
            ->with($indexConfig, $index, false);
113
114
        $this->elasticaClient->expects($this->exactly(2))
115
            ->method('request')
116
            ->withConsecutive(
117
                array('index1/', 'DELETE'),
118
                array('index1/', 'PUT', array(), array())
119
            );
120
121
        $this->resetter->resetIndex('index1');
122
    }
123
124
    /**
125
     * @expectedException \InvalidArgumentException
126
     */
127 View Code Duplication
    public function testFailureWhenMissingIndexDoesntDispatch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
128
    {
129
        $this->configManager->expects($this->once())
130
            ->method('getIndexConfiguration')
131
            ->with('nonExistant')
132
            ->will($this->throwException(new \InvalidArgumentException));
133
134
        $this->indexManager->expects($this->never())
135
            ->method('getIndex');
136
137
        $this->resetter->resetIndex('nonExistant');
138
    }
139
140
    public function testResetType()
141
    {
142
        $typeConfig = new TypeConfig('type', array(), array());
143
        $indexConfig = new IndexConfig('index', array(), array());
144
        $this->mockType('type', 'index', $typeConfig, $indexConfig);
145
146
        $this->dispatcherExpects(array(
147
            array(TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')),
148
            array(TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent'))
149
        ));
150
151
        $this->elasticaClient->expects($this->exactly(2))
152
            ->method('request')
153
            ->withConsecutive(
154
                array('index/type/', 'DELETE'),
155
                array('index/type/_mapping', 'PUT', array('type' => array()), array())
156
            );
157
158
        $this->resetter->resetIndexType('index', 'type');
159
    }
160
161
    public function testResetTypeWithChangedSettings()
162
    {
163
        $settingsValue = array(
164
            'analysis' => array(
165
                'analyzer' => array(
166
                    'test_analyzer' => array(
167
                        'type' => 'standard',
168
                        'tokenizer' => 'standard'
169
                    )
170
                )
171
            )
172
        );
173
        $typeConfig = new TypeConfig('type', array(), array());
174
        $indexConfig = new IndexConfig('index', array(), array('settings' => $settingsValue));
175
        $this->mockType('type', 'index', $typeConfig, $indexConfig);
176
177
        $this->elasticaClient->expects($this->exactly(5))
178
            ->method('request')
179
            ->withConsecutive(
180
                array('index/type/', 'DELETE'),
181
                array('index/_close', 'POST'),
182
                array('index/_settings', 'PUT', $settingsValue),
183
                array('index/_open', 'POST'),
184
                array('index/type/_mapping', 'PUT', array('type' => array()), array())
185
            );
186
187
        $this->resetter->resetIndexType('index', 'type');
188
    }
189
190
    /**
191
     * @expectedException \InvalidArgumentException
192
     */
193 View Code Duplication
    public function testNonExistantResetType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
194
    {
195
        $this->configManager->expects($this->once())
196
            ->method('getTypeConfiguration')
197
            ->with('index', 'nonExistant')
198
            ->will($this->throwException(new \InvalidArgumentException));
199
200
        $this->indexManager->expects($this->never())
201
            ->method('getIndex');
202
203
        $this->resetter->resetIndexType('index', 'nonExistant');
204
    }
205
206
    public function testPostPopulateWithoutAlias()
207
    {
208
        $this->mockIndex('index', new IndexConfig('index', array(), array()));
209
210
        $this->indexManager->expects($this->never())
211
            ->method('getIndex');
212
        $this->aliasProcessor->expects($this->never())
213
            ->method('switchIndexAlias');
214
215
        $this->resetter->postPopulate('index');
216
    }
217
218
    public function testPostPopulate()
219
    {
220
        $indexConfig = new IndexConfig('index', array(), array( 'useAlias' => true));
221
        $index = $this->mockIndex('index', $indexConfig);
222
223
        $this->aliasProcessor->expects($this->once())
224
            ->method('switchIndexAlias')
225
            ->with($indexConfig, $index);
226
227
        $this->resetter->postPopulate('index');
228
    }
229
230
    private function dispatcherExpects(array $events)
231
    {
232
        $expectation = $this->dispatcher->expects($this->exactly(count($events)))
233
            ->method('dispatch');
234
235
        call_user_func_array(array($expectation, 'withConsecutive'), $events);
236
    }
237
238
    private function mockIndex($indexName, IndexConfig $config, $mapping = array())
239
    {
240
        $this->configManager->expects($this->atLeast(1))
241
            ->method('getIndexConfiguration')
242
            ->with($indexName)
243
            ->will($this->returnValue($config));
244
        $index = new Index($this->elasticaClient, $indexName);
245
        $this->indexManager->expects($this->any())
246
            ->method('getIndex')
247
            ->with($indexName)
248
            ->willReturn($index);
249
        $this->mappingBuilder->expects($this->any())
250
            ->method('buildIndexMapping')
251
            ->with($config)
252
            ->willReturn($mapping);
253
254
        return $index;
255
    }
256
257
    private function mockType($typeName, $indexName, TypeConfig $typeConfig, IndexConfig $indexConfig, $mapping = array())
258
    {
259
        $this->configManager->expects($this->atLeast(1))
260
            ->method('getTypeConfiguration')
261
            ->with($indexName, $typeName)
262
            ->will($this->returnValue($typeConfig));
263
        $index = new Index($this->elasticaClient, $indexName);
264
        $this->indexManager->expects($this->once())
265
            ->method('getIndex')
266
            ->with($indexName)
267
            ->willReturn($index);
268
        $this->configManager->expects($this->atLeast(1))
269
            ->method('getIndexConfiguration')
270
            ->with($indexName)
271
            ->will($this->returnValue($indexConfig));
272
        $this->mappingBuilder->expects($this->once())
273
            ->method('buildTypeMapping')
274
            ->with($typeConfig)
275
            ->willReturn($mapping);
276
277
        return $index;
278
    }
279
280
    protected function setUp()
281
    {
282
        $this->aliasProcessor = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\AliasProcessor')
283
            ->disableOriginalConstructor()
284
            ->getMock();
285
        $this->configManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Configuration\\ConfigManager')
286
            ->disableOriginalConstructor()
287
            ->getMock();
288
        $this->dispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
289
            ->getMock();
290
        $this->elasticaClient = $this->getMockBuilder('Elastica\\Client')
291
            ->disableOriginalConstructor()
292
            ->getMock();
293
        $this->indexManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\IndexManager')
294
            ->disableOriginalConstructor()
295
            ->getMock();
296
        $this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder')
297
            ->disableOriginalConstructor()
298
            ->getMock();
299
300
        $this->resetter = new Resetter(
301
            $this->configManager,
302
            $this->indexManager,
303
            $this->aliasProcessor,
304
            $this->mappingBuilder,
305
            $this->dispatcher
306
        );
307
    }
308
}
309