RequestFsmTest.php
6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace GuzzleHttp\Tests;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Message\Response;
use GuzzleHttp\RequestFsm;
use GuzzleHttp\Ring\Future\CompletedFutureArray;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Transaction;
use GuzzleHttp\Client;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Event\EndEvent;
use GuzzleHttp\Message\FutureResponse;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Event\RequestEvents;
use React\Promise\Deferred;
class RequestFsmTest extends \PHPUnit_Framework_TestCase
{
private $mf;
public function setup()
{
$this->mf = new MessageFactory();
}
public function testEmitsBeforeEventInTransition()
{
$fsm = new RequestFsm(function () {
return new CompletedFutureArray(['status' => 200]);
}, $this->mf);
$t = new Transaction(new Client(), new Request('GET', 'http://foo.com'));
$c = false;
$t->request->getEmitter()->on('before', function (BeforeEvent $e) use (&$c) {
$c = true;
});
$fsm($t);
$this->assertTrue($c);
}
public function testEmitsCompleteEventInTransition()
{
$fsm = new RequestFsm(function () {
return new CompletedFutureArray(['status' => 200]);
}, $this->mf);
$t = new Transaction(new Client(), new Request('GET', 'http://foo.com'));
$t->response = new Response(200);
$t->state = 'complete';
$c = false;
$t->request->getEmitter()->on('complete', function (CompleteEvent $e) use (&$c) {
$c = true;
});
$fsm($t);
$this->assertTrue($c);
}
public function testDoesNotEmitCompleteForFuture()
{
$fsm = new RequestFsm(function () {
return new CompletedFutureArray(['status' => 200]);
}, $this->mf);
$t = new Transaction(new Client(), new Request('GET', 'http://foo.com'));
$deferred = new Deferred();
$t->response = new FutureResponse($deferred->promise());
$t->state = 'complete';
$c = false;
$t->request->getEmitter()->on('complete', function (CompleteEvent $e) use (&$c) {
$c = true;
});
$fsm($t);
$this->assertFalse($c);
}
public function testTransitionsThroughSuccessfulTransfer()
{
$client = new Client();
$client->getEmitter()->attach(new Mock([new Response(200)]));
$request = $client->createRequest('GET', 'http://ewfewwef.com');
$this->addListeners($request, $calls);
$client->send($request);
$this->assertEquals(['before', 'complete', 'end'], $calls);
}
public function testTransitionsThroughErrorsInBefore()
{
$fsm = new RequestFsm(function () {
return new CompletedFutureArray(['status' => 200]);
}, $this->mf);
$client = new Client();
$request = $client->createRequest('GET', 'http://ewfewwef.com');
$t = new Transaction($client, $request);
$calls = [];
$this->addListeners($t->request, $calls);
$t->request->getEmitter()->on('before', function (BeforeEvent $e) {
throw new \Exception('foo');
});
try {
$fsm($t);
$this->fail('did not throw');
} catch (RequestException $e) {
$this->assertContains('foo', $t->exception->getMessage());
$this->assertEquals(['before', 'error', 'end'], $calls);
}
}
public function testTransitionsThroughErrorsInComplete()
{
$client = new Client();
$client->getEmitter()->attach(new Mock([new Response(200)]));
$request = $client->createRequest('GET', 'http://ewfewwef.com');
$this->addListeners($request, $calls);
$request->getEmitter()->once('complete', function (CompleteEvent $e) {
throw new \Exception('foo');
});
try {
$client->send($request);
$this->fail('did not throw');
} catch (RequestException $e) {
$this->assertContains('foo', $e->getMessage());
$this->assertEquals(['before', 'complete', 'error', 'end'], $calls);
}
}
public function testTransitionsThroughErrorInterception()
{
$fsm = new RequestFsm(function () {
return new CompletedFutureArray(['status' => 404]);
}, $this->mf);
$client = new Client();
$request = $client->createRequest('GET', 'http://ewfewwef.com');
$t = new Transaction($client, $request);
$calls = [];
$this->addListeners($t->request, $calls);
$t->request->getEmitter()->on('error', function (ErrorEvent $e) {
$e->intercept(new Response(200));
});
$fsm($t);
$this->assertEquals(200, $t->response->getStatusCode());
$this->assertNull($t->exception);
$this->assertEquals(['before', 'complete', 'error', 'complete', 'end'], $calls);
}
private function addListeners(RequestInterface $request, &$calls)
{
$request->getEmitter()->on('before', function (BeforeEvent $e) use (&$calls) {
$calls[] = 'before';
}, RequestEvents::EARLY);
$request->getEmitter()->on('complete', function (CompleteEvent $e) use (&$calls) {
$calls[] = 'complete';
}, RequestEvents::EARLY);
$request->getEmitter()->on('error', function (ErrorEvent $e) use (&$calls) {
$calls[] = 'error';
}, RequestEvents::EARLY);
$request->getEmitter()->on('end', function (EndEvent $e) use (&$calls) {
$calls[] = 'end';
}, RequestEvents::EARLY);
}
/**
* @expectedException \GuzzleHttp\Exception\RequestException
* @expectedExceptionMessage Too many state transitions
*/
public function testDetectsInfiniteLoops()
{
$client = new Client([
'fsm' => $fsm = new RequestFsm(
function () {
return new CompletedFutureArray(['status' => 200]);
},
new MessageFactory(),
3
)
]);
$request = $client->createRequest('GET', 'http://foo.com:123');
$request->getEmitter()->on('before', function () {
throw new \Exception('foo');
});
$request->getEmitter()->on('error', function ($e) {
$e->retry();
});
$client->send($request);
}
}