CreateIndexHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testHandle() 0 21 1
A newIndices() 0 7 1
A newClient() 0 13 1
A newConfigRepository() 0 21 2
1
<?php
2
3
namespace GBProd\Tests\Units\ElasticsearchExtraBundle\Handler;
4
5
use atoum;
6
use mock\Elasticsearch\Client;
7
use mock\Elasticsearch\Namespaces\IndicesNamespace;
8
use mock\GBProd\ElasticsearchExtraBundle\Repository\ClientRepository;
9
use mock\GBProd\ElasticsearchExtraBundle\Repository\IndexConfigurationRepository;
10
11
/**
12
 * Tests for CreateIndexHandler
13
 *
14
 * @author gbprod <[email protected]>
15
 */
16
class CreateIndexHandler extends atoum
17
{
18
    public function testHandle()
19
    {
20
        $this
21
            ->given($config = ['my' => ['awesome' => 'config']])
22
                ->and($indices = $this->newIndices())
23
                ->and($client = $this->newClient($indices))
24
                ->and($configRepository = $this->newConfigRepository('my_index', $config))
25
                ->and($this->newTestedInstance($configRepository))
26
            ->if($this->testedInstance->handle($client, 'my_index'))
27
            ->then
28
                ->mock($indices)
29
                    ->call('create')
30
                        ->withArguments(
31
                            [
32
                                'index' => 'my_index',
33
                                'body'  => $config,
34
                            ]
35
                        )
36
                        ->once()
37
        ;
38
    }
39
40
    private function newIndices()
41
    {
42
        $this->mockGenerator->shuntParentClassCalls();
43
        $this->mockGenerator->orphanize('__construct');
44
45
        return new IndicesNamespace();
46
    }
47
48
    private function newClient($indices)
49
    {
50
        $this->mockGenerator->shuntParentClassCalls();
51
        $this->mockGenerator->orphanize('__construct');
52
53
        $client = new Client();
54
55
        $this->calling($client)->indices = function() use ($indices) {
56
            return $indices;
57
        };
58
59
        return $client;
60
    }
61
62
    private function newConfigRepository($index, $config)
63
    {
64
        $this->mockGenerator->shuntParentClassCalls();
65
        $this->mockGenerator->orphanize('__construct');
66
67
        $configRepository = new IndexConfigurationRepository();
68
69
        $this->calling($configRepository)->get =
70
            function($indexParam) use ($index, $config) {
71
                if ($index == $indexParam) {
72
                    return $config;
73
                }
74
75
                return null;
76
            }
77
        ;
78
79
        $this->mockGenerator->unshuntParentClassCalls();
80
81
        return $configRepository;
82
    }
83
}
84