Test Failed
Pull Request — master (#92)
by Adam
02:50
created

ConnectorTest::testGuzzleRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 38
rs 9.568
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;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Client\Test\Provider\Fake was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

105
        /** @scrutinizer ignore-deprecated */ $this->assertAttributeSame($expectedHeaders, 'headers', $request);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
106
        $this->assertAttributeSame($expectedHeaderNames, 'headerNames', $request);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

106
        /** @scrutinizer ignore-deprecated */ $this->assertAttributeSame($expectedHeaderNames, 'headerNames', $request);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

113
        /** @scrutinizer ignore-deprecated */ $this->assertAttributeSame('acquia-token', 'accessToken', $accessToken);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
114
        $this->assertAttributeSame(3, 'resourceOwnerId', $accessToken);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

114
        /** @scrutinizer ignore-deprecated */ $this->assertAttributeSame(3, 'resourceOwnerId', $accessToken);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
115
        $this->assertAttributeSame($expires, 'expires', $accessToken);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

115
        /** @scrutinizer ignore-deprecated */ $this->assertAttributeSame($expires, 'expires', $accessToken);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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