1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\Tests\Endpoints; |
4
|
|
|
|
5
|
|
|
use AcquiaCloudApi\Tests\CloudApiTestCase; |
6
|
|
|
use AcquiaCloudApi\Connector\Client; |
7
|
|
|
use AcquiaCloudApi\Connector\Connector; |
8
|
|
|
use AcquiaCloudApi\Endpoints\Applications; |
9
|
|
|
use League\OAuth2\Client\Test\Provider\Fake as MockProvider; |
|
|
|
|
10
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
11
|
|
|
use League\OAuth2\Client\Grant\AbstractGrant; |
12
|
|
|
use Psr\Http\Message\StreamInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Eloquent\Phony\Phpunit\Phony; |
15
|
|
|
use GuzzleHttp\ClientInterface; |
16
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
17
|
|
|
|
18
|
|
|
class ConnectorTest extends CloudApiTestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
public function testConnector() |
22
|
|
|
{ |
23
|
|
|
$config = [ |
24
|
|
|
'key' => 'key', |
25
|
|
|
'secret' => 'secret' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
$connector = new Connector($config); |
29
|
|
|
|
30
|
|
|
$this->assertEquals( |
31
|
|
|
$connector::BASE_URI, |
32
|
|
|
'https://cloud.acquia.com/api' |
33
|
|
|
); |
34
|
|
|
$this->assertEquals( |
35
|
|
|
$connector::URL_ACCESS_TOKEN, |
36
|
|
|
'https://accounts.acquia.com/api/auth/oauth/token' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testGetAuthenticatedRequest() |
41
|
|
|
{ |
42
|
|
|
// Clear the cache to make sure we get fresh results during testing. |
43
|
|
|
$cache = new FilesystemAdapter('acquia-php-sdk-v2'); |
44
|
|
|
$cache->deleteItem('cloudapi-token'); |
45
|
|
|
|
46
|
|
|
$config = [ |
47
|
|
|
'key' => 'key', |
48
|
|
|
'secret' => 'secret' |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
// Create a new Connector and override the provider property set in the constructor. |
52
|
|
|
$connector = new Connector($config); |
53
|
|
|
$reflectionClass = new \ReflectionClass('AcquiaCloudApi\Connector\Connector'); |
54
|
|
|
|
55
|
|
|
$provider = new MockProvider([ |
56
|
|
|
'clientId' => 'mock_client_id', |
57
|
|
|
'clientSecret' => 'mock_secret', |
58
|
|
|
'redirectUri' => 'none', |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$providerProperty = $reflectionClass->getProperty('provider'); |
62
|
|
|
$providerProperty->setAccessible(true); |
63
|
|
|
$providerProperty->setValue($connector, $provider); |
64
|
|
|
|
65
|
|
|
// Create the mock response from the call to get the access token. |
66
|
|
|
$expires = time() + 300; |
67
|
|
|
$raw_response = ['access_token' => 'acquia-token', 'expires' => $expires, 'resource_owner_id' => 3]; |
68
|
|
|
|
69
|
|
|
$grant = Phony::mock(AbstractGrant::class); |
70
|
|
|
$grant->prepareRequestParameters->returns([]); |
71
|
|
|
|
72
|
|
|
$stream = Phony::mock(StreamInterface::class); |
73
|
|
|
$stream->__toString->returns(json_encode($raw_response)); |
74
|
|
|
|
75
|
|
|
$response = Phony::mock(ResponseInterface::class); |
76
|
|
|
$response->getBody->returns($stream->get()); |
77
|
|
|
$response->getHeader->with('content-type')->returns('application/json'); |
78
|
|
|
|
79
|
|
|
$client = Phony::mock(ClientInterface::class); |
80
|
|
|
$client->send->returns($response->get()); |
81
|
|
|
|
82
|
|
|
$provider->setHttpClient($client->get()); |
83
|
|
|
|
84
|
|
|
// Create the request and check it matches our expectations. |
85
|
|
|
$request = $connector->createRequest('get', '/account'); |
86
|
|
|
$this->assertInstanceOf('GuzzleHttp\Psr7\Request', $request); |
87
|
|
|
|
88
|
|
|
$expectedHeaders = [ |
89
|
|
|
'Host' => [ |
90
|
|
|
'cloud.acquia.com', |
91
|
|
|
], |
92
|
|
|
'Authorization' => [ |
93
|
|
|
'Bearer acquia-token', |
94
|
|
|
] |
95
|
|
|
]; |
96
|
|
|
$expectedHeaderNames = [ |
97
|
|
|
'authorization' => 'Authorization', |
98
|
|
|
'host' => 'Host', |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
$this->assertAttributeSame($expectedHeaders, 'headers', $request); |
|
|
|
|
102
|
|
|
$this->assertAttributeSame($expectedHeaderNames, 'headerNames', $request); |
|
|
|
|
103
|
|
|
|
104
|
|
|
// Check the cache to make sure that the token has been cached successfully. |
105
|
|
|
$accessToken = $cache->getItem('cloudapi-token')->get(); |
106
|
|
|
|
107
|
|
|
// Ensure that the cached item is an AccessToken and that it contains the values we set above. |
108
|
|
|
$this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $accessToken); |
109
|
|
|
$this->assertAttributeSame('acquia-token', 'accessToken', $accessToken); |
|
|
|
|
110
|
|
|
$this->assertAttributeSame(3, 'resourceOwnerId', $accessToken); |
|
|
|
|
111
|
|
|
$this->assertAttributeSame($expires, 'expires', $accessToken); |
|
|
|
|
112
|
|
|
|
113
|
|
|
// Delete the cached token again to clean up. |
114
|
|
|
$delete = $cache->deleteItem('cloudapi-token'); |
115
|
|
|
$this->assertTrue($delete); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths