/** * @throws \OutOfBoundsException|\Exception */ public function onBefore(BeforeEvent $event) { if (!($item = array_shift($this->queue))) { throw new \OutOfBoundsException('Mock queue is empty'); } elseif ($item instanceof RequestException) { throw $item; } // Emulate reading a response body $request = $event->getRequest(); if ($this->readBodies && $request->getBody()) { while (!$request->getBody()->eof()) { $request->getBody()->read(8096); } } $saveTo = $event->getRequest()->getConfig()->get('save_to'); if (null !== $saveTo) { $body = $item->getBody(); if (is_resource($saveTo)) { fwrite($saveTo, $body); } elseif (is_string($saveTo)) { file_put_contents($saveTo, $body); } elseif ($saveTo instanceof StreamInterface) { $saveTo->write($body); } } $event->intercept($item); }
public function testInterceptsWithEvent() { $t = new Transaction(new Client(), new Request('GET', '/')); $t->exception = new \Exception('foo'); $e = new BeforeEvent($t); $response = new Response(200); $e->intercept($response); $this->assertTrue($e->isPropagationStopped()); $this->assertSame($t->response, $response); $this->assertNull($t->exception); }