Passed
Push — master ( 3c32e6...6d1be7 )
by Adam
02:49
created

ErrorTest::testErrorException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace AcquiaCloudApi\Tests\Endpoints;
4
5
use AcquiaCloudApi\Tests\CloudApiTestCase;
6
use GuzzleHttp\Exception\BadResponseException;
7
use AcquiaCloudApi\Exception\ApiErrorException;
8
use GuzzleHttp\Psr7\Request;
9
use GuzzleHttp\Psr7\Response;
10
use Psr\Http\Message\RequestInterface;
11
use AcquiaCloudApi\Endpoints\Applications;
12
use AcquiaCloudApi\Connector\Client;
13
use AcquiaCloudApi\Connector\Connector;
14
15
class ErrorTest extends CloudApiTestCase
16
{
17
18
    public function testError403()
19
    {
20
        $response = $this->getPsr7JsonResponseForFixture('Endpoints/Error/error403.json');
21
        $client = $this->getMockClient($response);
22
        $this->expectException(ApiErrorException::class);
23
        $this->expectExceptionMessage('You do not have permission to view applications.');
24
25
        /** @var \AcquiaCloudApi\CloudApi\ClientInterface $client */
26
        $application = new Applications($client);
27
        $application->getAll();
28
    }
29
30
    public function testError404()
31
    {
32
        $response = $this->getPsr7JsonResponseForFixture('Endpoints/Error/error404.json');
33
        $client = $this->getMockClient($response);
34
        $this->expectException(ApiErrorException::class);
35
        $this->expectExceptionMessage(
36
            'The application you are trying to access does not exist, or you do not have permission to access it.'
37
        );
38
        /** @var \AcquiaCloudApi\CloudApi\ClientInterface $client */
39
        $application = new Applications($client);
40
        $application->getAll();
41
    }
42
43
    public function testMultipleErrors()
44
    {
45
        $response = $this->getPsr7JsonResponseForFixture('Endpoints/Error/multipleErrors.json');
46
        $client = $this->getMockClient($response);
47
        $this->expectException(ApiErrorException::class);
48
        $errorMessage = <<< EOM
49
The application you are trying to access does not exist, or you do not have permission to access it.
50
You do not have sufficient permissions to do this task.
51
52
EOM;
53
        $this->expectExceptionMessage($errorMessage);
54
55
        $application = new Applications($client);
56
        $application->getAll();
57
    }
58
59
    public function testErrorException()
60
    {
61
62
        $request = new Request('GET', '/test');
63
        $response = new Response(500);
64
        $exception = new BadResponseException('Internal Server Error', $request, $response);
65
66
        $connector = $this
67
            ->getMockBuilder('AcquiaCloudApi\Connector\Connector')
68
            ->disableOriginalConstructor()
69
            ->setMethods(['sendRequest'])
70
            ->getMock();
71
        
72
        $connector->method('sendRequest')->willThrowException($exception);
73
        $client = Client::factory($connector);
74
        $response = $client->makeRequest('GET', '/test');
75
76
        $this->assertEquals($response->getStatusCode(), 500);
77
        $this->assertEquals($response->getReasonPhrase(), 'Internal Server Error');
78
    }
79
80
    public function testApiErrorException()
81
    {
82
        $response = $this->getPsr7JsonResponseForFixture('Endpoints/Error/error403.json');
83
        $body = $response->getBody();
84
        $object = json_decode($body);
85
86
        $exception = new ApiErrorException($object);
87
        $errorMessage = <<< EOM
88
AcquiaCloudApi\Exception\ApiErrorException: [forbidden]: You do not have permission to view applications.\n
89
EOM;
90
        $this->assertEquals($exception->__toString(), $errorMessage);
91
        $this->assertEquals($object, $exception->getResponseBody());
92
    }
93
}
94