Completed
Pull Request — master (#552)
by
unknown
09:50
created

testConstructCorrectInstanceWithCustomUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\OAuth2\Service\Xing;
6
7
class XingTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @covers OAuth\OAuth2\Service\Xing::__construct
11
     */
12
    public function testConstructCorrectInterfaceWithoutCustomUri()
13
    {
14
        $service = new Xing(
15
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
16
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
17
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
18
        );
19
20
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
21
    }
22
23
    /**
24
     * @covers OAuth\OAuth2\Service\Xing::__construct
25
     */
26
    public function testConstructCorrectInstanceWithoutCustomUri()
27
    {
28
        $service = new Xing(
29
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
30
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
31
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
32
        );
33
34
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
35
    }
36
37
    /**
38
     * @covers OAuth\OAuth2\Service\Xing::__construct
39
     */
40
    public function testConstructCorrectInstanceWithCustomUri()
41
    {
42
        $service = new Xing(
43
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
44
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
45
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
46
            array(),
47
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
48
        );
49
50
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
51
    }
52
53
    /**
54
     * @covers OAuth\OAuth2\Service\Xing::__construct
55
     * @covers OAuth\OAuth2\Service\Xing::getAuthorizationEndpoint
56
     */
57
    public function testGetAuthorizationEndpoint()
58
    {
59
        $service = new Xing(
60
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
61
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
62
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
63
        );
64
65
        $this->assertSame('https://api.xing.com/auth/oauth2/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
66
    }
67
68
    /**
69
     * @covers OAuth\OAuth2\Service\Xing::__construct
70
     * @covers OAuth\OAuth2\Service\Xing::getAccessTokenEndpoint
71
     */
72
    public function testGetAccessTokenEndpoint()
73
    {
74
        $service = new Xing(
75
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
76
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
77
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
78
        );
79
80
        $this->assertSame('https://api.xing.com/auth/oauth2/token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
81
    }
82
83
    /**
84
     * @covers OAuth\OAuth2\Service\Xing::__construct
85
     * @covers OAuth\OAuth2\Service\Xing::parseAccessTokenResponse
86
     */
87
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
88
    {
89
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
90
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
91
92
        $service = new Xing(
93
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
94
            $client,
95
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
96
        );
97
98
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
99
100
        $service->requestAccessToken('foo');
101
    }
102
103
    /**
104
     * @covers OAuth\OAuth2\Service\Xing::__construct
105
     * @covers OAuth\OAuth2\Service\Xing::parseAccessTokenResponse
106
     */
107
    public function testParseAccessTokenResponseThrowsExceptionOnError()
108
    {
109
        $error = array(
110
            'error' => 'some_error',
111
            'error_description' => 'something went very wrong',
112
            'error_uri' => 'this imaginary link contains more information'
113
        );
114
115
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
116
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(json_encode($error)));
117
118
        $service = new Xing(
119
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
120
            $client,
121
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
122
        );
123
124
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
125
126
        $service->requestAccessToken('foo');
127
    }
128
129
    /**
130
     * @covers OAuth\OAuth2\Service\Xing::__construct
131
     * @covers OAuth\OAuth2\Service\Xing::parseAccessTokenResponse
132
     */
133
    public function testParseAccessTokenResponseValidWithRefreshToken()
134
    {
135
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
136
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));
137
138
        $service = new Xing(
139
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
140
            $client,
141
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
142
        );
143
144
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
145
    }
146
}
147