|
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')); |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
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')); |
|
|
|
|
|
|
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
|
|
|
|