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

SlackAuthControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
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 11 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 GuzzleHttp\Client;
7
use Psr\Http\Message\ResponseInterface;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Control\HTTPResponse;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\SiteConfig\SiteConfig;
14
15
/**
16
 * Test various items on the SlackAuthController
17
 *
18
 * Class SlackAuthControllerTest
19
 */
20
class SlackAuthControllerTest extends SapphireTest
21
{
22
    /**
23
     * @var SlackAuthController
24
     */
25
    protected $controller;
26
27
    public function setUp()
28
    {
29
        $this->controller = Injector::inst()->get(SlackAuthController::class);
30
        parent::setUp();
31
    }
32
33
    public function tearDown()
34
    {
35
        parent::tearDown();
36
    }
37
38
    public function testGetQuery()
39
    {
40
        $config = SiteConfig::current_site_config();
41
        $result = $this->controller->getQuery($config, '1234567890987654321');
42
        $expectedArray = [
43
            'client_id'     => $config->SlackClientID,
44
            'client_secret' => $config->SlackClientSecret,
45
            'code'          => '1234567890987654321',
46
            'redirect_uri'  => Director::absoluteURL('/SlackAuthorization/'),
47
        ];
48
        $expected = http_build_query($expectedArray);
49
        $this->assertEquals($expected, $result);
50
    }
51
52
    public function testSaveToken()
53
    {
54
        $response = new HTTPResponse('{"access_token":"12345678chdiyp67"}');
55
56
        $config = SiteConfig::current_site_config();
57
58
        $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

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