1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Tests\Watcher\Communicator; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
6
|
|
|
use Coco\SourceWatcher\Utils\Internationalization; |
7
|
|
|
use Coco\SourceWatcher\Watcher\Communicator\SlackCommunicator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SlackCommunicatorTest |
12
|
|
|
* |
13
|
|
|
* @package Coco\SourceWatcher\Tests\Watcher\Communicator |
14
|
|
|
*/ |
15
|
|
|
class SlackCommunicatorTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
private string $slackWebHook; |
18
|
|
|
|
19
|
|
|
private string $method; |
20
|
|
|
|
21
|
|
|
private string $contentType; |
22
|
|
|
|
23
|
|
|
private array $mockResults; |
24
|
|
|
|
25
|
|
|
public function setUp () : void |
26
|
|
|
{ |
27
|
|
|
$this->slackWebHook = array_key_exists( "UNIT_TEST_SLACK_WEB_HOOK", |
28
|
|
|
$_ENV ) ? $_ENV["UNIT_TEST_SLACK_WEB_HOOK"] : null; |
29
|
|
|
|
30
|
|
|
$this->method = "POST"; |
31
|
|
|
|
32
|
|
|
$this->contentType = "Content-Type: application/json"; |
33
|
|
|
|
34
|
|
|
$this->mockResults = [ |
35
|
|
|
"blocks" => [ |
36
|
|
|
[ |
37
|
|
|
"type" => "section", |
38
|
|
|
"text" => [ "type" => "mrkdwn", "text" => gmdate( "l jS \of F Y h:i:s A" ) ] |
39
|
|
|
], |
40
|
|
|
[ |
41
|
|
|
"type" => "section", |
42
|
|
|
"text" => [ "type" => "mrkdwn", "text" => "Running unit test for Slack Communicator class" ] |
43
|
|
|
] |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function tearDown () : void |
49
|
|
|
{ |
50
|
|
|
unset( $this->slackWebHook ); |
51
|
|
|
unset( $this->method ); |
52
|
|
|
unset( $this->contentType ); |
53
|
|
|
unset( $this->mockResults ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testSetGetWebHookUrl () : void |
57
|
|
|
{ |
58
|
|
|
$communicator = new SlackCommunicator(); |
59
|
|
|
|
60
|
|
|
$givenSlackWebHook = $this->slackWebHook; |
61
|
|
|
$expectedSlackWebHook = $this->slackWebHook; |
62
|
|
|
|
63
|
|
|
$communicator->setWebHookUrl( $givenSlackWebHook ); |
64
|
|
|
|
65
|
|
|
$this->assertNotNull( $communicator->getWebHookUrl() ); |
66
|
|
|
$this->assertEquals( $expectedSlackWebHook, $communicator->getWebHookUrl() ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSetGetMethod () : void |
70
|
|
|
{ |
71
|
|
|
$communicator = new SlackCommunicator(); |
72
|
|
|
|
73
|
|
|
$givenMethod = $this->method; |
74
|
|
|
$expectedMethod = $this->method; |
75
|
|
|
|
76
|
|
|
$communicator->setMethod( $givenMethod ); |
77
|
|
|
|
78
|
|
|
$this->assertNotNull( $communicator->getMethod() ); |
79
|
|
|
$this->assertEquals( $expectedMethod, $communicator->getMethod() ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testSetGetContentType () : void |
83
|
|
|
{ |
84
|
|
|
$communicator = new SlackCommunicator(); |
85
|
|
|
|
86
|
|
|
$givenContentType = $this->contentType; |
87
|
|
|
$expectedContentType = $this->contentType; |
88
|
|
|
|
89
|
|
|
$communicator->setContentType( $givenContentType ); |
90
|
|
|
|
91
|
|
|
$this->assertNotNull( $communicator->getContentType() ); |
92
|
|
|
$this->assertEquals( $expectedContentType, $communicator->getContentType() ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testSetGetData () : void |
96
|
|
|
{ |
97
|
|
|
$communicator = new SlackCommunicator(); |
98
|
|
|
|
99
|
|
|
$givenData = json_encode( $this->mockResults ); |
100
|
|
|
$expectedData = json_encode( $this->mockResults ); |
101
|
|
|
|
102
|
|
|
$communicator->setData( $givenData ); |
103
|
|
|
|
104
|
|
|
$this->assertNotNull( $communicator->getData() ); |
105
|
|
|
$this->assertEquals( $expectedData, $communicator->getData() ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @throws SourceWatcherException |
110
|
|
|
*/ |
111
|
|
|
public function testSend () : void |
112
|
|
|
{ |
113
|
|
|
$communicator = new SlackCommunicator(); |
114
|
|
|
$communicator->setWebHookUrl( $this->slackWebHook ); |
115
|
|
|
$communicator->setMethod( $this->method ); |
116
|
|
|
$communicator->setContentType( $this->contentType ); |
117
|
|
|
$communicator->setData( json_encode( $this->mockResults ) ); |
118
|
|
|
|
119
|
|
|
$result = $communicator->send(); |
120
|
|
|
$this->assertNotNull( $result ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @throws SourceWatcherException |
125
|
|
|
*/ |
126
|
|
|
public function testNoWebHookException () : void |
127
|
|
|
{ |
128
|
|
|
$communicator = new SlackCommunicator(); |
129
|
|
|
$communicator->setWebHookUrl( "" ); |
130
|
|
|
|
131
|
|
|
$this->expectException( SourceWatcherException::class ); |
132
|
|
|
$this->expectExceptionMessage( Internationalization::getInstance()->getText( SlackCommunicator::class, |
133
|
|
|
"No_Web_Hook" ) ); |
134
|
|
|
|
135
|
|
|
$communicator->send(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @throws SourceWatcherException |
140
|
|
|
*/ |
141
|
|
|
public function testNoMethodException () : void |
142
|
|
|
{ |
143
|
|
|
$communicator = new SlackCommunicator(); |
144
|
|
|
$communicator->setWebHookUrl( $this->slackWebHook ); |
145
|
|
|
$communicator->setMethod( "" ); |
146
|
|
|
|
147
|
|
|
$this->expectException( SourceWatcherException::class ); |
148
|
|
|
$this->expectExceptionMessage( Internationalization::getInstance()->getText( SlackCommunicator::class, |
149
|
|
|
"No_Method" ) ); |
150
|
|
|
|
151
|
|
|
$communicator->send(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @throws SourceWatcherException |
156
|
|
|
*/ |
157
|
|
|
public function testNoContentTypeException () : void |
158
|
|
|
{ |
159
|
|
|
$communicator = new SlackCommunicator(); |
160
|
|
|
$communicator->setWebHookUrl( $this->slackWebHook ); |
161
|
|
|
$communicator->setMethod( $this->method ); |
162
|
|
|
$communicator->setContentType( "" ); |
163
|
|
|
|
164
|
|
|
$this->expectException( SourceWatcherException::class ); |
165
|
|
|
$this->expectExceptionMessage( Internationalization::getInstance()->getText( SlackCommunicator::class, |
166
|
|
|
"No_Content_Type" ) ); |
167
|
|
|
|
168
|
|
|
$communicator->send(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @throws SourceWatcherException |
173
|
|
|
*/ |
174
|
|
|
public function testNoDataException () : void |
175
|
|
|
{ |
176
|
|
|
$communicator = new SlackCommunicator(); |
177
|
|
|
$communicator->setWebHookUrl( $this->slackWebHook ); |
178
|
|
|
$communicator->setMethod( $this->method ); |
179
|
|
|
$communicator->setContentType( $this->contentType ); |
180
|
|
|
|
181
|
|
|
$this->expectException( SourceWatcherException::class ); |
182
|
|
|
$this->expectExceptionMessage( Internationalization::getInstance()->getText( SlackCommunicator::class, |
183
|
|
|
"No_Data" ) ); |
184
|
|
|
|
185
|
|
|
$communicator->send(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|