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

testAliasRemoveMustSucceed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Mockery\MockInterface;
13
14 View Code Duplication
final class AliasRemoveIndexCommandTest 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...
Duplication introduced by
This class 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...
15
{
16
    public function testAliasRemoveMustSucceed(): void
17
    {
18
        $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...RemoveIndexCommandTest>.

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...
19
            $mock->shouldReceive('indices')
20
                ->times(2)
21
                ->andReturn(
22
                    $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...RemoveIndexCommandTest>.

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...
23
                        $mock->shouldReceive('exists')
24
                            ->once()
25
                            ->andReturn(true);
26
27
                        $mock->shouldReceive('deleteAlias')
28
                            ->once()
29
                            ->andReturn([]);
30
                    })
31
                );
32
        });
33
34
        $this->artisan(
35
            'laravel-elasticsearch:utils:alias-remove-index',
36
            [
37
                'index-name' => 'valid_index_name',
38
                'alias-name' => 'valid_alias_name',
39
            ]
40
        )->assertExitCode(0)
41
            ->expectsOutput('Index valid_index_name removed from alias valid_alias_name.');
42
    }
43
44
    public function testAliasRemoveMustFail(): void
45
    {
46
        $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...RemoveIndexCommandTest>.

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...
47
            $mock->shouldReceive('indices')
48
                ->times(2)
49
                ->andReturn(
50
                    $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...RemoveIndexCommandTest>.

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...
51
                        $mock->shouldReceive('exists')
52
                            ->once()
53
                            ->andReturn(true);
54
55
                        $mock->shouldReceive('deleteAlias')
56
                            ->once()
57
                            ->andThrow(
58
                                new Exception('error removing index from alias exception')
59
                            );
60
                    })
61
                );
62
        });
63
64
        $this->artisan(
65
            'laravel-elasticsearch:utils:alias-remove-index',
66
            [
67
                'index-name' => 'valid_index_name',
68
                'alias-name' => 'valid_alias_name',
69
            ]
70
        )->assertExitCode(1)
71
            ->expectsOutput(
72
                'Error removing index valid_index_name from alias valid_alias_name, exception message: error removing index from alias exception.'
73
            );
74
    }
75
76
    public function testAliasRemoveMustFailBecauseIndexDoesntExists(): void
77
    {
78
        $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...RemoveIndexCommandTest>.

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...
79
            $mock->shouldReceive('indices')
80
                ->once()
81
                ->andReturn(
82
                    $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...RemoveIndexCommandTest>.

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...
83
                        $mock->shouldReceive('exists')
84
                            ->once()
85
                            ->andReturn(false);
86
87
                        $mock->shouldNotReceive('deleteAlias');
88
                    })
89
                );
90
        });
91
92
        $this->artisan(
93
            'laravel-elasticsearch:utils:alias-remove-index',
94
            [
95
                'index-name' => 'valid_index_name',
96
                'alias-name' => 'valid_alias_name',
97
            ]
98
        )->assertExitCode(1)
99
            ->expectsOutput(
100
                'Index valid_index_name doesn\'t exists and cannot be removed from alias.'
101
            );
102
    }
103
104
    /**
105
     * @dataProvider invalidIndexNameDataProvider
106
     */
107
    public function testArgumentIndexNameAndAliasAreInValid(
108
        $invalidIndexName,
109
        $invalidAliasName,
110
        string $expectedOutputMessage
111
    ): void {
112
        $this->artisan('laravel-elasticsearch:utils:alias-remove-index',
113
            [
114
                'index-name' => $invalidIndexName,
115
                'alias-name' => $invalidAliasName,
116
            ]
117
        )->assertExitCode(1)
118
            ->expectsOutput($expectedOutputMessage);
119
    }
120
121
    public function invalidIndexNameDataProvider(): Generator
122
    {
123
        yield [
124
            null,
125
            'valid_alias_name',
126
            'Argument index-name must be a non empty string.'
127
        ];
128
129
        yield [
130
            '',
131
            'valid_alias_name',
132
            'Argument index-name must be a non empty string.'
133
        ];
134
135
        yield [
136
            true,
137
            'valid_alias_name',
138
            'Argument index-name must be a non empty string.'
139
        ];
140
141
        yield [
142
            1,
143
            'valid_alias_name',
144
            'Argument index-name must be a non empty string.'
145
        ];
146
147
        yield [
148
            [],
149
            'valid_alias_name',
150
            'Argument index-name must be a non empty string.'
151
        ];
152
153
        yield [
154
            'valid_index_name',
155
            null,
156
            'Argument alias-name must be a non empty string.'
157
        ];
158
159
        yield [
160
            'valid_index_name',
161
            '',
162
            'Argument alias-name must be a non empty string.'
163
        ];
164
165
        yield [
166
            'valid_index_name',
167
            true,
168
            'Argument alias-name must be a non empty string.'
169
        ];
170
171
        yield [
172
            'valid_index_name',
173
            1,
174
            'Argument alias-name must be a non empty string.'
175
        ];
176
177
        yield [
178
            'valid_index_name',
179
            [],
180
            'Argument alias-name must be a non empty string.'
181
        ];
182
    }
183
}
184