Code Duplication    Length = 105-105 lines in 2 locations

tests/Console/Command/IndexCreateCommandTest.php 1 location

@@ 14-118 (lines=105) @@
11
use Generator;
12
use Mockery\MockInterface;
13
14
final class IndexCreateCommandTest extends TestCase
15
{
16
    public function testCreateIndexMustSucceed(): 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(false);
25
26
                        $mock->shouldReceive('create')
27
                            ->once()
28
                            ->andReturn([]);
29
                    })
30
                );
31
        });
32
33
        $this->artisan('laravel-elasticsearch:utils:index-create',
34
            ['index-name' => 'valid_index_name']
35
        )->assertExitCode(0)
36
            ->expectsOutput('Index valid_index_name created.');
37
    }
38
39
    public function testCreateIndexMustFail(): 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(false);
48
49
                        $mock->shouldReceive('create')
50
                            ->once()
51
                            ->andThrow(
52
                                new Exception('index already exists test exception')
53
                            );
54
                    })
55
                );
56
        });
57
58
        $this->artisan('laravel-elasticsearch:utils:index-create',
59
            ['index-name' => 'valid_index_name']
60
        )->assertExitCode(1)
61
            ->expectsOutput('Error creating index valid_index_name, exception message: index already exists test exception.');
62
    }
63
64
    public function testCreateIndexMustFailBecauseIndexAlreadyExists(): 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(true);
73
74
                        $mock->shouldNotReceive('create');
75
                    })
76
                );
77
        });
78
79
        $this->artisan('laravel-elasticsearch:utils:index-create',
80
            ['index-name' => 'valid_index_name']
81
        )->assertExitCode(1)
82
            ->expectsOutput('Index valid_index_name already exists and cannot be created.');
83
    }
84
85
    /**
86
     * @dataProvider invalidIndexNameDataProvider
87
     */
88
    public function testArgumentIndexNameIsInValid($invalidIndexName): void
89
    {
90
        $this->artisan('laravel-elasticsearch:utils:index-create',
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

tests/Console/Command/IndexDeleteCommandTest.php 1 location

@@ 14-118 (lines=105) @@
11
use Generator;
12
use Mockery\MockInterface;
13
14
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