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 IndexDeleteCommandTest extends TestCase |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
public function testIndexDeleteMustSucceed(): 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
|
|
|
->andReturn(true); |
25
|
|
|
|
26
|
|
|
$mock->shouldReceive('delete') |
27
|
|
|
->once() |
28
|
|
|
->andReturn([]); |
29
|
|
|
}) |
30
|
|
|
); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
$this->artisan('laravel-elasticsearch:utils:index-delete', |
34
|
|
|
['index-name' => 'valid_index_name'] |
35
|
|
|
)->assertExitCode(0) |
36
|
|
|
->expectsOutput('Index valid_index_name deleted.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testIndexDeleteMustFail(): void |
40
|
|
|
{ |
41
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
42
|
|
|
$mock->shouldReceive('indices') |
43
|
|
|
->times(2) |
44
|
|
|
->andReturn( |
45
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
46
|
|
|
$mock->shouldReceive('exists') |
|
|
|
|
47
|
|
|
->andReturn(true); |
48
|
|
|
|
49
|
|
|
$mock->shouldReceive('delete') |
50
|
|
|
->once() |
51
|
|
|
->andThrow( |
52
|
|
|
new Exception('error creating index test exception') |
53
|
|
|
); |
54
|
|
|
}) |
55
|
|
|
); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$this->artisan('laravel-elasticsearch:utils:index-delete', |
59
|
|
|
['index-name' => 'valid_index_name'] |
60
|
|
|
)->assertExitCode(1) |
61
|
|
|
->expectsOutput('Error deleting index valid_index_name, exception message: error creating index test exception.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testIndexDeleteMustFailBecauseIndexDoesntExists(): void |
65
|
|
|
{ |
66
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
67
|
|
|
$mock->shouldReceive('indices') |
68
|
|
|
->once() |
69
|
|
|
->andReturn( |
70
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
71
|
|
|
$mock->shouldReceive('exists') |
|
|
|
|
72
|
|
|
->andReturn(false); |
73
|
|
|
|
74
|
|
|
$mock->shouldNotReceive('create'); |
75
|
|
|
}) |
76
|
|
|
); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
$this->artisan('laravel-elasticsearch:utils:index-delete', |
80
|
|
|
['index-name' => 'valid_index_name'] |
81
|
|
|
)->assertExitCode(1) |
82
|
|
|
->expectsOutput('Index valid_index_name doesn\'t exists and cannot be deleted.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @dataProvider invalidIndexNameDataProvider |
87
|
|
|
*/ |
88
|
|
|
public function testArgumentIndexNameIsInValid($invalidIndexName): void |
89
|
|
|
{ |
90
|
|
|
$this->artisan('laravel-elasticsearch:utils:index-delete', |
91
|
|
|
['index-name' => $invalidIndexName] |
92
|
|
|
)->assertExitCode(1) |
93
|
|
|
->expectsOutput('Argument index-name must be a non empty string.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function invalidIndexNameDataProvider(): Generator |
97
|
|
|
{ |
98
|
|
|
yield [ |
99
|
|
|
null |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
yield [ |
103
|
|
|
'' |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
yield [ |
107
|
|
|
true |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
yield [ |
111
|
|
|
1 |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
yield [ |
115
|
|
|
[] |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|