Completed
Push — master ( cb7970...970861 )
by Colin
12:52 queued 03:09
created

IndexCreateOrUpdateMappingCommandTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 222
Duplicated Lines 51.8 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 115
loc 222
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateOrUpdateMappingMustSucceed() 37 37 1
A testCreateOrUpdateMappingMustFailBecauseMappingFileDoesntExistsOnFilesystem() 0 21 1
A testCreateOrUpdateMappingMustCreateNewIndexIfIndexDoesntExists() 37 37 1
A testCreateOrUpdateMappingMustFail() 41 41 1
A testArgumentIndexNameAndAliasAreInValid() 0 13 1
B invalidIndexNameDataProvider() 0 62 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cviebrock\LaravelElasticsearch\Tests\Console\Command;
6
7
use Cviebrock\LaravelElasticsearch\Tests\TestCase;
8
use Elasticsearch\Client;
9
use Elasticsearch\Namespaces\IndicesNamespace;
10
use Exception;
11
use Generator;
12
use Illuminate\Contracts\Filesystem\Filesystem;
13
use Mockery\MockInterface;
14
15
final class IndexCreateOrUpdateMappingCommandTest extends TestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
16
{
17 View Code Duplication
    public function testCreateOrUpdateMappingMustSucceed(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
18
    {
19
        $this->mock(Filesystem::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
20
            $mock->shouldReceive('exists')
21
                ->once()
22
                ->andReturn(true);
23
24
            $mock->shouldReceive('get')
25
                ->once()
26
                ->andReturn('{}');
27
        });
28
29
        $this->mock(Client::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
            $mock->shouldReceive('indices')
31
                ->times(2)
32
                ->andReturn(
33
                    $this->mock(IndicesNamespace::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
                        $mock->shouldReceive('exists')
35
                            ->once()
36
                            ->andReturn(true);
37
38
                        $mock->shouldReceive('putMapping')
39
                            ->once()
40
                            ->andReturn([]);
41
                    })
42
                );
43
        });
44
45
        $this->artisan(
46
            'laravel-elasticsearch:utils:index-create-or-update-mapping',
47
            [
48
                'index-name' => 'valid_index_name',
49
                'mapping-file-path' => '/path/to/existing_mapping_file.json',
50
            ]
51
        )->assertExitCode(0)
52
            ->expectsOutput('Mapping created or updated for index valid_index_name using file /path/to/existing_mapping_file.json.');
53
    }
54
55
    public function testCreateOrUpdateMappingMustFailBecauseMappingFileDoesntExistsOnFilesystem(): void
56
    {
57
        $this->mock(Filesystem::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
            $mock->shouldReceive('exists')
59
                ->once()
60
                ->andReturn(false);
61
        });
62
63
        $this->mock(Client::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
            $mock->shouldNotReceive('indices');
65
        });
66
67
        $this->artisan(
68
            'laravel-elasticsearch:utils:index-create-or-update-mapping',
69
            [
70
                'index-name' => 'valid_index_name',
71
                'mapping-file-path' => '/path/to/non_existing_mapping_file.json',
72
            ]
73
        )->assertExitCode(1)
74
            ->expectsOutput('Argument mapping-file-path must exists on filesystem and must be a non empty string.');
75
    }
76
77 View Code Duplication
    public function testCreateOrUpdateMappingMustCreateNewIndexIfIndexDoesntExists(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
78
    {
79
        $this->mock(Filesystem::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
            $mock->shouldReceive('exists')
81
                ->once()
82
                ->andReturn(true);
83
84
            $mock->shouldReceive('get')
85
                ->once()
86
                ->andReturn('{}');
87
        });
88
89
        $this->mock(Client::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            $mock->shouldReceive('indices')
91
                ->times(2)
92
                ->andReturn(
93
                    $this->mock(IndicesNamespace::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
                        $mock->shouldReceive('exists')
95
                            ->once()
96
                            ->andReturn(false);
97
98
                        $mock->shouldReceive('create')
99
                            ->once()
100
                            ->andReturn(true);
101
                    })
102
                );
103
        });
