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($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
|
|
View Code Duplication |
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
|
|
|
return $clientRepository; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
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
|
|
|
return $configRepository; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testHandleThrowExceptionIfNoClient() |
104
|
|
|
{ |
105
|
|
|
$this |
106
|
|
|
->given($config = ['my' => ['awesome' => 'config']]) |
107
|
|
|
->and($indices = $this->newIndices()) |
108
|
|
|
->and($client = $this->newClient($indices)) |
109
|
|
|
->and($clientRepository = $this->newClientRepository('my_client', null)) |
110
|
|
|
->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config)) |
111
|
|
|
->and($this->newTestedInstance($clientRepository, $configRepository)) |
112
|
|
|
->exception(function() { |
113
|
|
|
$this->testedInstance->handle('my_client', 'my_index'); |
114
|
|
|
}) |
115
|
|
|
->isInstanceOf(\InvalidArgumentException::class) |
116
|
|
|
; |
117
|
|
|
} |
118
|
|
|
} |
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.