|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\MakeSure; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class HttpClient. |
|
7
|
|
|
* |
|
8
|
|
|
* @method IsRespondedWith sendingPostRequest($uri, array $data = [], array $headers = []) |
|
9
|
|
|
* @method IsRespondedWith sendingJsonPostRequest($uri, array $data = [], array $headers = []) |
|
10
|
|
|
* @method IsRespondedWith sendingDeleteRequest($uri, array $data = [], array $headers = []) |
|
11
|
|
|
* @method IsRespondedWith sendingJsonDeleteRequest($uri, array $data = [], array $headers = []) |
|
12
|
|
|
* @method IsRespondedWith sendingPutRequest($uri, array $data = [], array $headers = []) |
|
13
|
|
|
* @method IsRespondedWith sendingJsonPutRequest($uri, array $data = [], array $headers = []) |
|
14
|
|
|
* @method IsRespondedWith sendingPatchRequest($uri, array $data = [], array $headers = []) |
|
15
|
|
|
* @method IsRespondedWith sendingJsonPatchRequest($uri, array $data = [], array $headers = []) |
|
16
|
|
|
* @method IsRespondedWith sendingGetRequest($uri, array $headers = []) |
|
17
|
|
|
* @method IsRespondedWith sendingJsonGetRequest($uri, array $headers = []) |
|
18
|
|
|
*/ |
|
19
|
|
|
class HttpClient |
|
20
|
|
|
{ |
|
21
|
|
|
private $chain; |
|
22
|
|
|
|
|
23
|
|
|
private $methods = [ |
|
24
|
|
|
'sendingPostRequest' => 'post', |
|
25
|
|
|
'sendingJsonPostRequest' => 'postJson', |
|
26
|
|
|
'sendingDeleteRequest' => 'delete', |
|
27
|
|
|
'sendingJsonDeleteRequest' => 'deleteJson', |
|
28
|
|
|
'sendingPutRequest' => 'put', |
|
29
|
|
|
'sendingJsonPutRequest' => 'putJson', |
|
30
|
|
|
'sendingPatchRequest' => 'patch', |
|
31
|
|
|
'sendingJsonPatchRequest' => 'patchJson', |
|
32
|
|
|
'sendingGetRequest' => 'get', |
|
33
|
|
|
'sendingJsonGetRequest' => 'getJson', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* HttpClient constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param $phpunit |
|
40
|
|
|
*/ |
|
41
|
14 |
|
public function __construct($phpunit) |
|
42
|
|
|
{ |
|
43
|
14 |
|
$this->chain = new Chain($phpunit); |
|
44
|
14 |
|
} |
|
45
|
|
|
|
|
46
|
12 |
|
public function __call($method, $params): IsRespondedWith |
|
47
|
|
|
{ |
|
48
|
12 |
|
return $this->sendRequest($method, ...$params); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
12 |
|
private function sendRequest($method, ...$data): IsRespondedWith |
|
52
|
|
|
{ |
|
53
|
12 |
|
$this->chain->data['http'] = [$this->methods[$method], $data]; |
|
54
|
|
|
|
|
55
|
12 |
|
return new IsRespondedWith($this->chain); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function exceptionIsThrown($type) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->chain->data['exception'] = $type; |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function whenEventHappens($event): self |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->chain->data['event'] = $event; |
|
66
|
|
|
|
|
67
|
1 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|