1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
use GuzzleHttp\Handler\MockHandler; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use GuzzleHttp\Psr7\Response; |
11
|
|
|
use Psr\Http\Message\RequestInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class MockTrait |
16
|
|
|
* |
17
|
|
|
* @package AlibabaCloud\Client\Request\Traits |
18
|
|
|
* @mixin Request |
19
|
|
|
*/ |
20
|
|
|
trait MockTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $mockQueue = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var HandlerStack |
29
|
|
|
*/ |
30
|
|
|
private static $mock; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param integer $status |
34
|
|
|
* @param array $headers |
35
|
|
|
* @param array|string|object $body |
36
|
|
|
*/ |
37
|
17 |
|
public static function mockResponse($status = 200, array $headers = [], $body = null) |
38
|
|
|
{ |
39
|
17 |
|
if (is_array($body) || is_object($body)) { |
40
|
6 |
|
$body = json_encode($body); |
41
|
6 |
|
} |
42
|
|
|
|
43
|
17 |
|
self::$mockQueue[] = new Response($status, $headers, $body); |
44
|
17 |
|
self::createHandlerStack(); |
45
|
17 |
|
} |
46
|
|
|
|
47
|
18 |
|
private static function createHandlerStack() |
48
|
|
|
{ |
49
|
18 |
|
self::$mock = new MockHandler(self::$mockQueue); |
|
|
|
|
50
|
18 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $message |
54
|
|
|
* @param RequestInterface $request |
55
|
|
|
* @param ResponseInterface|null $response |
56
|
|
|
* @param Exception|null $previous |
57
|
|
|
* @param array $handlerContext |
58
|
|
|
*/ |
59
|
1 |
|
public static function mockRequestException( |
60
|
|
|
$message, |
61
|
|
|
RequestInterface $request, |
62
|
|
|
ResponseInterface $response = null, |
63
|
|
|
Exception $previous = null, |
64
|
|
|
array $handlerContext = [] |
65
|
|
|
) { |
66
|
1 |
|
self::$mockQueue[] = new RequestException( |
67
|
1 |
|
$message, |
68
|
1 |
|
$request, |
69
|
1 |
|
$response, |
70
|
1 |
|
$previous, |
71
|
|
|
$handlerContext |
72
|
1 |
|
); |
73
|
|
|
|
74
|
1 |
|
self::createHandlerStack(); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
59 |
|
public static function cancelMock() |
78
|
|
|
{ |
79
|
59 |
|
self::$mockQueue = []; |
80
|
59 |
|
self::$mock = null; |
81
|
59 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
66 |
|
public static function hasMock() |
87
|
|
|
{ |
88
|
66 |
|
return (bool)self::$mockQueue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return HandlerStack |
93
|
|
|
*/ |
94
|
16 |
|
public static function getMock() |
95
|
|
|
{ |
96
|
16 |
|
return self::$mock; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..