MockTest.php
6.19 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
188
189
190
191
192
<?php
namespace GuzzleHttp\Tests\Subscriber;
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Message\FutureResponse;
use GuzzleHttp\Transaction;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use React\Promise\Deferred;
/**
* @covers GuzzleHttp\Subscriber\Mock
*/
class MockTest extends \PHPUnit_Framework_TestCase
{
public static function createFuture(
callable $wait,
callable $cancel = null
) {
$deferred = new Deferred();
return new FutureResponse(
$deferred->promise(),
function () use ($deferred, $wait) {
$deferred->resolve($wait());
},
$cancel
);
}
public function testDescribesSubscribedEvents()
{
$mock = new Mock();
$this->assertInternalType('array', $mock->getEvents());
}
public function testIsCountable()
{
$plugin = new Mock();
$plugin->addResponse((new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
$this->assertEquals(1, count($plugin));
}
public function testCanClearQueue()
{
$plugin = new Mock();
$plugin->addResponse((new MessageFactory())->fromMessage("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
$plugin->clearQueue();
$this->assertEquals(0, count($plugin));
}
public function testRetrievesResponsesFromFiles()
{
$tmp = tempnam('/tmp', 'tfile');
file_put_contents($tmp, "HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n");
$plugin = new Mock();
$plugin->addResponse($tmp);
unlink($tmp);
$this->assertEquals(1, count($plugin));
$q = $this->readAttribute($plugin, 'queue');
$this->assertEquals(201, $q[0]->getStatusCode());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionWhenInvalidResponse()
{
(new Mock())->addResponse(false);
}
public function testAddsMockResponseToRequestFromClient()
{
$response = new Response(200);
$t = new Transaction(new Client(), new Request('GET', '/'));
$m = new Mock([$response]);
$ev = new BeforeEvent($t);
$m->onBefore($ev);
$this->assertSame($response, $t->response);
}
/**
* @expectedException \OutOfBoundsException
*/
public function testUpdateThrowsExceptionWhenEmpty()
{
$p = new Mock();
$ev = new BeforeEvent(new Transaction(new Client(), new Request('GET', '/')));
$p->onBefore($ev);
}
public function testReadsBodiesFromMockedRequests()
{
$m = new Mock([new Response(200)]);
$client = new Client(['base_url' => 'http://test.com']);
$client->getEmitter()->attach($m);
$body = Stream::factory('foo');
$client->put('/', ['body' => $body]);
$this->assertEquals(3, $body->tell());
}
public function testCanMockBadRequestExceptions()
{
$client = new Client(['base_url' => 'http://test.com']);
$request = $client->createRequest('GET', '/');
$ex = new RequestException('foo', $request);
$mock = new Mock([$ex]);
$this->assertCount(1, $mock);
$request->getEmitter()->attach($mock);
try {
$client->send($request);
$this->fail('Did not dequeue an exception');
} catch (RequestException $e) {
$this->assertSame($e, $ex);
$this->assertSame($request, $ex->getRequest());
}
}
public function testCanMockFutureResponses()
{
$client = new Client(['base_url' => 'http://test.com']);
$request = $client->createRequest('GET', '/', ['future' => true]);
$response = new Response(200);
$future = self::createFuture(function () use ($response) {
return $response;
});
$mock = new Mock([$future]);
$this->assertCount(1, $mock);
$request->getEmitter()->attach($mock);
$res = $client->send($request);
$this->assertSame($future, $res);
$this->assertFalse($this->readAttribute($res, 'isRealized'));
$this->assertSame($response, $res->wait());
}
public function testCanMockExceptionFutureResponses()
{
$client = new Client(['base_url' => 'http://test.com']);
$request = $client->createRequest('GET', '/', ['future' => true]);
$future = self::createFuture(function () use ($request) {
throw new RequestException('foo', $request);
});
$mock = new Mock([$future]);
$request->getEmitter()->attach($mock);
$response = $client->send($request);
$this->assertSame($future, $response);
$this->assertFalse($this->readAttribute($response, 'isRealized'));
try {
$response->wait();
$this->fail('Did not throw');
} catch (RequestException $e) {
$this->assertContains('foo', $e->getMessage());
}
}
public function testCanMockFailedFutureResponses()
{
$client = new Client(['base_url' => 'http://test.com']);
$request = $client->createRequest('GET', '/', ['future' => true]);
// The first mock will be a mocked future response.
$future = self::createFuture(function () use ($client) {
// When dereferenced, we will set a mocked response and send
// another request.
$client->get('http://httpbin.org', ['events' => [
'before' => function (BeforeEvent $e) {
$e->intercept(new Response(404));
}
]]);
});
$mock = new Mock([$future]);
$request->getEmitter()->attach($mock);
$response = $client->send($request);
$this->assertSame($future, $response);
$this->assertFalse($this->readAttribute($response, 'isRealized'));
try {
$response->wait();
$this->fail('Did not throw');
} catch (RequestException $e) {
$this->assertEquals(404, $e->getResponse()->getStatusCode());
}
}
}