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

ConnectorTest::testGetAuthenticatedRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 45
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 76
rs 9.2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
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);
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

101
        /** @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...
102
        $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

102
        /** @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...
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);
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

109
        /** @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...
110
        $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

110
        /** @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...
111
        $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

111
        /** @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...
112
113
        // Delete the cached token again to clean up.
114
        $delete = $cache->deleteItem('cloudapi-token');
115
        $this->assertTrue($delete);
116
    }
117
}
118