1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\NetAcuity\Tests; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
abstract class NetAcuityTestSuite extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Client |
15
|
|
|
*/ |
16
|
|
|
protected function getMockGuzzleClient() : \PHPUnit_Framework_MockObject_MockObject |
17
|
|
|
{ |
18
|
|
|
return $this->getMockBuilder( |
19
|
|
|
'\GuzzleHttp\Client' |
20
|
|
|
)->disableOriginalConstructor()->setMethods(['send'])->getMock(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function getMockClientException(int $code, string $errorMessage) : ClientException |
24
|
|
|
{ |
25
|
|
|
$mockStream =$this->getMockBuilder( |
26
|
|
|
'\GuzzleHttp\Psr7\Stream' |
27
|
|
|
)->disableOriginalConstructor()->setMethods(['getContents'])->getMock(); |
28
|
|
|
$mockStream->method( |
29
|
|
|
'getContents' |
30
|
|
|
)->willReturn(json_encode(['error' => ['message' => $errorMessage]])); |
31
|
|
|
|
32
|
|
|
$mockResponse = $this->getMockBuilder( |
33
|
|
|
'\GuzzleHttp\Psr7\Response' |
34
|
|
|
)->disableOriginalConstructor()->setMethods(['getStatusCode', 'getBody'])->getMock(); |
35
|
|
|
$mockResponse->method('getStatusCode')->willReturn($code); |
36
|
|
|
$mockResponse->method('getBody')->willReturn($mockStream); |
37
|
|
|
|
38
|
|
|
$mockException = $this->getMockBuilder( |
39
|
|
|
'\GuzzleHttp\Exception\ClientException' |
40
|
|
|
)->disableOriginalConstructor()->setMethods(['getResponse'])->getMock(); |
41
|
|
|
$mockException->method('getResponse')->willReturn($mockResponse); |
42
|
|
|
|
43
|
|
|
return $mockException; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function getMockResponse(array $response) : ResponseInterface |
47
|
|
|
{ |
48
|
|
|
$mockStream = $this->getMockBuilder( |
49
|
|
|
'\GuzzleHttp\Psr7\Stream' |
50
|
|
|
)->disableOriginalConstructor()->setMethods(['getContents'])->getMock(); |
51
|
|
|
$mockStream->method('getContents')->willReturn(json_encode(['response' => $response], true)); |
52
|
|
|
|
53
|
|
|
$mockResponse = $this->getMockBuilder( |
54
|
|
|
'\GuzzleHttp\Psr7\Response' |
55
|
|
|
)->disableOriginalConstructor()->setMethods(['getBody'])->getMock(); |
56
|
|
|
$mockResponse->method('getBody')->willReturn($mockStream); |
57
|
|
|
|
58
|
|
|
return $mockResponse; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|