IntegrationTest.php
3.48 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
<?php
namespace GuzzleHttp\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Event\AbstractTransferEvent;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\EndEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Pool;
class IntegrationTest extends \PHPUnit_Framework_TestCase
{
/**
* @issue https://github.com/guzzle/guzzle/issues/867
*/
public function testDoesNotFailInEventSystemForNetworkError()
{
$c = new Client();
$r = $c->createRequest(
'GET',
Server::$url,
[
'timeout' => 1,
'connect_timeout' => 1,
'proxy' => 'http://127.0.0.1:123/foo'
]
);
$events = [];
$fn = function(AbstractTransferEvent $event) use (&$events) {
$events[] = [
get_class($event),
$event->hasResponse(),
$event->getResponse()
];
};
$pool = new Pool($c, [$r], [
'error' => $fn,
'end' => $fn
]);
$pool->wait();
$this->assertCount(2, $events);
$this->assertEquals('GuzzleHttp\Event\ErrorEvent', $events[0][0]);
$this->assertFalse($events[0][1]);
$this->assertNull($events[0][2]);
$this->assertEquals('GuzzleHttp\Event\EndEvent', $events[1][0]);
$this->assertFalse($events[1][1]);
$this->assertNull($events[1][2]);
}
/**
* @issue https://github.com/guzzle/guzzle/issues/866
*/
public function testProperyGetsTransferStats()
{
$transfer = [];
Server::enqueue([new Response(200)]);
$c = new Client();
$response = $c->get(Server::$url . '/foo', [
'events' => [
'end' => function (EndEvent $e) use (&$transfer) {
$transfer = $e->getTransferInfo();
}
]
]);
$this->assertEquals(Server::$url . '/foo', $response->getEffectiveUrl());
$this->assertNotEmpty($transfer);
$this->assertArrayHasKey('url', $transfer);
}
public function testNestedFutureResponsesAreResolvedWhenSending()
{
$c = new Client();
$total = 3;
Server::enqueue([
new Response(200),
new Response(201),
new Response(202)
]);
$c->getEmitter()->on(
'complete',
function (CompleteEvent $e) use (&$total) {
if (--$total) {
$e->retry();
}
}
);
$response = $c->get(Server::$url);
$this->assertEquals(202, $response->getStatusCode());
$this->assertEquals('GuzzleHttp\Message\Response', get_class($response));
}
public function testNestedFutureErrorsAreResolvedWhenSending()
{
$c = new Client();
$total = 3;
Server::enqueue([
new Response(500),
new Response(501),
new Response(502)
]);
$c->getEmitter()->on(
'error',
function (ErrorEvent $e) use (&$total) {
if (--$total) {
$e->retry();
}
}
);
try {
$c->get(Server::$url);
$this->fail('Did not throw!');
} catch (RequestException $e) {
$this->assertEquals(502, $e->getResponse()->getStatusCode());
}
}
}