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

CloudApiTestCase::getMockClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 1
dl 0
loc 23
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace AcquiaCloudApi\Tests;
4
5
use AcquiaCloudApi\Connector\Client;
6
use GuzzleHttp\Psr7;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * Class CloudApiTestCase
11
 */
12
abstract class CloudApiTestCase extends TestCase
13
{
14
15
    /**
16
     * Returns a PSR7 Stream for a given fixture.
17
     *
18
     * @param  string     $fixture The fixture to create the stream for.
19
     * @return Psr7\Stream
20
     */
21
    protected function getPsr7StreamForFixture($fixture): Psr7\Stream
22
    {
23
        $path = sprintf('%s/Fixtures/%s', __DIR__, $fixture);
24
        $this->assertFileExists($path);
25
        $stream = Psr7\stream_for(file_get_contents($path));
0 ignored issues
show
Bug introduced by
The function stream_for was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

25
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for(file_get_contents($path));
Loading history...
26
        $this->assertInstanceOf(Psr7\Stream::class, $stream);
27
28
        return $stream;
29
    }
30
31
    /**
32
     * Returns a PSR7 Response (JSON) for a given fixture.
33
     *
34
     * @param  string        $fixture    The fixture to create the response for.
35
     * @param  integer       $statusCode A HTTP Status Code for the response.
36
     * @return Psr7\Response
37
     */
38
    protected function getPsr7JsonResponseForFixture($fixture, $statusCode = 200): Psr7\Response
39
    {
40
        $stream = $this->getPsr7StreamForFixture($fixture);
41
        $this->assertNotNull(json_decode($stream));
42
        $this->assertEquals(JSON_ERROR_NONE, json_last_error());
43
44
        return new Psr7\Response($statusCode, ['Content-Type' => 'application/json'], $stream);
45
    }
46
47
    /**
48
     * Returns a PSR7 Response (Gzip) for a given fixture.
49
     *
50
     * @param  string        $fixture    The fixture to create the response for.
51
     * @param  integer       $statusCode A HTTP Status Code for the response.
52
     * @return Psr7\Response
53
     */
54
    protected function getPsr7GzipResponseForFixture($fixture, $statusCode = 200): Psr7\Response
55
    {
56
        $stream = $this->getPsr7StreamForFixture($fixture);
57
        $this->assertEquals(JSON_ERROR_NONE, json_last_error());
58
59
        return new Psr7\Response($statusCode, ['Content-Type' => 'application/octet-stream'], $stream);
60
    }
61
62
    /**
63
     * Mock client class.
64
     *
65
     * @param  mixed  $response
66
     * @return Client
67
     */
68
    protected function getMockClient($response = '')
69
    {
70
        if ($response) {
71
            $connector = $this
72
                ->getMockBuilder('AcquiaCloudApi\Connector\Connector')
73
                ->disableOriginalConstructor()
74
                ->setMethods(['sendRequest'])
75
                ->getMock();
76
77
            $connector
78
                ->expects($this->atLeastOnce())
79
                ->method('sendRequest')
80
                ->willReturn($response);
81
        } else {
82
            $connector = $this
83
                ->getMockBuilder('AcquiaCloudApi\Connector\Connector')
84
                ->disableOriginalConstructor()
85
                ->getMock();
86
        }
87
88
        $client = Client::factory($connector);
89
90
        return $client;
91
    }
92
}
93