Completed
Pull Request — master (#455)
by
unknown
03:13
created

testConstructCorrectInterfaceWithoutCustomUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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