Completed
Pull Request — master (#479)
by Andrey
05:46
created

YandexTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 125
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAuthorizationUriWithoutAdditionalParams() 0 16 1
A testGetAuthorizationEndpoint() 0 10 1
A testGetAccessTokenEndpoint() 0 10 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 15 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 15 1
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 13 1
A testParseAccessTokenResponseValidWithRefreshToken() 0 13 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\OAuth2\Service\Yandex;
6
use OAuth\Common\Token\TokenInterface;
7
8
class YandexTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers OAuth\OAuth2\Service\Yandex::__construct
12
     * @covers OAuth\OAuth2\Service\Yandex::getAuthorizationUri
13
     */
14
    public function testGetAuthorizationUriWithoutAdditionalParams()
15
    {
16
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
17
        $credentials->expects($this->once())->method('getConsumerId')->will($this->returnValue('foo'));
18
19
        $service = new Yandex(
20
          $credentials,
21
          $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
22
          $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
23
        );
24
25
        $this->assertSame(
26
          'https://oauth.yandex.ru/authorize?client_id=foo&response_type=code',
27
          $service->getAuthorizationUri()->getAbsoluteUri()
28
        );
29
    }
30
31
    /**
32
     * @covers OAuth\OAuth2\Service\Yandex::getAuthorizationEndpoint
33
     */
34
    public function testGetAuthorizationEndpoint()
35
    {
36
        $service = new Yandex(
37
          $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
38
          $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
39
          $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
40
        );
41
42
        $this->assertSame('https://oauth.yandex.ru/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
43
    }
44
    
45
    /**
46
     * @covers OAuth\OAuth2\Service\Yandex::getAccessTokenEndpoint
47
     */
48
    public function testGetAccessTokenEndpoint()
49
    {
50
        $service = new Yandex(
51
          $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
52
          $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
53
          $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
54
        );
55
56
        $this->assertSame('https://oauth.yandex.ru/token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
57
    }
58
    
59
    /**
60
     * @covers OAuth\OAuth2\Service\Yandex::parseAccessTokenResponse
61
     */
62
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
63
    {
64
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
65
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
66
67
        $service = new Yandex(
68
          $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
69
          $client,
70
          $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
71
        );
72
73
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
74
75
        $service->requestAccessToken('foo');
76
    }
77
78
    /**
79
     * @covers OAuth\OAuth2\Service\Yandex::parseAccessTokenResponse
80
     */
81
    public function testParseAccessTokenResponseThrowsExceptionOnError()
82
    {
83
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
84
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
85
86
        $service = new Yandex(
87
          $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
88
          $client,
89
          $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
90
        );
91
92
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
93
94
        $service->requestAccessToken('foo');
95
    }
96
97
    /**
98
     * @covers OAuth\OAuth2\Service\Yandex::parseAccessTokenResponse
99
     */
100
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
101
    {
102
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
103
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
104
105
        $service = new Yandex(
106
          $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
107
          $client,
108
          $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
109
        );
110
111
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
112
    }
113
114
    /**
115
     * @covers OAuth\OAuth2\Service\Yandex::__construct
116
     * @covers OAuth\OAuth2\Service\Yandex::parseAccessTokenResponse
117
     */
118
    public function testParseAccessTokenResponseValidWithRefreshToken()
119
    {
120
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
121
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));
122
123
        $service = new Yandex(
124
          $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
125
          $client,
126
          $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
127
        );
128
129
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
130
    }
131
132
}
133