Code Duplication    Length = 105-107 lines in 2 locations

tests/Units/ElasticsearchExtraBundle/Handler/CreateIndexHandler.php 1 location

@@ 16-122 (lines=107) @@
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($clientRepository = $this->newClientRepository('my_client', $client))
25
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
26
                ->and($this->newTestedInstance($clientRepository, $configRepository))
27
            ->if($this->testedInstance->handle('my_client', 'my_index'))
28
            ->then
29
                ->mock($indices)
30
                    ->call('create')
31
                        ->withArguments(
32
                            [
33
                                'index' => 'my_index',
34
                                'body'  => $config,
35
                            ]
36
                        )
37
                        ->once()
38
        ;
39
    }
40
    
41
    private function newIndices()
42
    {
43
        $this->mockGenerator->shuntParentClassCalls();
44
        $this->mockGenerator->orphanize('__construct');
45
46
        return new IndicesNamespace();
47
    }
48
    
49
    private function newClient($indices)
50
    {
51
        $this->mockGenerator->shuntParentClassCalls();
52
        $this->mockGenerator->orphanize('__construct');
53
54
        $client = new Client();
55
        
56
        $this->calling($client)->indices = function() use ($indices) {
57
            return $indices;
58
        };
59
        
60
        return $client;
61
    }
62
    
63
    private function newClientRepository($clientId, $client)
64
    {
65
        $this->mockGenerator->shuntParentClassCalls();
66
        $this->mockGenerator->orphanize('__construct');
67
68
        $clientRepository = new ClientRepository();
69
        
70
        $this->calling($clientRepository)->get = 
71
            function($id) use ($clientId, $client) {
72
                if ($id == $clientId) {
73
                    return $client;
74
                }
75
                
76
                return null;
77
            }
78
        ;
79
80
        $this->mockGenerator->unshuntParentClassCalls();
81
82
        return $clientRepository;
83
    }
84
    
85
    private function newConfigRepository($clientId, $indexId, $config)
86
    {
87
        $this->mockGenerator->shuntParentClassCalls();
88
        $this->mockGenerator->orphanize('__construct');
89
90
        $configRepository = new IndexConfigurationRepository();
91
        
92
        $this->calling($configRepository)->get = 
93
            function($clientIdParam, $indexIdParam) use ($clientId, $indexId, $config) {
94
                if ($clientId == $clientIdParam && $indexId == $indexIdParam) {
95
                    return $config;
96
                }
97
                
98
                return null;
99
            }
100
        ;
101
102
        $this->mockGenerator->unshuntParentClassCalls();
103
        
104
        return $configRepository;
105
    }
106
    
107
    public function testHandleThrowExceptionIfNoClient()
108
    {
109
        $this
110
            ->given($config = ['my' => ['awesome' => 'config']])
111
                ->and($indices = $this->newIndices())
112
                ->and($client = $this->newClient($indices))
113
                ->and($clientRepository = $this->newClientRepository('my_client', null))
114
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
115
                ->and($this->newTestedInstance($clientRepository, $configRepository))
116
            ->exception(function() {
117
                    $this->testedInstance->handle('my_client', 'my_index');
118
                })
119
                ->isInstanceOf(\InvalidArgumentException::class)
120
        ;
121
    }
122
}

tests/Units/ElasticsearchExtraBundle/Handler/DeleteIndexHandler.php 1 location

@@ 16-120 (lines=105) @@
13
 * 
14
 * @author gbprod <[email protected]>
15
 */
16
class DeleteIndexHandler 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($clientRepository = $this->newClientRepository('my_client', $client))
25
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
26
                ->and($this->newTestedInstance($clientRepository, $configRepository))
27
            ->if($this->testedInstance->handle('my_client', 'my_index'))
28
            ->then
29
                ->mock($indices)
30
                    ->call('delete')
31
                        ->withArguments([
32
                                'index' => 'my_index',
33
                            ]
34
                        )
35
                        ->once()
36
        ;
37
    }
38
    
39
    private function newIndices()
40
    {
41
        $this->mockGenerator->shuntParentClassCalls();
42
        $this->mockGenerator->orphanize('__construct');
43
44
        return new IndicesNamespace();
45
    }
46
    
47
    private function newClient($indices)
48
    {
49
        $this->mockGenerator->shuntParentClassCalls();
50
        $this->mockGenerator->orphanize('__construct');
51
52
        $client = new Client();
53
        
54
        $this->calling($client)->indices = function() use ($indices) {
55
            return $indices;
56
        };
57
        
58
        return $client;
59
    }
60
    
61
    private function newClientRepository($clientId, $client)
62
    {
63
        $this->mockGenerator->shuntParentClassCalls();
64
        $this->mockGenerator->orphanize('__construct');
65
66
        $clientRepository = new ClientRepository();
67
        
68
        $this->calling($clientRepository)->get = 
69
            function($id) use ($clientId, $client) {
70
                if ($id == $clientId) {
71
                    return $client;
72
                }
73
                
74
                return null;
75
            }
76
        ;
77
78
        $this->mockGenerator->unshuntParentClassCalls();
79
80
        return $clientRepository;
81
    }
82
    
83
    private function newConfigRepository($clientId, $indexId, $config)
84
    {
85
        $this->mockGenerator->shuntParentClassCalls();
86
        $this->mockGenerator->orphanize('__construct');
87
88
        $configRepository = new IndexConfigurationRepository();
89
        
90
        $this->calling($configRepository)->get = 
91
            function($clientIdParam, $indexIdParam) use ($clientId, $indexId, $config) {
92
                if ($clientId == $clientIdParam && $indexId == $indexIdParam) {
93
                    return $config;
94
                }
95
                
96
                return null;
97
            }
98
        ;
99
100
        $this->mockGenerator->unshuntParentClassCalls();
101
        
102
        return $configRepository;
103
    }
104
    
105
    public function testHandleThrowExceptionIfNoClient()
106
    {
107
        $this
108
            ->given($config = ['my' => ['awesome' => 'config']])
109
                ->and($indices = $this->newIndices())
110
                ->and($client = $this->newClient($indices))
111
                ->and($clientRepository = $this->newClientRepository('my_client', null))
112
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
113
                ->and($this->newTestedInstance($clientRepository, $configRepository))
114
            ->exception(function() {
115
                    $this->testedInstance->handle('my_client', 'my_index');
116
                })
117
                ->isInstanceOf(\InvalidArgumentException::class)
118
        ;
119
    }
120
}