CreateIndexHandler::newClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
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