1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Alekseytupichenkov\GuzzleStub\Traits; |
6
|
|
|
|
7
|
|
|
use Alekseytupichenkov\GuzzleStub\Exception\GuzzleStubException; |
8
|
|
|
use Alekseytupichenkov\GuzzleStub\Handler\StubHandler; |
9
|
|
|
use Alekseytupichenkov\GuzzleStub\Model\Fixture; |
10
|
|
|
use GuzzleHttp\HandlerStack; |
11
|
|
|
use GuzzleHttp\Middleware; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
trait GuzzleClientTrait |
16
|
|
|
{ |
17
|
|
|
/** @var HandlerStack $handlerStack */ |
18
|
|
|
private $handlerStack; |
19
|
|
|
private $stubHandler; |
20
|
|
|
private $history = []; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
call_user_func_array([$this, 'parent::__construct'], func_get_args()); |
25
|
|
|
|
26
|
|
|
$this->handlerStack = $this->getConfig('handler'); |
27
|
|
|
$this->handlerStack->push(Middleware::history($this->history)); |
28
|
|
|
|
29
|
|
|
$this->stubHandler = new StubHandler(); |
30
|
|
|
$this->handlerStack->setHandler($this->stubHandler); |
31
|
|
|
|
32
|
|
|
$this->loadFixtures(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Append fixture. |
37
|
|
|
* |
38
|
|
|
* @param Fixture $fixture |
39
|
|
|
* |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
|
|
public function append(Fixture $fixture): self |
43
|
|
|
{ |
44
|
|
|
$this->stubHandler->append($fixture); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
abstract public function getConfig($option = null); |
50
|
|
|
|
51
|
|
|
abstract public function loadFixtures(); |
52
|
|
|
|
53
|
|
|
public function getHistoryCount(): int |
54
|
|
|
{ |
55
|
|
|
return count($this->history); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getHistoryList(): array |
59
|
|
|
{ |
60
|
|
|
return $this->history; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function hasHistory(int $index): bool |
64
|
|
|
{ |
65
|
|
|
return isset($this->history[$index]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws GuzzleStubException |
70
|
|
|
*/ |
71
|
|
|
public function getHistory(int $index): array |
72
|
|
|
{ |
73
|
|
|
if (!$this->hasHistory($index)) { |
74
|
|
|
throw GuzzleStubException::cantFoundHistoryWithIndex($index); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->history[$index]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
|
|
public function getLatestHistory(): array |
84
|
|
|
{ |
85
|
|
|
return $this->getHistory($this->getHistoryCount() - 1); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
public function getLatestHistoryRequest(): RequestInterface |
92
|
|
|
{ |
93
|
|
|
return $this->getLatestHistory()['request']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
public function getLatestHistoryResponse(): ResponseInterface |
100
|
|
|
{ |
101
|
|
|
return $this->getLatestHistory()['response']; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|