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() { |
|
|
|
|
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([ |
|
|
|
|
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([ |
|
|
|
|
120
|
|
|
'name' => 'server', |
121
|
|
|
'ssh_user' => 'user', |
122
|
|
|
'port' => 22]); |
123
|
|
|
|
124
|
|
|
$host2 = Host::create([ |
|
|
|
|
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
|
|
|
|
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.