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
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
18
|
|
|
use GuzzleHttp\Handler\MockHandler; |
19
|
|
|
use GuzzleHttp\Psr7\Response; |
20
|
|
|
use GuzzleHttp\Psr7\Request; |
21
|
|
|
|
22
|
|
|
class ConnectorTest extends CloudApiTestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
public function testConnector() |
26
|
|
|
{ |
27
|
|
|
$config = [ |
28
|
|
|
'key' => 'key', |
29
|
|
|
'secret' => 'secret' |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
$connector = new Connector($config); |
33
|
|
|
|
34
|
|
|
$this->assertEquals( |
35
|
|
|
$connector::BASE_URI, |
36
|
|
|
'https://cloud.acquia.com/api' |
37
|
|
|
); |
38
|
|
|
$this->assertEquals( |
39
|
|
|
$connector::URL_ACCESS_TOKEN, |
40
|
|
|
'https://accounts.acquia.com/api/auth/oauth/token' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetAuthenticatedRequest() |
45
|
|
|
{ |
46
|
|
|
// Clear the cache to make sure we get fresh results during testing. |
47
|
|
|
$cache = new FilesystemAdapter('acquia-php-sdk-v2'); |
48
|
|
|
$cache->deleteItem('cloudapi-token'); |
49
|
|
|
|
50
|
|
|
$config = [ |
51
|
|
|
'key' => 'key', |
52
|
|
|
'secret' => 'secret' |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
// Create a new Connector and override the provider property set in the constructor. |
56
|
|
|
$connector = new Connector($config); |
57
|
|
|
$reflectionClass = new \ReflectionClass('AcquiaCloudApi\Connector\Connector'); |
58
|
|
|
|
59
|
|
|
$provider = new MockProvider([ |
60
|
|
|
'clientId' => 'mock_client_id', |
61
|
|
|
'clientSecret' => 'mock_secret', |
62
|
|
|
'redirectUri' => 'none', |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$providerProperty = $reflectionClass->getProperty('provider'); |
66
|
|
|
$providerProperty->setAccessible(true); |
67
|
|
|
$providerProperty->setValue($connector, $provider); |
68
|
|
|
|
69
|
|
|
// Create the mock response from the call to get the access token. |
70
|
|
|
$expires = time() + 300; |
71
|
|
|
$raw_response = ['access_token' => 'acquia-token', 'expires' => $expires, 'resource_owner_id' => 3]; |
72
|
|
|
|
73
|
|
|
$grant = Phony::mock(AbstractGrant::class); |
74
|
|
|
$grant->prepareRequestParameters->returns([]); |
75
|
|
|
|
76
|
|
|
$stream = Phony::mock(StreamInterface::class); |
77
|
|
|
$stream->__toString->returns(json_encode($raw_response)); |
78
|
|
|
|
79
|
|
|
$response = Phony::mock(ResponseInterface::class); |
80
|
|
|
$response->getBody->returns($stream->get()); |
81
|
|
|
$response->getHeader->with('content-type')->returns('application/json'); |
82
|
|
|
|
83
|
|
|
$client = Phony::mock(ClientInterface::class); |
84
|
|
|
$client->send->returns($response->get()); |
85
|
|
|
|
86
|
|
|
$provider->setHttpClient($client->get()); |
87
|
|
|
|
88
|
|
|
// Create the request and check it matches our expectations. |
89
|
|
|
$request = $connector->createRequest('get', '/account'); |
90
|
|
|
$this->assertInstanceOf('GuzzleHttp\Psr7\Request', $request); |
91
|
|
|
|
92
|
|
|
$expectedHeaders = [ |
93
|
|
|
'Host' => [ |
94
|
|
|
'cloud.acquia.com', |
95
|
|
|
], |
96
|
|
|
'Authorization' => [ |
97
|
|
|
'Bearer acquia-token', |
98
|
|
|
] |
99
|
|
|
]; |
100
|
|
|
$expectedHeaderNames = [ |
101
|
|
|
'authorization' => 'Authorization', |
102
|
|
|
'host' => 'Host', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
$this->assertAttributeSame($expectedHeaders, 'headers', $request); |
|
|
|
|
106
|
|
|
$this->assertAttributeSame($expectedHeaderNames, 'headerNames', $request); |
|
|
|
|
107
|
|
|
|
108
|
|
|
// Check the cache to make sure that the token has been cached successfully. |
109
|
|
|
$accessToken = $cache->getItem('cloudapi-token')->get(); |
110
|
|
|
|
111
|
|
|
// Ensure that the cached item is an AccessToken and that it contains the values we set above. |
112
|
|
|
$this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $accessToken); |
113
|
|
|
$this->assertAttributeSame('acquia-token', 'accessToken', $accessToken); |
|
|
|
|
114
|
|
|
$this->assertAttributeSame(3, 'resourceOwnerId', $accessToken); |
|
|
|
|
115
|
|
|
$this->assertAttributeSame($expires, 'expires', $accessToken); |
|
|
|
|
116
|
|
|
|
117
|
|
|
// Delete the cached token again to clean up. |
118
|
|
|
$delete = $cache->deleteItem('cloudapi-token'); |
119
|
|
|
$this->assertTrue($delete); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testGuzzleRequest() |
123
|
|
|
{ |
124
|
|
|
// Clear the cache to make sure we get fresh results during testing. |
125
|
|
|
$cache = new FilesystemAdapter('acquia-php-sdk-v2'); |
126
|
|
|
$cache->deleteItem('cloudapi-token'); |
127
|
|
|
|
128
|
|
|
// Fake a Guzzle client for the request and response. |
129
|
|
|
$client = new GuzzleClient(['handler' => new MockHandler([new Response()])]); |
130
|
|
|
|
131
|
|
|
// Mock the connector. |
132
|
|
|
$request = new Request('GET', 'https://cloud.acquia.com/api/account'); |
133
|
|
|
$connector = $this |
134
|
|
|
->getMockBuilder('AcquiaCloudApi\Connector\Connector') |
135
|
|
|
->disableOriginalConstructor() |
136
|
|
|
->setMethods(['createRequest']) |
137
|
|
|
->getMock(); |
138
|
|
|
|
139
|
|
|
$connector |
140
|
|
|
->expects($this->atLeastOnce()) |
141
|
|
|
->method('createRequest') |
142
|
|
|
->willReturn($request); |
143
|
|
|
|
144
|
|
|
// Add our fake Guzzle client to the Connector class. |
145
|
|
|
$reflectionClass = new \ReflectionClass('AcquiaCloudApi\Connector\Connector'); |
146
|
|
|
$providerProperty = $reflectionClass->getProperty('client'); |
147
|
|
|
$providerProperty->setAccessible(true); |
148
|
|
|
$providerProperty->setValue($connector, $client); |
149
|
|
|
|
150
|
|
|
// Create the request and check it matches our expectations. |
151
|
|
|
$return = $connector->sendRequest('get', '/account', []); |
152
|
|
|
|
153
|
|
|
// Basic checks to make sure that we get a return code. |
154
|
|
|
$this->assertEquals(200, $return->getStatusCode()); |
155
|
|
|
$this->assertEquals('OK', $return->getReasonPhrase()); |
156
|
|
|
|
157
|
|
|
// Delete the cached token again to clean up. |
158
|
|
|
$delete = $cache->deleteItem('cloudapi-token'); |
159
|
|
|
$this->assertTrue($delete); |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
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