testConstructCorrectInstanceWithoutCustomUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 9
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\Bitly;
7
use PHPUnit\Framework\TestCase;
8
9
class BitlyTest extends TestCase
10
{
11
    /**
12
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
13
     */
14
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
15
    {
16
        $service = new Bitly(
17
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
18
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
19
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
20
        );
21
22
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
23
    }
24
25
    /**
26
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri(): void
29
    {
30
        $service = new Bitly(
31
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
32
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
33
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
34
        );
35
36
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
37
    }
38
39
    /**
40
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
41
     */
42
    public function testConstructCorrectInstanceWithCustomUri(): void
43
    {
44
        $service = new Bitly(
45
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
46
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
47
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
48
            [],
49
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
50
        );
51
52
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
53
    }
54
55
    /**
56
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
57
     * @covers \OAuth\OAuth2\Service\Bitly::getAuthorizationEndpoint
58
     */
59
    public function testGetAuthorizationEndpoint(): void
60
    {
61
        $service = new Bitly(
62
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
63
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
64
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
65
        );
66
67
        self::assertSame('https://bitly.com/oauth/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
68
    }
69
70
    /**
71
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
72
     * @covers \OAuth\OAuth2\Service\Bitly::getAccessTokenEndpoint
73
     */
74
    public function testGetAccessTokenEndpoint(): void
75
    {
76
        $service = new Bitly(
77
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
78
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
79
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
80
        );
81
82
        self::assertSame('https://api-ssl.bitly.com/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
83
    }
84
85
    /**
86
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
87
     * @covers \OAuth\OAuth2\Service\Bitly::getAuthorizationMethod
88
     */
89
    public function testGetAuthorizationMethod(): void
90
    {
91
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
92
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0);
93
94
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
95
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
96
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
97
98
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
99
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
100
101
        $service = new Bitly(
102
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
103
            $client,
104
            $storage
105
        );
106
107
        $uri = $service->request('https://pieterhordijk.com/my/awesome/path');
108
        $absoluteUri = parse_url($uri->getAbsoluteUri());
109
110
        self::assertSame('access_token=foo', $absoluteUri['query']);
111
    }
112
113
    /**
114
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
115
     * @covers \OAuth\OAuth2\Service\Bitly::parseAccessTokenResponse
116
     */
117
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
118
    {
119
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
120
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error');
121
122
        $service = new Bitly(
123
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
124
            $client,
125
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
126
        );
127
128
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
129
130
        $service->requestAccessToken('foo');
131
    }
132
133
    /**
134
     * @covers \OAuth\OAuth2\Service\Bitly::__construct
135
     * @covers \OAuth\OAuth2\Service\Bitly::parseAccessTokenResponse
136
     * @covers \OAuth\OAuth2\Service\Bitly::requestAccessToken
137
     */
138
    public function testParseAccessTokenResponseValid(): void
139
    {
140
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
141
        $client->expects(self::once())->method('retrieveResponse')->willReturn('access_token=foo');
142
143
        $service = new Bitly(
144
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
145
            $client,
146
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
147
        );
148
149
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
150
    }
151
}
152