Completed
Push — develop ( 2d2214...2a74ad )
by Adam
02:43
created

AbstractTestCase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 13
dl 0
loc 42
rs 10
c 0
b 0
f 0
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 int    $code HTTP status code.
35
     *
36
     * @return \GuzzleHttp\Psr7\Response
37
     */
38
    public function getMockResponse($path, $code = 200): Response
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