@@ 14-179 (lines=166) @@ | ||
11 | use Generator; |
|
12 | use Mockery\MockInterface; |
|
13 | ||
14 | final class AliasCreateCommandTest extends TestCase |
|
15 | { |
|
16 | public function testAliasCreateMustSucceed(): void |
|
17 | { |
|
18 | $this->mock(Client::class, function (MockInterface $mock) { |
|
19 | $mock->shouldReceive('indices') |
|
20 | ->times(2) |
|
21 | ->andReturn( |
|
22 | $this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
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) { |
|
47 | $mock->shouldReceive('indices') |
|
48 | ->times(2) |
|
49 | ->andReturn( |
|
50 | $this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
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) { |
|
77 | $mock->shouldReceive('indices') |
|
78 | ->once() |
|
79 | ->andReturn( |
|
80 | $this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
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 |
@@ 14-183 (lines=170) @@ | ||
11 | use Generator; |
|
12 | use Mockery\MockInterface; |
|
13 | ||
14 | final class AliasRemoveIndexCommandTest extends TestCase |
|
15 | { |
|
16 | public function testAliasRemoveMustSucceed(): void |
|
17 | { |
|
18 | $this->mock(Client::class, function (MockInterface $mock) { |
|
19 | $mock->shouldReceive('indices') |
|
20 | ->times(2) |
|
21 | ->andReturn( |
|
22 | $this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
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) { |
|
47 | $mock->shouldReceive('indices') |
|
48 | ->times(2) |
|
49 | ->andReturn( |
|
50 | $this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
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) { |
|
79 | $mock->shouldReceive('indices') |
|
80 | ->once() |
|
81 | ->andReturn( |
|
82 | $this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
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 |