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

AliasCreateCommandTest::testAliasCreateMustFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
dl 29
loc 29
rs 9.456
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 AliasCreateCommandTest 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 testAliasCreateMustSucceed(): 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...AliasCreateCommandTest>.

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...AliasCreateCommandTest>.

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('putAlias')
28
                            ->once()
29
                            ->andReturn([]);
30
                    })
31
                );
32
        });
33
34
        $this->artisan(
35
            'laravel-elasticsearch:utils:alias-create',
36
            [
37
                'index-name' => 'valid_index_name',
38
                'alias-name' => 'valid_alias_name',
39
            ]
40
        )->assertExitCode(0)
41
            ->expectsOutput('Alias valid_alias_name created for index valid_index_name.');
42
    }
43
44
    public function testAliasCreateMustFail(): 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...AliasCreateCommandTest>.

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...AliasCreateCommandTest>.

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('putAlias')
56
                            ->once()
57
                            ->andThrow(
58
                                new Exception('error creating alias test exception')
59
                            );
60
                    })
61
                );
62
        });
63
64
        $this->artisan(
65
            'laravel-elasticsearch:utils:alias-create',
66
            [
67
                'index-name' => 'valid_index_name',
68
                'alias-name' => 'valid_alias_name',
69
            ]
70
        )->assertExitCode(1)
71
            ->expectsOutput('Error creating alias valid_alias_name for index valid_index_name, exception message: error creating alias test exception.');
72
    }
73
74
    public function testAliasCreateMustFailBecauseIndexDoesntExists(): void
75
    {
76
        $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...AliasCreateCommandTest>.

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...
77
            $mock->shouldReceive('indices')
78
                ->once()
79
                ->andReturn(
80
                    $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...AliasCreateCommandTest>.

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