1+ <?php
2+
3+ namespace OAuthTest \Unit \OAuth2 \Service ;
4+
5+ use OAuth \Common \Token \TokenInterface ;
6+ use OAuth \OAuth2 \Service \DeviantArt ;
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 ('\\Buzz \\Browser ' ),
18+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' )
19+ );
20+
21+ $ this ->assertInstanceOf ('\\OAuth \\OAuth2 \\Service \\ServiceInterface ' , $ service );
22+ }
23+ /**
24+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
25+ */
26+ public function testConstructCorrectInstanceWithoutCustomUri ()
27+ {
28+ $ service = new DeviantArt (
29+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
30+ $ this ->getMock ('\\Buzz \\Browser ' ),
31+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' )
32+ );
33+
34+ $ this ->assertInstanceOf ('\\OAuth \\OAuth2 \\Service \\AbstractService ' , $ service );
35+ }
36+ /**
37+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
38+ */
39+ public function testConstructCorrectInstanceWithCustomUri ()
40+ {
41+ $ service = new DeviantArt (
42+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
43+ $ this ->getMock ('\\Buzz \\Browser ' ),
44+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' ),
45+ [],
46+ ''
47+ );
48+
49+ $ this ->assertInstanceOf ('\\OAuth \\OAuth2 \\Service \\AbstractService ' , $ service );
50+ }
51+
52+ public function testBaseApiUriIsCorrect ()
53+ {
54+ $ token = $ this ->getMock ('\\OAuth \\OAuth2 \\Token \\TokenInterface ' );
55+ $ token ->expects ($ this ->once ())->method ('getEndOfLife ' )->will ($ this ->returnValue (TokenInterface::EOL_NEVER_EXPIRES ));
56+ $ token ->expects ($ this ->once ())->method ('getAccessToken ' )->will ($ this ->returnValue ('foo ' ));
57+
58+ $ storage = $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' );
59+ $ storage ->expects ($ this ->once ())->method ('retrieveAccessToken ' )->will ($ this ->returnValue ($ token ));
60+
61+ /** @var DeviantArt|\PHPUnit_Framework_MockObject_MockObject $service */
62+ $ service = $ this ->getMock ('\\OAuth \\OAuth2 \\Service \\DeviantArt ' , ['httpRequest ' ], [
63+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
64+ $ this ->getMock ('\\Buzz \\Browser ' ),
65+ $ storage
66+ ]);
67+
68+ $ service ->expects ($ this ->once ())->method ('httpRequest ' )->willReturnArgument (0 );
69+
70+ $ this ->assertEquals ('https://www.deviantart.com/api/v1/oauth2//api/method ' , (string ) $ service ->request ('/api/method ' ));
71+ }
72+
73+ /**
74+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
75+ * @covers OAuth\OAuth2\Service\DeviantArt::getAuthorizationEndpoint
76+ */
77+ public function testGetAuthorizationEndpoint ()
78+ {
79+ $ service = new DeviantArt (
80+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
81+ $ this ->getMock ('\\Buzz \\Browser ' ),
82+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' )
83+ );
84+
85+ $ this ->assertSame ('https://www.deviantart.com/oauth2/authorize ' , (string ) $ service ->getAuthorizationEndpoint ());
86+ }
87+ /**
88+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
89+ * @covers OAuth\OAuth2\Service\DeviantArt::getAccessTokenEndpoint
90+ */
91+ public function testGetAccessTokenEndpoint ()
92+ {
93+ $ service = new DeviantArt (
94+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
95+ $ this ->getMock ('\\Buzz \\Browser ' ),
96+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' )
97+ );
98+
99+ $ this ->assertSame ('https://www.deviantart.com/oauth2/token ' , (string ) $ service ->getAccessTokenEndpoint ());
100+ }
101+ /**
102+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
103+ */
104+ public function testGetAuthorizationMethod ()
105+ {
106+ $ token = $ this ->getMock ('\\OAuth \\OAuth2 \\Token \\TokenInterface ' );
107+ $ token ->expects ($ this ->once ())->method ('getEndOfLife ' )->will ($ this ->returnValue (TokenInterface::EOL_NEVER_EXPIRES ));
108+ $ token ->expects ($ this ->once ())->method ('getAccessToken ' )->will ($ this ->returnValue ('foo ' ));
109+
110+ $ storage = $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' );
111+ $ storage ->expects ($ this ->once ())->method ('retrieveAccessToken ' )->will ($ this ->returnValue ($ token ));
112+
113+ /** @var DeviantArt|\PHPUnit_Framework_MockObject_MockObject $service */
114+ $ service = $ this ->getMock ('\\OAuth \\OAuth2 \\Service \\DeviantArt ' , ['httpRequest ' ], [
115+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
116+ $ this ->getMock ('\\Buzz \\Browser ' ),
117+ $ storage
118+ ]);
119+
120+ $ service ->expects ($ this ->once ())->method ('httpRequest ' )->will ($ this ->returnArgument (2 ));
121+
122+ $ headers = $ service ->request ('https://pieterhordijk.com/my/awesome/path ' );
123+ $ this ->assertTrue (array_key_exists ('Authorization ' , $ headers ));
124+ $ this ->assertTrue (in_array ('OAuth foo ' , $ headers , true ));
125+ }
126+ /**
127+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
128+ * @covers OAuth\OAuth2\Service\DeviantArt::parseAccessTokenResponse
129+ */
130+ public function testParseAccessTokenResponseThrowsExceptionOnError ()
131+ {
132+ /** @var DeviantArt|\PHPUnit_Framework_MockObject_MockObject $service */
133+ $ service = $ this ->getMock ('\\OAuth \\OAuth2 \\Service \\DeviantArt ' , ['httpRequest ' ], [
134+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
135+ $ this ->getMock ('\\Buzz \\Browser ' ),
136+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' )
137+ ]);
138+
139+ $ service ->expects ($ this ->once ())->method ('httpRequest ' )->will ($ this ->returnValue ('error=some_error ' ));
140+
141+ $ this ->setExpectedException ('\\OAuth \\Common \\Http \\Exception \\TokenResponseException ' );
142+ $ service ->requestAccessToken ('foo ' );
143+ }
144+ /**
145+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
146+ * @covers OAuth\OAuth2\Service\DeviantArt::parseAccessTokenResponse
147+ */
148+ public function testParseAccessTokenResponseValidWithoutRefreshToken ()
149+ {
150+ /** @var DeviantArt|\PHPUnit_Framework_MockObject_MockObject $service */
151+ $ service = $ this ->getMock ('\\OAuth \\OAuth2 \\Service \\DeviantArt ' , ['httpRequest ' ], [
152+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
153+ $ this ->getMock ('\\Buzz \\Browser ' ),
154+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' )
155+ ]);
156+
157+ $ service ->expects ($ this ->once ())->method ('httpRequest ' )->will ($ this ->returnValue ('{"access_token":"foo","expires_in":"bar"} ' ));
158+
159+ $ this ->assertInstanceOf ('\\OAuth \\OAuth2 \\Token \\StdOAuth2Token ' , $ service ->requestAccessToken ('foo ' ));
160+ }
161+ /**
162+ * @covers OAuth\OAuth2\Service\DeviantArt::__construct
163+ * @covers OAuth\OAuth2\Service\DeviantArt::parseAccessTokenResponse
164+ */
165+ public function testParseAccessTokenResponseValidWithRefreshToken ()
166+ {
167+ /** @var DeviantArt|\PHPUnit_Framework_MockObject_MockObject $service */
168+ $ service = $ this ->getMock ('\\OAuth \\OAuth2 \\Service \\DeviantArt ' , ['httpRequest ' ], [
169+ $ this ->getMock ('\\OAuth \\Common \\Consumer \\CredentialsInterface ' ),
170+ $ this ->getMock ('\\Buzz \\Browser ' ),
171+ $ this ->getMock ('\\OAuth \\Common \\Storage \\TokenStorageInterface ' )
172+ ]);
173+
174+ $ service ->expects ($ this ->once ())->method ('httpRequest ' )->will ($ this ->returnValue ('{"access_token":"foo","expires_in":"bar","refersh_token":"baz"} ' ));
175+
176+ $ this ->assertInstanceOf ('\\OAuth \\OAuth2 \\Token \\StdOAuth2Token ' , $ service ->requestAccessToken ('foo ' ));
177+ }
178+ }
0 commit comments