Passed
Push — stable ( 7698e0...f9d7ca )
by Nuno
03:02
created

BaseCommandTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 87
Duplicated Lines 36.78 %

Importance

Changes 0
Metric Value
dl 32
loc 87
rs 10
c 0
b 0
f 0
wmc 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ handle() 0 3 1
A hp$0 ➔ setOutput() 0 4 1
A it_has_a_container_getter() 0 6 1
A it_allows_notifications() 0 18 2
A it_allows_success_tasks() 16 16 1
A it_allows_fail_tasks() 16 16 1
A makeCommand() 0 20 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
namespace Tests;
4
5
use LaravelZero\Framework\Commands\Command;
6
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class BaseCommandTest extends TestCase
10
{
11
    /** @test */
12
    public function it_has_a_container_getter(): void
13
    {
14
        $command = $this->makeCommand();
15
16
        $this->assertSame($command->getContainer(), app());
17
    }
18
19
    /** @test */
20
    public function it_allows_notifications(): void
21
    {
22
        $command = $this->makeCommand();
23
24
        $notifierMock = $this->createMock(Notifier::class);
25
26
        $notifierMock->expects($this->once())->method('send')->with(
27
            $this->callback(
28
                function ($notification) {
29
                    return $notification->getTitle() === 'foo' && $notification->getBody() === 'bar';
30
                }
31
            )
32
        );
33
34
        app()->instance(Notifier::class, $notifierMock);
35
36
        $command->notify('foo', 'bar');
37
    }
38
39
    /** @test */
40 View Code Duplication
    public function it_allows_success_tasks(): void
0 ignored issues
show
Duplication introduced by
This method 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...
41
    {
42
        $command = $this->makeCommand();
43
44
        $outputMock = $this->createMock(OutputInterface::class);
45
        $outputMock->expects($this->once())->method('writeln')->with(
46
            'foo: <info>✔</info>'
47
        );
48
49
        $command->setOutput($outputMock);
50
51
        $command->task(
52
            'foo', function () {
53
            return true;
54
        });
55
    }
56
57
    /** @test */
58 View Code Duplication
    public function it_allows_fail_tasks(): void
0 ignored issues
show
Duplication introduced by
This method 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...
59
    {
60
        $command = $this->makeCommand();
61
62
        $outputMock = $this->createMock(OutputInterface::class);
63
        $outputMock->expects($this->once())->method('writeln')->with(
64
            'bar: <error>failed</error>'
65
        );
66
67
        $command->setOutput($outputMock);
68
69
        $command->task(
70
            'bar', function () {
71
            return false;
72
        });
73
    }
74
75
    private function makeCommand()
76
    {
77
        $command = new class extends Command
78
        {
79
            protected $name = 'foo:bar';
80
81
            public function handle(): void
82
            {
83
            }
84
85
            public function setOutput($output)
86
            {
87
                $this->output = $output;
88
            }
89
        };
90
91
        $this->app->add($command);
92
93
        return $command;
94
    }
95
}
96