|
4 | 4 |
|
5 | 5 | use GuzzleHttp\ClientInterface; |
6 | 6 | use GuzzleHttp\Exception\ClientException; |
| 7 | +use GuzzleHttp\Psr7\Request; |
| 8 | +use GuzzleHttp\Psr7\Response; |
| 9 | +use GuzzleHttp\Psr7\Utils; |
7 | 10 | use PHPUnit\Framework\TestCase; |
8 | 11 | use Psr\Http\Message\ResponseInterface; |
9 | 12 |
|
10 | 13 | abstract class NetAcuityTestSuite extends TestCase |
11 | 14 | { |
12 | 15 | protected function getMockGuzzleClient(): ClientInterface |
13 | 16 | { |
14 | | - return $this->getMockBuilder( |
15 | | - '\GuzzleHttp\Client' |
16 | | - )->disableOriginalConstructor()->setMethods(['send'])->getMock(); |
| 17 | + return $this->getMockBuilder(ClientInterface::class)->getMock(); |
17 | 18 | } |
18 | 19 |
|
19 | 20 | protected function getMockClientException(int $code, string $errorMessage): ClientException |
20 | 21 | { |
21 | | - $mockStream = $this->getMockBuilder( |
22 | | - '\GuzzleHttp\Psr7\Stream' |
23 | | - )->disableOriginalConstructor()->setMethods(['getContents'])->getMock(); |
24 | | - $mockStream->method( |
25 | | - 'getContents' |
26 | | - )->willReturn(json_encode(['error' => ['message' => $errorMessage]])); |
27 | | - |
28 | | - $mockResponse = $this->getMockBuilder( |
29 | | - '\GuzzleHttp\Psr7\Response' |
30 | | - )->disableOriginalConstructor()->setMethods(['getStatusCode', 'getBody'])->getMock(); |
31 | | - $mockResponse->method('getStatusCode')->willReturn($code); |
32 | | - $mockResponse->method('getBody')->willReturn($mockStream); |
33 | | - |
34 | | - $mockException = $this->getMockBuilder( |
35 | | - '\GuzzleHttp\Exception\ClientException' |
36 | | - )->disableOriginalConstructor()->setMethods(['getResponse'])->getMock(); |
37 | | - $mockException->method('getResponse')->willReturn($mockResponse); |
38 | | - |
39 | | - return $mockException; |
| 22 | + $stream = Utils::streamFor(json_encode(['error' => ['message' => $errorMessage]])); |
| 23 | + $response = new Response($code, [], $stream); |
| 24 | + return new ClientException($errorMessage, new Request('GET', 'localhost'), $response); |
40 | 25 | } |
41 | 26 |
|
42 | 27 | protected function getMockResponse(array $response): ResponseInterface |
43 | 28 | { |
44 | | - $mockStream = $this->getMockBuilder( |
45 | | - '\GuzzleHttp\Psr7\Stream' |
46 | | - )->disableOriginalConstructor()->setMethods(['getContents'])->getMock(); |
47 | | - $mockStream->method('getContents')->willReturn(json_encode(['response' => $response], true)); |
48 | | - |
49 | | - $mockResponse = $this->getMockBuilder( |
50 | | - '\GuzzleHttp\Psr7\Response' |
51 | | - )->disableOriginalConstructor()->setMethods(['getBody'])->getMock(); |
52 | | - $mockResponse->method('getBody')->willReturn($mockStream); |
53 | | - |
54 | | - return $mockResponse; |
| 29 | + return new Response(200, [], Utils::streamFor(json_encode(['response' => $response]))); |
55 | 30 | } |
56 | 31 | } |
0 commit comments