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