scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests\Event;
4  
5 use GuzzleHttp\Adapter\Transaction;
6 use GuzzleHttp\Client;
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;
14  
15 /**
16 * @covers GuzzleHttp\Event\RequestEvents
17 */
18 class RequestEventsTest extends \PHPUnit_Framework_TestCase
19 {
20 public function testEmitsAfterSendEvent()
21 {
22 $res = null;
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) {
26 $res = $e;
27 });
28 RequestEvents::emitComplete($t);
29 $this->assertSame($res->getClient(), $t->getClient());
30 $this->assertSame($res->getRequest(), $t->getRequest());
31 $this->assertEquals('/', $t->getResponse()->getEffectiveUrl());
32 }
33  
34 public function testEmitsAfterSendEventAndEmitsErrorIfNeeded()
35 {
36 $ex2 = $res = null;
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) {
42 $ex->e = $e;
43 throw $ex;
44 });
45 $t->getRequest()->getEmitter()->on('error', function ($e) use (&$ex2) {
46 $ex2 = $e->getException();
47 $e->stopPropagation();
48 });
49 RequestEvents::emitComplete($t);
50 $this->assertSame($ex, $ex2);
51 }
52  
53 public function testBeforeSendEmitsErrorEvent()
54 {
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;
61  
62 $request->getEmitter()->on(
63 'before',
64 function (BeforeEvent $e) use ($request, $client, &$beforeCalled, $ex) {
65 $this->assertSame($request, $e->getRequest());
66 $this->assertSame($client, $e->getClient());
67 $beforeCalled++;
68 throw $ex;
69 }
70 );
71  
72 $request->getEmitter()->on(
73 'error',
74 function (ErrorEvent $e) use (&$errCalled, $response, $ex) {
75 $errCalled++;
76 $this->assertInstanceOf('GuzzleHttp\Exception\RequestException', $e->getException());
77 $this->assertSame($ex, $e->getException()->getPrevious());
78 $e->intercept($response);
79 }
80 );
81  
82 RequestEvents::emitBefore($t);
83 $this->assertEquals(1, $beforeCalled);
84 $this->assertEquals(1, $errCalled);
85 $this->assertSame($response, $t->getResponse());
86 }
87  
88 public function testThrowsUnInterceptedErrors()
89 {
90 $ex = new \Exception('Foo');
91 $client = new Client();
92 $request = new Request('GET', '/');
93 $t = new Transaction($client, $request);
94 $errCalled = 0;
95  
96 $request->getEmitter()->on('before', function (BeforeEvent $e) use ($ex) {
97 throw $ex;
98 });
99  
100 $request->getEmitter()->on('error', function (ErrorEvent $e) use (&$errCalled) {
101 $errCalled++;
102 });
103  
104 try {
105 RequestEvents::emitBefore($t);
106 $this->fail('Did not throw');
107 } catch (RequestException $e) {
108 $this->assertEquals(1, $errCalled);
109 }
110 }
111  
112 public function testDoesNotEmitErrorEventTwice()
113 {
114 $client = new Client();
115 $mock = new Mock([new Response(500)]);
116 $client->getEmitter()->attach($mock);
117  
118 $r = [];
119 $client->getEmitter()->on('error', function (ErrorEvent $event) use (&$r) {
120 $r[] = $event->getRequest();
121 });
122  
123 try {
124 $client->get('http://foo.com');
125 $this->fail('Did not throw');
126 } catch (RequestException $e) {
127 $this->assertCount(1, $r);
128 }
129 }
130  
131 /**
132 * Note: Longest test name ever.
133 */
134 public function testEmitsErrorEventForRequestExceptionsThrownDuringBeforeThatHaveNotEmittedAnErrorEvent()
135 {
136 $request = new Request('GET', '/');
137 $ex = new RequestException('foo', $request);
138  
139 $client = new Client();
140 $client->getEmitter()->on('before', function (BeforeEvent $event) use ($ex) {
141 throw $ex;
142 });
143 $called = false;
144 $client->getEmitter()->on('error', function (ErrorEvent $event) use ($ex, &$called) {
145 $called = true;
146 $this->assertSame($ex, $event->getException());
147 });
148  
149 try {
150 $client->get('http://foo.com');
151 $this->fail('Did not throw');
152 } catch (RequestException $e) {
153 $this->assertTrue($called);
154 }
155 }
156  
157 public function prepareEventProvider()
158 {
159 $cb = function () {};
160  
161 return [
162 [[], ['complete'], $cb, ['complete' => [$cb]]],
163 [
164 ['complete' => $cb],
165 ['complete'],
166 $cb,
167 ['complete' => [$cb, $cb]]
168 ],
169 [
170 ['prepare' => []],
171 ['error', 'foo'],
172 $cb,
173 [
174 'prepare' => [],
175 'error' => [$cb],
176 'foo' => [$cb]
177 ]
178 ],
179 [
180 ['prepare' => []],
181 ['prepare'],
182 $cb,
183 [
184 'prepare' => [$cb]
185 ]
186 ],
187 [
188 ['prepare' => ['fn' => $cb]],
189 ['prepare'], $cb,
190 [
191 'prepare' => [
192 ['fn' => $cb],
193 $cb
194 ]
195 ]
196 ],
197 ];
198 }
199  
200 /**
201 * @dataProvider prepareEventProvider
202 */
203 public function testConvertsEventArrays(
204 array $in,
205 array $events,
206 $add,
207 array $out
208 ) {
209 $result = RequestEvents::convertEventArray($in, $events, $add);
210 $this->assertEquals($out, $result);
211 }
212 }