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

SlackAuthControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetQuery() 0 12 1
A testGetConfig() 0 12 1
A setUp() 0 4 1
A tearDown() 0 3 1
A testSaveToken() 0 10 1
1
<?php
2
3
namespace Firesphere\StripeSlack\Test;
4
5
use Firesphere\StripeSlack\Controller\SlackAuthController;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\SiteConfig\SiteConfig;
12
13
/**
14
 * Test various items on the SlackAuthController
15
 *
16
 * Class SlackAuthControllerTest
17
 */
18
class SlackAuthControllerTest extends SapphireTest
19
{
20
    /**
21
     * @var SlackAuthController
22
     */
23
    protected $controller;
24
25
    public function setUp()
26
    {
27
        $this->controller = Injector::inst()->get(SlackAuthController::class);
28
        parent::setUp();
29
    }
30
31
    public function tearDown()
32
    {
33
        parent::tearDown();
34
    }
35
36
    public function testGetQuery()
37
    {
38
        $config = SiteConfig::current_site_config();
39
        $result = $this->controller->getQuery($config, '1234567890987654321');
40
        $expectedArray = [
41
            'client_id'     => $config->SlackClientID,
42
            'client_secret' => $config->SlackClientSecret,
43
            'code'          => '1234567890987654321',
44
            'redirect_uri'  => Director::absoluteURL('/SlackAuthorization/'),
45
        ];
46
        $expected = http_build_query($expectedArray);
47
        $this->assertEquals($expected, $result);
48
    }
49
50
    public function testSaveToken()
51
    {
52
        $response = new HTTPResponse('{"access_token":"12345678chdiyp67"}');
53
54
        $config = SiteConfig::current_site_config();
55
56
        $this->controller->saveToken($response, $config);
0 ignored issues
show
Bug introduced by
$response of type SilverStripe\Control\HTTPResponse is incompatible with the type Psr\Http\Message\ResponseInterface expected by parameter $response of Firesphere\StripeSlack\C...Controller::saveToken(). ( Ignorable by Annotation )

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

56
        $this->controller->saveToken(/** @scrutinizer ignore-type */ $response, $config);
Loading history...
57
        $config = SiteConfig::current_site_config();
58
        // This seems to not work on tests?? SiteConfig seems to not write
59
        $this->assertEquals('12345678chdiyp67', $config->SlackToken);
60
    }
61
62
    public function testGetConfig()
63
    {
64
        $controller = SlackAuthController::create();
65
        $config = SiteConfig::current_site_config();
66
        $config->SlackURL = 'https://team.slack.com';
67
        $config->write();
68
        $result = $controller->getConfig(new HTTPRequest('GET', 'https://team.slack.com/api/something',
69
            ['code' => '1234567890']));
70
        $this->assertEquals('1234567890', $result[0]);
71
        $this->assertInstanceOf(SiteConfig::class, $result[1]);
72
        $this->assertEquals($config->SlackURL . '/', $result[2]);
73
        $this->assertEquals('api/oauth.access?', $result[3]);
74
    }
75
}
76