|
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 Generator; |
|
11
|
|
|
use Mockery\MockInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class IndexExistsCommandTest extends TestCase |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
public function testIndexExists(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
|
|
18
|
|
|
$mock->shouldReceive('indices') |
|
19
|
|
|
->once() |
|
20
|
|
|
->andReturn( |
|
21
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
|
|
22
|
|
|
$mock->shouldReceive('exists') |
|
23
|
|
|
->once() |
|
24
|
|
|
->andReturn(true); |
|
25
|
|
|
}) |
|
26
|
|
|
); |
|
27
|
|
|
}); |
|
28
|
|
|
|
|
29
|
|
|
$this->artisan( |
|
30
|
|
|
'laravel-elasticsearch:utils:index-exists', |
|
31
|
|
|
['index-name' => 'index_name_exists'] |
|
32
|
|
|
)->assertExitCode(0) |
|
33
|
|
|
->expectsOutput('Index index_name_exists exists.'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testIndexDoesntExists(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->mock(Client::class, function (MockInterface $mock) { |
|
|
|
|
|
|
39
|
|
|
$mock->shouldReceive('indices') |
|
40
|
|
|
->once() |
|
41
|
|
|
->andReturn( |
|
42
|
|
|
$this->mock(IndicesNamespace::class, function (MockInterface $mock) { |
|
|
|
|
|
|
43
|
|
|
$mock->shouldReceive('exists') |
|
44
|
|
|
->once() |
|
45
|
|
|
->andReturn(false); |
|
46
|
|
|
}) |
|
47
|
|
|
); |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
$this->artisan( |
|
51
|
|
|
'laravel-elasticsearch:utils:index-exists', |
|
52
|
|
|
['index-name' => 'test_index_name_doesnt_exists'] |
|
53
|
|
|
)->assertExitCode(0) |
|
54
|
|
|
->expectsOutput('Index test_index_name_doesnt_exists doesn\'t exists.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider invalidIndexNameDataProvider |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testArgumentIndexNameIsInValid($invalidIndexName): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->artisan('laravel-elasticsearch:utils:index-exists', |
|
63
|
|
|
['index-name' => $invalidIndexName] |
|
64
|
|
|
)->assertExitCode(1) |
|
65
|
|
|
->expectsOutput('Argument index-name must be a non empty string.'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function invalidIndexNameDataProvider(): Generator |
|
69
|
|
|
{ |
|
70
|
|
|
yield [ |
|
71
|
|
|
null |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
yield [ |
|
75
|
|
|
'' |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
yield [ |
|
79
|
|
|
true |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
yield [ |
|
83
|
|
|
1 |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
yield [ |
|
87
|
|
|
[] |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|