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

IndexCreateCommandTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 105
loc 105
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateIndexMustSucceed() 22 22 1
A testCreateIndexMustFail() 24 24 1
A testCreateIndexMustFailBecauseIndexAlreadyExists() 20 20 1
A testArgumentIndexNameIsInValid() 7 7 1
A invalidIndexNameDataProvider() 22 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 IndexCreateCommandTest 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 testCreateIndexMustSucceed(): 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...IndexCreateCommandTest>.

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

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(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) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...IndexCreateCommandTest>.

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

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(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) {
0 ignored issues
show
Bug introduced by
The method mock() does not seem to exist on object<Cviebrock\Laravel...IndexCreateCommandTest>.

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

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(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