icationByHostTest.php$0   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 12 %

Importance

Changes 0
Metric Value
wmc 6
dl 3
loc 25
rs 10
c 0
b 0
f 0

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\Commands;
4
5
use Illuminate\Support\Facades\Artisan;
6
use Mockery as m;
7
use Spatie\ServerMonitor\HostRepository;
8
use Spatie\ServerMonitor\Models\Host;
9
use TheCodingMachine\ServerMonitorPluginNotificationByHost\Commands\AddNotificationByHost;
10
use TheCodingMachine\ServerMonitorPluginNotificationByHost\Test\TestCase;
11
12
class AddNotificationByHostTest extends TestCase
13
{
14
    protected $command;
15
16
    public function setUp()
17
    {
18
        parent::setUp();
19
20
        $this->command = new class extends AddNotificationByHost {
21
            private $answers = [];
22
            private $answer = 0;
23
            public function answers($answers) {
24
                $this->answer = 0;
25
                $this->answers = $answers;
26
            }
27
            public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null)
28
            {
29
                $result = $this->answers[$this->answer];
30
                if($multiple) {
31 View Code Duplication
                    if(!is_array($this->answers[$this->answer])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
32
                        $result = [$this->answers[$this->answer]];
33
                    }
34
                }
35
                $this->answer ++;
36
                return $result;
37
            }
38
            public function confirm($question, $default = false)
39
            {
40
                return $this->answers[$this->answer ++];
41
            }
42
43
            public function ask($question, $default = null) {
44
                return $this->answers[$this->answer ++];
45
            }
46
        };
47
48
        $this->app->bind('command.server-monitor:add-notification-host', function () {
49
            return $this->command;
50
        });
51
    }
52
53
    /** @test */
54
    public function it_can_notify_host_empty()
55
    {
56
        $host = Host::create([
57
            'name' => 'test',
58
            'ssh_user' => 'user',
59
            'port' => 22]);
60
61
        $this->command->answers([false, "test", 'No channel', 'No channel', 'No channel', 'No channel']);
62
        Artisan::call('server-monitor:add-notification-host');
63
        $this->assertNull($host->getCustomProperty('notifications'));
64
65
        $this->command->answers([true, "wrongTest", true, 'test', 'No channel', 'No channel', 'No channel', 'No channel']);
66
        Artisan::call('server-monitor:add-notification-host');
67
        $this->assertNull($host->getCustomProperty('notifications'));
68
    }
69
70
    /** @test */
71
    public function it_can_notify_host_check_mail()
72
    {
73
        $host = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
74
            'name' => 'test',
75
            'ssh_user' => 'user',
76
            'port' => 22]);
77
        $this->command->answers([false, 'test', 'mail', 'No channel', 'No channel', 'No channel', '[email protected]', false]);
78
        Artisan::call('server-monitor:add-notification-host');
79
80
        $host = HostRepository::determineHostModel()::where('name', 'test')->first();
81
82
        $notifications = $host->getCustomProperty('notifications');
83
84
        $checkSucceeded = $notifications['TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckSucceeded'];
85
86
        $this->assertContains('mail', $checkSucceeded['channels']);
87
    }
88
89
    /** @test */
90
    public function it_can_notify_host_check_keep_old_parameter()
91
    {
92
        $host = Host::create([
93
            'name' => 'test',
94
            'ssh_user' => 'user',
95
            'port' => 22]);
96
        $host->setCustomProperty('test', 'test');
97
        $host->save();
98
99
        $this->command->answers([false, 'test', 'mail', 'No channel', 'No channel', 'No channel', true, '[email protected]', '']);
100
        Artisan::call('server-monitor:add-notification-host');
101
102
        $host = HostRepository::determineHostModel()::where('name', 'test')->first();
103
104
        $test = $host->getCustomProperty('test');
105
106
        $this->assertSame($test, 'test');
107
    }
108
109
    /** @test */
110
    public function error_channel()
111
    {
112
        $host = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
113
            'name' => 'test',
114
            'ssh_user' => 'user',
115
            'port' => 22]);
116
117
        $this->command->answers([false, "test", 'test', 'No channel', 'No channel', 'No channel']);
118
        Artisan::call('server-monitor:add-notification-host');
119
        $this->seeInConsoleOutput('No configuration for');
120
    }
121
}
122