Completed
Push — develop ( c8d2e9...9b1d71 )
by Adam
03:00
created

AbstractTestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 52
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A setUpUnauthorizedResponse() 0 6 1
A setUpNotFoundResponse() 0 6 1
A setupWatsonServiceError() 0 6 1
A getMockResponse() 0 17 3
1
<?php
2
3
namespace IBM\Watson\Common\tests\Api;
4
5
use GuzzleHttp\Psr7\Response;
6
use Http\Client\HttpClient;
7
use IBM\Watson\Common\Hydrator\HydratorInterface;
8
use IBM\Watson\Common\RequestBuilder;
9
use PHPUnit\Framework\TestCase;
10
use Mockery as m;
11
12
abstract class AbstractTestCase extends TestCase
13
{
14
    protected $httpClient;
15
    protected $hydrator;
16
    protected $requestBuilder;
17
18
    public function setUp()
19
    {
20
        $this->httpClient = m::mock(HttpClient::class);
21
        $this->hydrator = m::mock(HydratorInterface::class)->makePartial();
22
        $this->requestBuilder = new RequestBuilder();
23
    }
24
25
    public function setUpUnauthorizedResponse()
26
    {
27
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () {
28
            return new Response(401, [], '{"error":"Not Authorized"}');
29
        });
30
    }
31
32
    public function setUpNotFoundResponse()
33
    {
34
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () {
35
            return new Response(404, [], '{"error":"Not Found"}');
36
        });
37
    }
38
39
    public function setupWatsonServiceError()
40
    {
41
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () {
42
            return new Response(500, [], '{"error":"The service encountered an internal error"}');
43
        });
44
    }
45
46
    public function getMockResponse($path, $code = 200)
47
    {
48
        $ref = new \ReflectionObject($this);
49
        $dir = dirname($ref->getFileName());
50
51
        if (!file_exists($dir . '/mock/'. $path) && file_exists($dir . '/../mock/' . $path )) {
52
            return new Response(
53
                $code,
54
                [
55
                    'Content-Type' => 'application/json'
56
                ],
57
                file_get_contents($dir . '/../mock/' . $path)
58
            );
59
        }
60
61
        return new Response($code, [], file_get_contents($dir . '/mock/' . $path));
62
    }
63
}
64