3
namespace GuzzleHttp\Tests\Event;
5
use GuzzleHttp\Adapter\Transaction;
7
use GuzzleHttp\Event\BeforeEvent;
8
use GuzzleHttp\Event\ErrorEvent;
9
use GuzzleHttp\Event\RequestEvents;
10
use GuzzleHttp\Exception\RequestException;
11
use GuzzleHttp\Message\Request;
12
use GuzzleHttp\Message\Response;
13
use GuzzleHttp\Subscriber\Mock;
16
* @covers GuzzleHttp\Event\RequestEvents
18
class RequestEventsTest extends \PHPUnit_Framework_TestCase
20
public function testEmitsAfterSendEvent()
23
$t = new Transaction(new Client(), new Request('GET', '/'));
24
$t->setResponse(new Response(200));
25
$t->getRequest()->getEmitter()->on('complete', function ($e) use (&$res) {
28
RequestEvents::emitComplete($t);
29
$this->assertSame($res->getClient(), $t->getClient());
30
$this->assertSame($res->getRequest(), $t->getRequest());
31
$this->assertEquals('/', $t->getResponse()->getEffectiveUrl());
34
public function testEmitsAfterSendEventAndEmitsErrorIfNeeded()
37
$request = new Request('GET', '/');
38
$t = new Transaction(new Client(), $request);
39
$t->setResponse(new Response(200));
40
$ex = new RequestException('foo', $request);
41
$t->getRequest()->getEmitter()->on('complete', function ($e) use ($ex) {
45
$t->getRequest()->getEmitter()->on('error', function ($e) use (&$ex2) {
46
$ex2 = $e->getException();
47
$e->stopPropagation();
49
RequestEvents::emitComplete($t);
50
$this->assertSame($ex, $ex2);
53
public function testBeforeSendEmitsErrorEvent()
55
$ex = new \Exception('Foo');
56
$client = new Client();
57
$request = new Request('GET', '/');
58
$response = new Response(200);
59
$t = new Transaction($client, $request);
60
$beforeCalled = $errCalled = 0;
62
$request->getEmitter()->on(
64
function (BeforeEvent $e) use ($request, $client, &$beforeCalled, $ex) {
65
$this->assertSame($request, $e->getRequest());
66
$this->assertSame($client, $e->getClient());
72
$request->getEmitter()->on(
74
function (ErrorEvent $e) use (&$errCalled, $response, $ex) {
76
$this->assertInstanceOf('GuzzleHttp\Exception\RequestException', $e->getException());
77
$this->assertSame($ex, $e->getException()->getPrevious());
78
$e->intercept($response);
82
RequestEvents::emitBefore($t);
83
$this->assertEquals(1, $beforeCalled);
84
$this->assertEquals(1, $errCalled);
85
$this->assertSame($response, $t->getResponse());
88
public function testThrowsUnInterceptedErrors()
90
$ex = new \Exception('Foo');
91
$client = new Client();
92
$request = new Request('GET', '/');
93
$t = new Transaction($client, $request);
96
$request->getEmitter()->on('before', function (BeforeEvent $e) use ($ex) {
100
$request->getEmitter()->on('error', function (ErrorEvent $e) use (&$errCalled) {
105
RequestEvents::emitBefore($t);
106
$this->fail('Did not throw');
107
} catch (RequestException $e) {
108
$this->assertEquals(1, $errCalled);
112
public function testDoesNotEmitErrorEventTwice()
114
$client = new Client();
115
$mock = new Mock([new Response(500)]);
116
$client->getEmitter()->attach($mock);
119
$client->getEmitter()->on('error', function (ErrorEvent $event) use (&$r) {
120
$r[] = $event->getRequest();
124
$client->get('http://foo.com');
125
$this->fail('Did not throw');
126
} catch (RequestException $e) {
127
$this->assertCount(1, $r);
132
* Note: Longest test name ever.
134
public function testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent()
136
$request = new Request('GET', '/');
137
$ex = new RequestException('foo', $request);
139
$client = new Client();
140
$client->getEmitter()->on('before', function (BeforeEvent $event) use ($ex) {
144
$client->getEmitter()->on('error', function (ErrorEvent $event) use ($ex, &$called) {
146
$this->assertSame($ex, $event->getException());
150
$client->get('http://foo.com');
151
$this->fail('Did not throw');
152
} catch (RequestException $e) {
153
$this->assertTrue($called);
157
public function prepareEventProvider()
159
$cb = function () {};
162
[[], ['complete'], $cb, ['complete' => [$cb]]],
167
['complete' => [$cb, $cb]]
188
['prepare' => ['fn' => $cb]],
201
* @dataProvider prepareEventProvider
203
public function testConvertsEventArrays(
209
$result = RequestEvents::convertEventArray($in, $events, $add);
210
$this->assertEquals($out, $result);