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

NotifiableTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 43.44 %

Importance

Changes 0
Metric Value
wmc 7
dl 53
loc 122
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A route_notification_for_slack() 0 4 1
A route_notification_for_slack_custom() 17 17 1
A route_notification_for_mail_custom_array() 0 18 1
A setUp() 0 20 1
A route_notification_no_custom_data() 17 17 1
A route_notification_for_mail() 0 3 1
A route_notification_for_mail_custom() 17 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
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