Completed
Push — master ( e4922e...fc487f )
by Karel
03:53
created

ResetterTest::testResetTypeWithChangedSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Tests\Index;
13
14
use Elastica\Request;
15
use Elastica\Type;
16
use FOS\ElasticaBundle\Configuration\IndexConfig;
17
use FOS\ElasticaBundle\Configuration\TypeConfig;
18
use FOS\ElasticaBundle\Elastica\Index;
19
use FOS\ElasticaBundle\Event\IndexResetEvent;
20
use FOS\ElasticaBundle\Event\TypeResetEvent;
21
use FOS\ElasticaBundle\Index\AliasProcessor;
22
use FOS\ElasticaBundle\Index\Resetter;
23
24
class ResetterTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @var Resetter
28
     */
29
    private $resetter;
30
31
    private $aliasProcessor;
32
    private $configManager;
33
    private $dispatcher;
34
    private $elasticaClient;
35
    private $indexManager;
36
    private $mappingBuilder;
37
38
    public function testResetAllIndexes()
39
    {
40
        $indexName = 'index1';
41
        $indexConfig = new IndexConfig($indexName, [], []);
42
        $this->mockIndex($indexName, $indexConfig);
43
44
        $this->configManager->expects($this->once())
45
            ->method('getIndexNames')
46
            ->will($this->returnValue([$indexName]));
47
48
        $this->dispatcherExpects([
49
            [IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
50
            [IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
51
        ]);
52
53
        $this->elasticaClient->expects($this->exactly(2))
54
            ->method('requestEndpoint');
55
56
        $this->resetter->resetAllIndexes();
57
    }
58
59 View Code Duplication
    public function testResetIndex()
60
    {
61
        $indexConfig = new IndexConfig('index1', [], []);
62
        $this->mockIndex('index1', $indexConfig);
63
64
        $this->dispatcherExpects([
65
            [IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
66
            [IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
67
        ]);
68
69
        $this->elasticaClient->expects($this->exactly(2))
70
            ->method('requestEndpoint');
71
72
        $this->resetter->resetIndex('index1');
73
    }
74
75 View Code Duplication
    public function testResetIndexWithDifferentName()
76
    {
77
        $indexConfig = new IndexConfig('index1', [], [
78
            'elasticSearchName' => 'notIndex1',
79
        ]);
80
        $this->mockIndex('index1', $indexConfig);
81
        $this->dispatcherExpects([
82
            [IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
83
            [IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
84
        ]);
85
86
        $this->elasticaClient->expects($this->exactly(2))
87
            ->method('requestEndpoint');
88
89
        $this->resetter->resetIndex('index1');
90
    }
91
92
    public function testResetIndexWithDifferentNameAndAlias()
93
    {
94
        $indexConfig = new IndexConfig('index1', [], [
95
            'elasticSearchName' => 'notIndex1',
96
            'useAlias' => true,
97
        ]);
98
        $index = $this->mockIndex('index1', $indexConfig);
99
        $this->dispatcherExpects([
100
            [IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
101
            [IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
102
        ]);
103
104
        $this->aliasProcessor->expects($this->once())
105
            ->method('switchIndexAlias')
106
            ->with($indexConfig, $index, false);
107
108
        $this->elasticaClient->expects($this->exactly(2))
109
            ->method('requestEndpoint');
110
111
        $this->resetter->resetIndex('index1');
112
    }
113
114
    /**
115
     * @expectedException \InvalidArgumentException
116
     */
117 View Code Duplication
    public function testFailureWhenMissingIndexDoesntDispatch()
118
    {
119
        $this->configManager->expects($this->once())
120
            ->method('getIndexConfiguration')
121
            ->with('nonExistant')
122
            ->will($this->throwException(new \InvalidArgumentException()));
123
124
        $this->indexManager->expects($this->never())
125
            ->method('getIndex');
126
127
        $this->resetter->resetIndex('nonExistant');
128
    }
129
130
    public function testResetType()
131
    {
132
        $typeConfig = new TypeConfig('type', [], []);
133
        $indexConfig = new IndexConfig('index', [], []);
134
        $this->mockType('type', 'index', $typeConfig, $indexConfig);
135
136
        $this->dispatcherExpects([
137
            [IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
138
            [IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')],
139
            [TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')],
140
            [TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')],
141
        ]);
142
143
        $this->elasticaClient->expects($this->exactly(3))
144
            ->method('requestEndpoint');
145
146
        $this->resetter->resetIndexType('index', 'type');
147
    }
148
149
    public function testResetTypeWithChangedSettings()
150
    {
151
        $settingsValue = [
152
            'analysis' => [
153
                'analyzer' => [
154
                    'test_analyzer' => [
155
                        'type' => 'standard',
156
                        'tokenizer' => 'standard',
157
                    ],
158
                ],
159
            ],
160
        ];
161
        $typeConfig = new TypeConfig('type', [], []);
162
        $indexConfig = new IndexConfig('index', [], ['settings' => $settingsValue]);
163
        $this->mockType('type', 'index', $typeConfig, $indexConfig);
164
165
        $this->elasticaClient->expects($this->exactly(3))
166
            ->method('requestEndpoint');
167
168
        $this->resetter->resetIndexType('index', 'type');
169
    }
170
171
    /**
172
     * @expectedException \InvalidArgumentException
173
     */
174 View Code Duplication
    public function testNonExistantResetType()
175
    {
176
        $this->configManager->expects($this->once())
177
            ->method('getTypeConfiguration')
178
            ->with('index', 'nonExistant')
179
            ->will($this->throwException(new \InvalidArgumentException()));
180
181
        $this->indexManager->expects($this->never())
182
            ->method('getIndex');
183
184
        $this->resetter->resetIndexType('index', 'nonExistant');
185
    }
186
187
    public function testPostPopulateWithoutAlias()
188
    {
189
        $this->mockIndex('index', new IndexConfig('index', [], []));
190
191
        $this->indexManager->expects($this->never())
192
            ->method('getIndex');
193
        $this->aliasProcessor->expects($this->never())
194
            ->method('switchIndexAlias');
195
196
        $this->resetter->switchIndexAlias('index');
197
    }
198
199
    public function testPostPopulate()
200
    {
201
        $indexConfig = new IndexConfig('index', [], ['useAlias' => true]);
202
        $index = $this->mockIndex('index', $indexConfig);
203
204
        $this->aliasProcessor->expects($this->once())
205
            ->method('switchIndexAlias')
206
            ->with($indexConfig, $index);
207
208
        $this->resetter->switchIndexAlias('index');
209
    }
210
211
    private function dispatcherExpects(array $events)
212
    {
213
        $expectation = $this->dispatcher->expects($this->exactly(count($events)))
214
            ->method('dispatch');
215
216
        call_user_func_array([$expectation, 'withConsecutive'], $events);
217
    }
218
219
    private function mockIndex($indexName, IndexConfig $config, $mapping = [])
220
    {
221
        $this->configManager->expects($this->atLeast(1))
222
            ->method('getIndexConfiguration')
223
            ->with($indexName)
224
            ->will($this->returnValue($config));
225
        $index = new Index($this->elasticaClient, $indexName);
226
        $this->indexManager->expects($this->any())
227
            ->method('getIndex')
228
            ->with($indexName)
229
            ->willReturn($index);
230
        $this->mappingBuilder->expects($this->any())
231
            ->method('buildIndexMapping')
232
            ->with($config)
233
            ->willReturn($mapping);
234
235
        return $index;
236
    }
237
238
    private function mockType($typeName, $indexName, TypeConfig $typeConfig, IndexConfig $indexConfig, $mapping = [])
239
    {
240
        $this->configManager->expects($this->atLeast(1))
241
            ->method('getTypeConfiguration')
242
            ->with($indexName, $typeName)
243
            ->will($this->returnValue($typeConfig));
244
        $index = new Index($this->elasticaClient, $indexName);
245
        $this->indexManager->expects($this->atLeast(2))
246
            ->method('getIndex')
247
            ->with($indexName)
248
            ->willReturn($index);
249
        $this->configManager->expects($this->atLeast(1))
250
            ->method('getIndexConfiguration')
251
            ->with($indexName)
252
            ->will($this->returnValue($indexConfig));
253
        $this->mappingBuilder->expects($this->any())
254
            ->method('buildIndexMapping')
255
            ->with($indexConfig)
256
            ->willReturn($mapping);
257
        $this->mappingBuilder->expects($this->once())
258
            ->method('buildTypeMapping')
259
            ->with($typeConfig)
260
            ->willReturn($mapping);
261
262
        return $index;
263
    }
264
265
    protected function setUp()
266
    {
267
        $this->aliasProcessor = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\AliasProcessor')
268
            ->disableOriginalConstructor()
269
            ->getMock();
270
        $this->configManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Configuration\\ConfigManager')
271
            ->disableOriginalConstructor()
272
            ->getMock();
273
        $this->dispatcher = $this->getMockBuilder('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
274
            ->getMock();
275
        $this->elasticaClient = $this->getMockBuilder('Elastica\\Client')
276
            ->disableOriginalConstructor()
277
            ->getMock();
278
        $this->indexManager = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\IndexManager')
279
            ->disableOriginalConstructor()
280
            ->getMock();
281
        $this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder')
282
            ->disableOriginalConstructor()
283
            ->getMock();
284
285
        $this->resetter = new Resetter(
286
            $this->configManager,
287
            $this->indexManager,
288
            $this->aliasProcessor,
289
            $this->mappingBuilder,
290
            $this->dispatcher
291
        );
292
    }
293
}
294