Passed
Push — master ( bcdbf3...5b3224 )
by Marc
11:54
created

ListNotificationsTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 33.06 %

Importance

Changes 0
Metric Value
wmc 11
dl 40
loc 121
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ confirm() 3 3 1
it_check() 0 25 ?
A hp$0 ➔ ask() 3 3 1
B hp$0 ➔ it_check() 0 25 1
A hp$0 ➔ answers() 3 3 1
A hp$0 ➔ choice() 10 10 3
B hp$0 ➔ setUp() 38 38 1
A hp$0 ➔ it_check_many_host() 0 15 1
it_check_many_host() 0 15 ?
A hp$0 ➔ it_check_no_data() 0 4 1
specific_host() 0 16 ?
setUp() 38 38 ?
A hp$0 ➔ specific_host() 0 16 1
it_check_no_data() 0 4 ?

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 TheCodingMachine\ServerMonitorPluginNotificationByHost\Test;
4
5
6
use Illuminate\Support\Facades\Artisan;
7
use Mockery as m;
8
use Spatie\ServerMonitor\Models\Host;
9
use TheCodingMachine\ServerMonitorPluginNotificationByHost\Commands\ListNotifications;
10
use TheCodingMachine\ServerMonitorPluginNotificationByHost\Test\TestCase;
11
12
class ListNotificationsTest extends TestCase
13
{
14
    /** @var \TheCodingMachine\ServerMonitorPluginNotificationByHost\Commands\ListNotifications|m\Mock */
15
    protected $command;
16
17 View Code Duplication
    public function setUp() {
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...
18
        parent::setUp();
19
        $this->command = new class extends ListNotifications {
20
21
            private $answers = [];
22
23
            private $answer = 0;
24
25
            public function answers($answers) {
26
                $this->answer = 0;
27
                $this->answers = $answers;
28
            }
29
30
            public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null) {
31
                echo $question . "\n";
32
                $result = $this->answers[$this->answer];
33
                if ($multiple) {
34
                    if (!is_array($this->answers[$this->answer])) {
35
                        $result = [$this->answers[$this->answer]];
36
                    }
37
                }
38
                $this->answer++;
39
                return $result;
40
            }
41
42
            public function confirm($question, $default = false) {
43
                echo $question . "\n";
44
                return $this->answers[$this->answer++];
45
            }
46
47
            public function ask($question, $default = null) {
48
                echo $question . "\n";
49
                return $this->answers[$this->answer++];
50
            }
51
        };
52
53
        $this->app->bind('command.server-monitor:list-notifications', function () {
54
            return $this->command;
55
        });
56
    }
57
58
    /** @test */
59
    public function it_check_no_data() {
60
        Artisan::call('server-monitor:list-notifications');
61
62
        $this->seeInConsoleOutput(['There are no hosts configured']);
63
64
//        var_dump($this->getArtisanOutput());
65
        //        $this->dontSeeInConsoleOutput(['wrong-host', 'wrong-check']);
66
        //
67
        //        $this->seeInConsoleOutput(['correct-check', 'correct-host']);
68
    }
69
70
    /** @test */
71
    public function it_check() {
72
73
        $notifications =  [
74
            'TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckSucceeded' =>
75
                [
76
                    'channels' => ['mail']
77
                ],
78
            'TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckWarning' =>
79
                [
80
                    'channels' => ['slack']
81
                ],
82
            'configuration' => ['mail' => [ 'to' => '[email protected]' ]]
83
            ];
84
85
        $host = Host::create([
86
            'name' => 'test',
87
            'ssh_user' => 'user',
88
            'port' => 22]);
89
        $host->setCustomProperty('notifications', $notifications);
90
        $host->save();
91
92
        Artisan::call('server-monitor:list-notifications');
93
94
        $this->seeInConsoleOutput(['TheCodingMachine\ServerMonitorPluginNotificationBy | to:[email protected]']);
95
        $this->seeInConsoleOutput([' TheCodingMachine\ServerMonitorPluginNotificationBy |                  | Global']);
96
    }
97
98
    /** @test */
99
    public function it_check_many_host() {
100
101
        $host = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
102
            'name' => 'server',
103
            'ssh_user' => 'user',
104
            'port' => 22]);
105
106
        $host = Host::create([
107
            'name' => 'other',
108
            'ssh_user' => 'user',
109
            'port' => 22]);
110
111
        Artisan::call('server-monitor:list-notifications');
112
113
        $this->seeInConsoleOutput(['server', 'other']);
114
    }
115
116
    /** @test */
117
    public function specific_host() {
118
119
        $host1 = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host1 is dead and can be removed.
Loading history...
120
            'name' => 'server',
121
            'ssh_user' => 'user',
122
            'port' => 22]);
123
124
        $host2 = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host2 is dead and can be removed.
Loading history...
125
            'name' => 'other',
126
            'ssh_user' => 'user',
127
            'port' => 22]);
128
129
        Artisan::call('server-monitor:list-notifications', ['--host' => 'server']);
130
131
        $this->seeInConsoleOutput(['server']);
132
        $this->dontSeeInConsoleOutput(['other']);
133
    }
134
135
}
136