Passed
Push — master ( 1c6332...1ba0c5 )
by Jean Paul
06:52
created

SlackCommunicatorTest::testSetGetMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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