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

AddNotificationByHostTest.php$0 ➔ choice()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
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 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...
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
                echo $question."\n";
30
                $result = $this->answers[$this->answer];
31
                if($multiple) {
32
                    if(!is_array($this->answers[$this->answer])) {
33
                        $result = [$this->answers[$this->answer]];
34
                    }
35
                }
36
                $this->answer ++;
37
                return $result;
38
            }
39
            public function confirm($question, $default = false)
40
            {
41
                echo $question."\n";
42
                return $this->answers[$this->answer ++];
43
            }
44
45
            public function ask($question, $default = null) {
46
                echo $question."\n";
47
                return $this->answers[$this->answer ++];
48
            }
49
        };
50
51
        $this->app->bind('command.server-monitor:add-notification-host', function () {
52
            return $this->command;
53
        });
54
    }
55
56
    /** @test */
57
    public function it_can_notify_host_empty()
58
    {
59
        $host = Host::create([
60
            'name' => 'test',
61
            'ssh_user' => 'user',
62
            'port' => 22]);
63
64
        $this->command->answers([false, "test", 'No channel', 'No channel', 'No channel', 'No channel']);
65
        Artisan::call('server-monitor:add-notification-host');
66
        $this->assertNull($host->getCustomProperty('notifications'));
67
68
        $this->command->answers([true, "wrongTest", true, 'test', 'No channel', 'No channel', 'No channel', 'No channel']);
69
        Artisan::call('server-monitor:add-notification-host');
70
        $this->assertNull($host->getCustomProperty('notifications'));
71
    }
72
73
    /** @test */
74
    public function it_can_notify_host_check_mail()
75
    {
76
        $host = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
77
            'name' => 'test',
78
            'ssh_user' => 'user',
79
            'port' => 22]);
80
        $this->command->answers([false, 'test', 'mail', 'No channel', 'No channel', 'No channel', '[email protected]', false]);
81
        Artisan::call('server-monitor:add-notification-host');
82
83
        $host = HostRepository::determineHostModel()::where('name', 'test')->first();
84
85
        $notifications = $host->getCustomProperty('notifications');
86
87
        $checkSucceeded = $notifications['TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckSucceeded'];
88
89
        $this->assertContains('mail', $checkSucceeded['channels']);
90
    }
91
92
    /** @test */
93
    public function it_can_notify_host_check_keep_old_parameter()
94
    {
95
        $host = Host::create([
96
            'name' => 'test',
97
            'ssh_user' => 'user',
98
            'port' => 22]);
99
        $host->setCustomProperty('test', 'test');
100
        $host->save();
101
102
        $this->command->answers([false, 'test', 'mail', 'No channel', 'No channel', 'No channel', true, '[email protected]', '']);
103
        Artisan::call('server-monitor:add-notification-host');
104
105
        $host = HostRepository::determineHostModel()::where('name', 'test')->first();
106
107
        $test = $host->getCustomProperty('test');
108
109
        $this->assertSame($test, 'test');
110
    }
111
112
    /** @test */
113
    public function error_channel()
114
    {
115
        $host = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
116
            'name' => 'test',
117
            'ssh_user' => 'user',
118
            'port' => 22]);
119
120
        $this->command->answers([false, "test", 'test', 'No channel', 'No channel', 'No channel']);
121
        Artisan::call('server-monitor:add-notification-host');
122
    }
123
}
124