Completed
Push — develop ( 2c75a2...3f95e8 )
by Adam
08:12
created

AbstractTestCase::getMockResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 2
nop 2
1
<?php
2
3
namespace IBM\Watson\Common\Tests;
4
5
use Mockery as m;
6
use GuzzleHttp\Psr7\Response;
7
use Http\Client\HttpClient;
8
use IBM\Watson\Common\RequestBuilder;
9
use PHPUnit\Framework\TestCase;
10
use ReflectionObject;
11
12
abstract class AbstractTestCase extends TestCase
13
{
14
    /**
15
     * @var \Http\Client\HttpClient
16
     */
17
    protected $httpClient;
18
19
    /**
20
     * @var \IBM\Watson\Common\RequestBuilderInterface
21
     */
22
    protected $requestBuilder;
23
24
    public function setUp()
25
    {
26
        $this->httpClient = m::mock(HttpClient::class);
27
        $this->requestBuilder = m::mock(RequestBuilder::class);
28
    }
29
30
    /**
31
     * Get mock response from file.
32
     *
33
     * @param string  $path Location of the response file.
34
     * @param integer $code HTTP status code.
35
     *
36
     * @return \GuzzleHttp\Psr7\Response
37
     */
38
    public function getMockResponse($path, $code = 200)
39
    {
40
        $ref = new ReflectionObject($this);
41
        $dir = \dirname($ref->getFileName());
42
43
        if (!file_exists($dir . '/mock/'. $path) && file_exists($dir . '/../mock/' . $path)) {
44
            return new Response(
45
                $code,
46
                [
47
                    'Content-Type' => 'application/json'
48
                ],
49
                file_get_contents($dir . '/../mock/' . $path)
50
            );
51
        }
52
53
        return new Response($code, [], file_get_contents($dir . '/mock/' . $path));
54
    }
55
}
56