1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shippinno\Base\OAuth2\Client\Tests\Provider; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
7
|
|
|
use Mockery; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Shippinno\Base\OAuth2\Client\Provider\BaseProvider; |
11
|
|
|
|
12
|
|
|
class BaseTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var BaseProvider |
16
|
|
|
*/ |
17
|
|
|
protected $provider; |
18
|
|
|
|
19
|
|
|
protected function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->provider = new BaseProvider([ |
22
|
|
|
'clientId' => 'mock_client_id', |
23
|
|
|
'clientSecret' => 'mock_secret', |
24
|
|
|
'redirectUri' => 'mock_redirect_uri', |
25
|
|
|
]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testAuthorizationUrl() |
29
|
|
|
{ |
30
|
|
|
$url = $this->provider->getAuthorizationUrl(); |
31
|
|
|
$uri = parse_url($url); |
32
|
|
|
parse_str($uri['query'], $query); |
33
|
|
|
|
34
|
|
|
$this->assertArrayHasKey('client_id', $query); |
35
|
|
|
$this->assertArrayHasKey('redirect_uri', $query); |
36
|
|
|
$this->assertArrayHasKey('state', $query); |
37
|
|
|
$this->assertArrayHasKey('scope', $query); |
38
|
|
|
$this->assertArrayHasKey('response_type', $query); |
39
|
|
|
$this->assertArrayHasKey('approval_prompt', $query); |
40
|
|
|
$this->assertNotNull($this->provider->getState()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testResourceOwnerDetailsUrl() |
44
|
|
|
{ |
45
|
|
|
$token = Mockery::mock(AccessToken::class); |
46
|
|
|
$url = $this->provider->getResourceOwnerDetailsUrl($token); |
47
|
|
|
$uri = parse_url($url); |
48
|
|
|
$this->assertEquals('/1/users/me', $uri['path']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetAccessToken() |
52
|
|
|
{ |
53
|
|
|
$response = Mockery::mock(ResponseInterface::class); |
54
|
|
|
$response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "token_type":"bearer"}'); |
55
|
|
|
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
56
|
|
|
$response->shouldReceive('getStatusCode')->andReturn(200); |
57
|
|
|
$client = Mockery::mock(ClientInterface::class); |
58
|
|
|
$client->shouldReceive('send')->once()->andReturn($response); |
59
|
|
|
$this->provider->setHttpClient($client); |
60
|
|
|
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); |
61
|
|
|
$this->assertEquals('mock_access_token', $token->getToken()); |
62
|
|
|
$this->assertNull($token->getExpires()); |
63
|
|
|
$this->assertNull($token->getRefreshToken()); |
64
|
|
|
$this->assertNull($token->getResourceOwnerId()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException |
69
|
|
|
*/ |
70
|
|
|
public function testExceptionThrownWhenErrorObjectReceived() |
71
|
|
|
{ |
72
|
|
|
$message = uniqid(); |
73
|
|
|
$status = rand(400, 600); |
74
|
|
|
$postResponse = Mockery::mock(ResponseInterface::class); |
75
|
|
|
$postResponse->shouldReceive('getBody')->andReturn(' {"error":"' . $message . '"}'); |
76
|
|
|
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
77
|
|
|
$postResponse->shouldReceive('getStatusCode')->andReturn($status); |
78
|
|
|
$client = Mockery::mock('GuzzleHttp\ClientInterface'); |
79
|
|
|
$client->shouldReceive('send')->once()->andReturn($postResponse); |
80
|
|
|
$this->provider->setHttpClient($client); |
81
|
|
|
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testUserData() |
85
|
|
|
{ |
86
|
|
|
$shopId = uniqid(); |
87
|
|
|
$shopName = uniqid(); |
88
|
|
|
$postResponse = Mockery::mock(ResponseInterface::class); |
89
|
|
|
$postResponse->shouldReceive('getBody')->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token'); |
90
|
|
|
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']); |
91
|
|
|
$postResponse->shouldReceive('getStatusCode')->andReturn(200); |
92
|
|
|
$accountResponse = Mockery::mock(ResponseInterface::class); |
93
|
|
|
$accountResponse->shouldReceive('getBody')->andReturn('{"user":{"shop_id":"'.$shopId.'","shop_name":"'.$shopName.'","shop_introduction":null,"shop_url":"'.$shopId.'","twitter_id":null,"facebook_id":null,"ameba_id":null,"instagram_id":null,"background":null,"display_background":0,"repeat_background":1,"logo":null,"display_logo":0}}'); |
94
|
|
|
$accountResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
95
|
|
|
$accountResponse->shouldReceive('getStatusCode')->andReturn(200); |
96
|
|
|
$client = Mockery::mock(ClientInterface::class); |
97
|
|
|
$client->shouldReceive('send') |
98
|
|
|
->times(2) |
99
|
|
|
->andReturn($postResponse, $accountResponse); |
100
|
|
|
$this->provider->setHttpClient($client); |
101
|
|
|
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); |
102
|
|
|
$account = $this->provider->getResourceOwner($token); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($shopId, $account->getId()); |
105
|
|
|
$this->assertEquals($shopId, $account->toArray()['user']['shop_id']); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|