ListNotificationsTest.php$0 ➔ confirm()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
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
    public function setUp() {
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 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...
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
    }
65
66
    /** @test */
67
    public function it_check() {
68
69
        $notifications =  [
70
            'TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckSucceeded' =>
71
                [
72
                    'channels' => ['mail']
73
                ],
74
            'TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckWarning' =>
75
                [
76
                    'channels' => ['slack']
77
                ],
78
            'configuration' => ['mail' => [ 'to' => '[email protected]' ]]
79
            ];
80
81
        $host = Host::create([
82
            'name' => 'test',
83
            'ssh_user' => 'user',
84
            'port' => 22]);
85
        $host->setCustomProperty('notifications', $notifications);
86
        $host->save();
87
88
        Artisan::call('server-monitor:list-notifications');
89
90
        $this->seeInConsoleOutput(['TheCodingMachine\ServerMonitorPluginNotificationBy | to:[email protected]']);
91
        $this->seeInConsoleOutput([' TheCodingMachine\ServerMonitorPluginNotificationBy |                  | Global']);
92
    }
93
94
    /** @test */
95
    public function it_check_many_host() {
96
97
        $host = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
98
            'name' => 'server',
99
            'ssh_user' => 'user',
100
            'port' => 22]);
101
102
        $host = Host::create([
103
            'name' => 'other',
104
            'ssh_user' => 'user',
105
            'port' => 22]);
106
107
        Artisan::call('server-monitor:list-notifications');
108
109
        $this->seeInConsoleOutput(['server', 'other']);
110
    }
111
112
    /** @test */
113
    public function specific_host() {
114
115
        $host1 = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host1 is dead and can be removed.
Loading history...
116
            'name' => 'server',
117
            'ssh_user' => 'user',
118
            'port' => 22]);
119
120
        $host2 = Host::create([
0 ignored issues
show
Unused Code introduced by
The assignment to $host2 is dead and can be removed.
Loading history...
121
            'name' => 'other',
122
            'ssh_user' => 'user',
123
            'port' => 22]);
124
125
        Artisan::call('server-monitor:list-notifications', ['--host' => 'server']);
126
127
        $this->seeInConsoleOutput(['server']);
128
        $this->dontSeeInConsoleOutput(['other']);
129
    }
130
131
}
132