1
|
|
|
<?php |
2
|
|
|
namespace TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications; |
3
|
|
|
|
4
|
|
|
use TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckFailed; |
5
|
|
|
use TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckSucceeded; |
6
|
|
|
use TheCodingMachine\ServerMonitorPluginNotificationByHost\Test\TestCase; |
7
|
|
|
use Spatie\ServerMonitor\Models\Check; |
8
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
9
|
|
|
use Spatie\ServerMonitor\Models\Host; |
10
|
|
|
|
11
|
|
|
class BaseNotificationTraitTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifiable |
17
|
|
|
*/ |
18
|
|
|
private $notifiable; |
19
|
|
|
|
20
|
|
|
public function setUp() { |
21
|
|
|
parent::setUp(); |
22
|
|
|
$this->notifiable = new Notifiable(); |
23
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** @test */ |
27
|
|
|
function via() { |
|
|
|
|
28
|
|
|
|
29
|
|
|
$checks = ['diskspace']; |
30
|
|
|
|
31
|
|
|
$host = Host::create([ |
32
|
|
|
'name' => 'test', |
33
|
|
|
'port' => 22, |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$host->checks()->saveMany(collect($checks)->map(function (string $checkName) { |
37
|
|
|
return new Check([ |
38
|
|
|
'type' => $checkName, |
39
|
|
|
'status' => CheckStatus::NOT_YET_CHECKED, |
40
|
|
|
]); |
41
|
|
|
})); |
42
|
|
|
|
43
|
|
|
$event = new \Spatie\ServerMonitor\Events\CheckSucceeded($host->checks()->first()); |
44
|
|
|
$this->notifiable->setEvent($event); |
45
|
|
|
|
46
|
|
|
$check = new CheckFailed(); |
47
|
|
|
$result = $check->via($this->notifiable); |
48
|
|
|
$this->assertSame('slack', $result[0]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @test */ |
52
|
|
View Code Duplication |
function via_custom() { |
|
|
|
|
53
|
|
|
|
54
|
|
|
$checks = ['diskspace']; |
55
|
|
|
|
56
|
|
|
$host = Host::create([ |
57
|
|
|
'name' => 'test2', |
58
|
|
|
'port' => 22, |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$host->checks()->saveMany(collect($checks)->map(function (string $checkName) { |
62
|
|
|
return new Check([ |
63
|
|
|
'type' => $checkName, |
64
|
|
|
'status' => CheckStatus::NOT_YET_CHECKED, |
65
|
|
|
]); |
66
|
|
|
})); |
67
|
|
|
|
68
|
|
|
$event = new \Spatie\ServerMonitor\Events\CheckSucceeded($host->checks()->first()); |
69
|
|
|
$this->notifiable->setEvent($event); |
70
|
|
|
|
71
|
|
|
$host = $this->notifiable->getEvent()->check->host()->first(); |
72
|
|
|
$notifications = [ |
73
|
|
|
'TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckSucceeded' => |
74
|
|
|
[ |
75
|
|
|
'channels' => ['mail'] |
76
|
|
|
], |
77
|
|
|
'configuration' => ['mail' => [ 'to' => ['[email protected]'] ]] |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
$host->setCustomProperty('notifications', $notifications); |
81
|
|
|
|
82
|
|
|
$host->save(); |
83
|
|
|
|
84
|
|
|
$check = new CheckFailed(); |
85
|
|
|
$result = $check->via($this->notifiable); |
86
|
|
|
$this->assertCount(0, $result); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @test */ |
90
|
|
View Code Duplication |
function via_custom_notifiation_exist() { |
|
|
|
|
91
|
|
|
|
92
|
|
|
$checks = ['diskspace']; |
93
|
|
|
|
94
|
|
|
$host = Host::create([ |
95
|
|
|
'name' => 'test', |
96
|
|
|
'port' => 22, |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$host->checks()->saveMany(collect($checks)->map(function (string $checkName) { |
100
|
|
|
return new Check([ |
101
|
|
|
'type' => $checkName, |
102
|
|
|
'status' => CheckStatus::NOT_YET_CHECKED, |
103
|
|
|
]); |
104
|
|
|
})); |
105
|
|
|
|
106
|
|
|
$event = new \Spatie\ServerMonitor\Events\CheckSucceeded($host->checks()->first()); |
107
|
|
|
$this->notifiable->setEvent($event); |
108
|
|
|
|
109
|
|
|
$host = $this->notifiable->getEvent()->check->host()->first(); |
110
|
|
|
$notifications = [ |
111
|
|
|
'TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications\Notifications\CheckFailed' => |
112
|
|
|
[ |
113
|
|
|
'channels' => ['mail'] |
114
|
|
|
], |
115
|
|
|
'configuration' => ['mail' => [ 'to' => ['[email protected]'] ]] |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
$host->setCustomProperty('notifications', $notifications); |
119
|
|
|
|
120
|
|
|
$host->save(); |
121
|
|
|
|
122
|
|
|
$check = new CheckFailed(); |
123
|
|
|
$result = $check->via($this->notifiable); |
124
|
|
|
$this->assertSame('mail', $result[0]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.