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

IndexDeleteCommandTest::testIndexDeleteMustFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 9.536
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 IndexDeleteCommandTest 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 testIndexDeleteMustSucceed(): 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...IndexDeleteCommandTest>.

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

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')
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...IndexDeleteCommandTest>.

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...
42
            $mock->shouldReceive('indices')
43
                ->times(2)
44
                ->andReturn(
45
                    $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...IndexDeleteCommandTest>.

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...
46
                        $mock->shouldReceive('exists')
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...IndexDeleteCommandTest>.

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...
67
            $mock->shouldReceive('indices')
68
                ->once()
69
                ->andReturn(
70
                    $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...IndexDeleteCommandTest>.

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...
71
                        $mock->shouldReceive('exists')
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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