1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shippinno\NextEngine\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\NextEngine\OAuth2\Client\Provider\NextEngineProvider; |
11
|
|
|
|
12
|
|
|
class NextEngineTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var NextEngineProvider |
16
|
|
|
*/ |
17
|
|
|
protected $provider; |
18
|
|
|
|
19
|
|
|
protected function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->provider = new NextEngineProvider([ |
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('client_secret', $query); |
36
|
|
|
$this->assertArrayHasKey('redirect_uri', $query); |
37
|
|
|
$this->assertNull($this->provider->getState()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testResourceOwnerDetailsUrl() |
41
|
|
|
{ |
42
|
|
|
$token = Mockery::mock(AccessToken::class); |
43
|
|
|
$this->assertEquals('', $this->provider->getResourceOwnerDetailsUrl($token)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetAccessToken() |
47
|
|
|
{ |
48
|
|
|
$response = Mockery::mock(ResponseInterface::class); |
49
|
|
|
$response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "token_type":"bearer"}'); |
50
|
|
|
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
51
|
|
|
$response->shouldReceive('getStatusCode')->andReturn(200); |
52
|
|
|
$client = Mockery::mock(ClientInterface::class); |
53
|
|
|
$client->shouldReceive('send')->once()->andReturn($response); |
54
|
|
|
$this->provider->setHttpClient($client); |
55
|
|
|
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); |
56
|
|
|
$this->assertEquals('mock_access_token', $token->getToken()); |
57
|
|
|
$this->assertNull($token->getExpires()); |
58
|
|
|
$this->assertNull($token->getRefreshToken()); |
59
|
|
|
$this->assertNull($token->getResourceOwnerId()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException |
64
|
|
|
*/ |
65
|
|
|
public function testExceptionThrownWhenErrorObjectReceived() |
66
|
|
|
{ |
67
|
|
|
$message = uniqid(); |
68
|
|
|
$status = rand(400, 600); |
69
|
|
|
$postResponse = Mockery::mock(ResponseInterface::class); |
70
|
|
|
$postResponse->shouldReceive('getBody')->andReturn(' {"error":"' . $message . '"}'); |
71
|
|
|
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']); |
72
|
|
|
$postResponse->shouldReceive('getStatusCode')->andReturn($status); |
73
|
|
|
$client = Mockery::mock('GuzzleHttp\ClientInterface'); |
74
|
|
|
$client->shouldReceive('send')->once()->andReturn($postResponse); |
75
|
|
|
$this->provider->setHttpClient($client); |
76
|
|
|
$this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testUserData() |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|