scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Subscriber; |
||
4 | |||
5 | use GuzzleHttp\Adapter\Transaction; |
||
6 | use GuzzleHttp\Event\BeforeEvent; |
||
7 | use GuzzleHttp\Event\HeadersEvent; |
||
8 | use GuzzleHttp\Event\RequestEvents; |
||
9 | use GuzzleHttp\Event\SubscriberInterface; |
||
10 | use GuzzleHttp\Exception\RequestException; |
||
11 | use GuzzleHttp\Message\MessageFactory; |
||
12 | use GuzzleHttp\Message\ResponseInterface; |
||
13 | |||
14 | /** |
||
15 | * Queues mock responses or exceptions and delivers mock responses or |
||
16 | * exceptions in a fifo order. |
||
17 | */ |
||
18 | class Mock implements SubscriberInterface, \Countable |
||
19 | { |
||
20 | /** @var array Array of mock responses / exceptions */ |
||
21 | private $queue = []; |
||
22 | |||
23 | /** @var bool Whether or not to consume an entity body when mocking */ |
||
24 | private $readBodies; |
||
25 | |||
26 | /** @var MessageFactory */ |
||
27 | private $factory; |
||
28 | |||
29 | /** |
||
30 | * @param array $items Array of responses or exceptions to queue |
||
31 | * @param bool $readBodies Set to false to not consume the entity body of |
||
32 | * a request when a mock is served. |
||
33 | */ |
||
34 | public function __construct(array $items = [], $readBodies = true) |
||
35 | { |
||
36 | $this->factory = new MessageFactory(); |
||
37 | $this->readBodies = $readBodies; |
||
38 | $this->addMultiple($items); |
||
39 | } |
||
40 | |||
41 | public function getEvents() |
||
42 | { |
||
43 | // Fire the event last, after signing |
||
44 | return ['before' => ['onBefore', RequestEvents::SIGN_REQUEST - 10]]; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @throws \OutOfBoundsException|\Exception |
||
49 | */ |
||
50 | public function onBefore(BeforeEvent $event) |
||
51 | { |
||
52 | if (!$item = array_shift($this->queue)) { |
||
53 | throw new \OutOfBoundsException('Mock queue is empty'); |
||
54 | } elseif ($item instanceof RequestException) { |
||
55 | throw $item; |
||
56 | } |
||
57 | |||
58 | // Emulate the receiving of the response headers |
||
59 | $request = $event->getRequest(); |
||
60 | $transaction = new Transaction($event->getClient(), $request); |
||
61 | $transaction->setResponse($item); |
||
62 | $request->getEmitter()->emit( |
||
63 | 'headers', |
||
64 | new HeadersEvent($transaction) |
||
65 | ); |
||
66 | |||
67 | // Emulate reading a response body |
||
68 | if ($this->readBodies && $request->getBody()) { |
||
69 | while (!$request->getBody()->eof()) { |
||
70 | $request->getBody()->read(8096); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | $event->intercept($item); |
||
75 | } |
||
76 | |||
77 | public function count() |
||
78 | { |
||
79 | return count($this->queue); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Add a response to the end of the queue |
||
84 | * |
||
85 | * @param string|ResponseInterface $response Response or path to response file |
||
86 | * |
||
87 | * @return self |
||
88 | * @throws \InvalidArgumentException if a string or Response is not passed |
||
89 | */ |
||
90 | public function addResponse($response) |
||
91 | { |
||
92 | if (is_string($response)) { |
||
93 | $response = file_exists($response) |
||
94 | ? $this->factory->fromMessage(file_get_contents($response)) |
||
95 | : $this->factory->fromMessage($response); |
||
96 | } elseif (!($response instanceof ResponseInterface)) { |
||
97 | throw new \InvalidArgumentException('Response must a message ' |
||
98 | . 'string, response object, or path to a file'); |
||
99 | } |
||
100 | |||
101 | $this->queue[] = $response; |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Add an exception to the end of the queue |
||
108 | * |
||
109 | * @param RequestException $e Exception to throw when the request is executed |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | public function addException(RequestException $e) |
||
114 | { |
||
115 | $this->queue[] = $e; |
||
116 | |||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Add multiple items to the queue |
||
122 | * |
||
123 | * @param array $items Items to add |
||
124 | */ |
||
125 | public function addMultiple(array $items) |
||
126 | { |
||
127 | foreach ($items as $item) { |
||
128 | if ($item instanceof RequestException) { |
||
129 | $this->addException($item); |
||
130 | } else { |
||
131 | $this->addResponse($item); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Clear the queue |
||
138 | */ |
||
139 | public function clearQueue() |
||
140 | { |
||
141 | $this->queue = []; |
||
142 | } |
||
143 | } |