|
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
|
|
|
|