Passed
Pull Request — master (#11)
by Simon
01:34
created

SlackStatusControllerTest::testSVG()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 2
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\StripeSlack\Test;
4
5
use Firesphere\StripeSlack\Controller\SlackStatusController;
6
use Firesphere\StripeSlack\Model\SlackUserCount;
7
use GuzzleHttp\Client;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\SiteConfig\SiteConfig;
12
13
class SlackStatusControllerTest extends SapphireTest
14
{
15
    protected static $fixture_file = '../fixtures/count.yml';
16
17
18
    protected function setUp()
19
    {
20
        $config = SiteConfig::current_site_config();
21
        $config->SlackURL = 'https://team.slack.com';
22
        $config->SlackToken = '1234567890';
23
        $config->SlackChannel = 'EAR9887';
24
        $config->write();
25
        parent::setUp();
26
    }
27
28
    protected function tearDown()
29
    {
30
        /** @var DataList|SlackUserCount[] $counts */
31
        $counts = SlackUserCount::get();
32
        foreach ($counts as $count) {
33
            $count->delete();
34
        }
35
        parent::tearDown();
36
    }
37
38
    public function testInvalidConfig()
39
    {
40
        $controller = SlackStatusController::create(new HTTPRequest('GET', '/SlackStatus/usercount'));
0 ignored issues
show
Bug introduced by
new SilverStripe\Control...SlackStatus/usercount') of type SilverStripe\Control\HTTPRequest is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $controller = SlackStatusController::create(/** @scrutinizer ignore-type */ new HTTPRequest('GET', '/SlackStatus/usercount'));
Loading history...
41
        $config = SiteConfig::current_site_config();
42
        $config->SlackURL = '';
43
        $config->write();
44
        $this->assertEquals('', $controller->usercount());
45
    }
46
47
    public function additionProvider()
48
    {
49
        return [
50
            [[25, 60], 20],
51
            [[35, 65], 200],
52
            [[45, 70], 2000],
53
        ];
54
    }
55
56
    /**
57
     * @dataProvider additionProvider
58
     * @param $expected
59
     * @param $amount
60
     */
61
    public function testGetSVGSettings($expected, $amount)
62
    {
63
        $controller = SlackStatusController::create(new HTTPRequest('GET', '/SlackStatus/usercount'));
0 ignored issues
show
Bug introduced by
new SilverStripe\Control...SlackStatus/usercount') of type SilverStripe\Control\HTTPRequest is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $controller = SlackStatusController::create(/** @scrutinizer ignore-type */ new HTTPRequest('GET', '/SlackStatus/usercount'));
Loading history...
64
        $result = $controller->getSVGSettings($amount);
65
        $this->assertEquals($expected, $result);
66
    }
67
68
    public function testCachedStatus()
69
    {
70
        $controller = SlackStatusController::create(new HTTPRequest('GET', '/SlackStatus/usercount'));
0 ignored issues
show
Bug introduced by
new SilverStripe\Control...SlackStatus/usercount') of type SilverStripe\Control\HTTPRequest is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $controller = SlackStatusController::create(/** @scrutinizer ignore-type */ new HTTPRequest('GET', '/SlackStatus/usercount'));
Loading history...
71
        $this->assertEquals(10, $controller->getStatus(SiteConfig::current_site_config()));
72
    }
73
74
    /**
75
     * @dataProvider additionProvider
76
     * @param $expected
77
     * @param $amount
78
     */
79
    public function testSVG($expected, $amount)
80
    {
81
        $controller = SlackStatusController::create(new HTTPRequest('GET', '/SlackStatus/badge'));
0 ignored issues
show
Bug introduced by
new SilverStripe\Control..., '/SlackStatus/badge') of type SilverStripe\Control\HTTPRequest is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $controller = SlackStatusController::create(/** @scrutinizer ignore-type */ new HTTPRequest('GET', '/SlackStatus/badge'));
Loading history...
82
83
        $counter = SlackUserCount::get()->first();
84
        $counter->UserCount = $amount;
85
        $counter->write();
86
87
        $svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"115\" height=\"20\">
88
    <rect rx=\"3\" width=\"70\" height=\"20\" fill=\"#555\"></rect>
89
    <rect rx=\"3\" x=\"47\" width=\"$expected[0]\" height=\"20\" fill=\"#005b94\"></rect>
90
    <path d=\"M47 0h4v20h-4z\" fill=\"#005b94\"></path>
91
    <g text-anchor=\"middle\" font-family=\"Verdana\" font-size=\"11\">
92
        <text fill=\"#010101\" fill-opacity=\".3\" x=\"24\" y=\"15\">slack</text>
93
        <text fill=\"#fff\" x=\"24\" y=\"14\">slack</text>
94
        <text fill=\"#010101\" fill-opacity=\".3\" x=\"$expected[1]\" y=\"15\">$amount</text>
95
        <text fill=\"#fff\" x=\"$expected[1]\" y=\"14\">$amount</text>
96
    </g>
97
</svg>";
98
        $this->assertEquals($svg, $controller->badge()->getBody());
99
        $this->assertEquals('image/svg+xml', $controller->badge()->getHeader('Content-Type'));
100
    }
101
102
    public function testGetRestfulService()
103
    {
104
        /** @var SlackStatusController $controller */
105
        $controller = SlackStatusController::create();
106
        $result = $controller->getClient(SiteConfig::current_site_config());
107
        $this->assertInstanceOf(Client::class, $result);
108
    }
109
110
    public function testValidateResponse()
111
    {
112
        /** @var SlackUserCount $count */
113
        $count = SlackUserCount::get()->first();
114
        if (!$count) {
115
            $count = SlackUserCount::create();
116
        }
117
        $controller = SlackStatusController::create();
118
        $expected = [
119
            'ok'      => true,
120
            'channel' => [
121
                'members' => ['1', '4', '2']
122
            ]
123
        ];
124
125
        $result = $controller->validateResponse($count, $expected);
126
        $this->assertEquals(count($expected['channel']['members']), $result);
127
        $this->assertEquals(3, $result);
128
        $result = $controller->validateResponse($count, ['ok' => false]);
129
        $this->assertEquals(0, $result);
130
    }
131
}
132