104
105
        $this->artisan(
106
            'laravel-elasticsearch:utils:index-create-or-update-mapping',
107
            [
108
                'index-name' => 'valid_index_name',
109
                'mapping-file-path' => '/path/to/existing_mapping_file.json',
110
            ]
111
        )->assertExitCode(0)
112
            ->expectsOutput('Index valid_index_name doesn\'t exist, a new index was created with mapping/settings using file /path/to/existing_mapping_file.json.');
113
    }
114
115 View Code Duplication
    public function testCreateOrUpdateMappingMustFail(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
116
    {
117
        $this->mock(Filesystem::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
            $mock->shouldReceive('exists')
119
                ->once()
120
                ->andReturn(true);
121
122
            $mock->shouldReceive('get')
123
                ->once()
124
                ->andReturn('{}');
125
        });
126
127
        $this->mock(Client::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
            $mock->shouldReceive('indices')
129
                ->times(2)
130
                ->andReturn(
131
                    $this->mock(IndicesNamespace::class, function (MockInterface $mock) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...dateMappingCommandTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
                        $mock->shouldReceive('exists')
133
                            ->once()
134
                            ->andReturn(true);
135
136
                        $mock->shouldReceive('putMapping')
137
                            ->once()
138
                            ->andThrow(
139
                                new Exception('error creating or updating mapping test exception')
140
                            );
141
                    })
142
                );
143
        });
144
145
        $this->artisan(
146
            'laravel-elasticsearch:utils:index-create-or-update-mapping',
147
            [
148
                'index-name' => 'valid_index_name',
149
                'mapping-file-path' => '/path/to/existing_mapping_file.json',
150
            ]
151
        )->assertExitCode(1)
152
            ->expectsOutput(
153
                'Error creating or updating mapping for index valid_index_name, given mapping file: /path/to/existing_mapping_file.json - error message: error creating or updating mapping test exception.'
154
            );
155
    }
156
157
    /**
158
     * @dataProvider invalidIndexNameDataProvider
159
     */
160
    public function testArgumentIndexNameAndAliasAreInValid(
161
        $invalidIndexName,
162
        $invalidAliasName,
163
        string $expectedOutputMessage
164
    ): void {
165
        $this->artisan('laravel-elasticsearch:utils:index-create-or-update-mapping',
166
            [
167
                'index-name' => $invalidIndexName,
168
                'mapping-file-path' => $invalidAliasName,
169
            ]
170
        )->assertExitCode(1)
171
            ->expectsOutput($expectedOutputMessage);
172
    }
173
174
    public function invalidIndexNameDataProvider(): Generator
175
    {
176
        yield [
177
            null,
178
            '/valid/path/mapping.json',
179
            'Argument index-name must be a non empty string.'
180
        ];
181
182
        yield [
183
            '',
184
            '/valid/path/mapping.json',
185
            'Argument index-name must be a non empty string.'
186
        ];
187
188
        yield [
189
            true,
190
            '/valid/path/mapping.json',
191
            'Argument index-name must be a non empty string.'
192
        ];
193
194
        yield [
195
            1,
196
            '/valid/path/mapping.json',
197
            'Argument index-name must be a non empty string.'
198
        ];
199
200
        yield [
201
            [],
202
            '/valid/path/mapping.json',
203
            'Argument index-name must be a non empty string.'
204
        ];
205
206
        yield [
207
            'valid_index_name',
208
            null,
209
            'Argument mapping-file-path must exists on filesystem and must be a non empty string.'
210
        ];
211
212
        yield [
213
            'valid_index_name',
214
            '',
215
            'Argument mapping-file-path must exists on filesystem and must be a non empty string.'
216
        ];
217
218
        yield [
219
            'valid_index_name',
220
            true,
221
            'Argument mapping-file-path must exists on filesystem and must be a non empty string.'
222
        ];
223
224
        yield [
225
            'valid_index_name',
226
            1,
227
            'Argument mapping-file-path must exists on filesystem and must be a non empty string.'
228
        ];
229
230
        yield [
231
            'valid_index_name',
232
            [],
233
            'Argument mapping-file-path must exists on filesystem and must be a non empty string.'
234
        ];
235
    }
236
}
237