Skip to content

Commit a433905

Browse files
committed
Merge pull request #10 from joelwurtz/feature/async-test
Add test for async client
2 parents 5083455 + 3523ea5 commit a433905

File tree

6 files changed

+322
-122
lines changed

6 files changed

+322
-122
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
"phpunit/phpunit": "^4.4",
1616
"php-http/discovery": "~0.2@dev",
1717
"php-http/httplug": "~1.0@dev",
18+
"php-http/httplug-async": "~0.1@dev",
1819
"guzzlehttp/psr7": "^1.0",
1920
"th3n3rd/cartesian-product": "^0.3"
2021
},
2122
"autoload": {
2223
"psr-4": {
23-
"Http\\Adapter\\Tests\\": "src/"
24+
"Http\\Client\\Tests\\": "src/"
2425
}
2526
},
2627
"bin": [

fixture/server.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
require_once __DIR__.'/../src/PHPUnitUtility.php';
1313

14-
use Http\Adapter\Tests\PHPUnitUtility;
14+
use Http\Client\Tests\PHPUnitUtility;
1515

1616
$file = fopen(PHPUnitUtility::getFile(true, 'php-http-adapter.log'), 'c');
1717
flock($file, LOCK_EX);

src/HttpAsyncClientTest.php

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
namespace Http\Client\Tests;
4+
5+
use Http\Client\HttpAsyncClient;
6+
7+
abstract class HttpAsyncClientTest extends HttpBaseTest
8+
{
9+
/**
10+
* @var HttpAsyncClient
11+
*/
12+
protected $httpAsyncClient;
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
protected function setUp()
18+
{
19+
$this->httpAsyncClient = $this->createHttpAsyncClient();
20+
}
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
protected function tearDown()
26+
{
27+
unset($this->httpAdapter);
28+
}
29+
30+
/**
31+
* @return HttpAsyncClient
32+
*/
33+
abstract protected function createHttpAsyncClient();
34+
35+
public function testSuccessiveCallMustUseResponseInterface()
36+
{
37+
$request = self::$messageFactory->createRequest(
38+
'GET',
39+
$this->getUri(),
40+
$this->defaultHeaders
41+
);
42+
43+
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
44+
$this->assertInstanceOf('Http\Client\Promise', $promise);
45+
46+
$response = null;
47+
$promise->then()->then()->then(function ($r) use(&$response) {
48+
$response = $r;
49+
});
50+
51+
$promise->wait();
52+
$this->assertResponse(
53+
$response,
54+
[
55+
'body' => 'Ok',
56+
]
57+
);
58+
}
59+
60+
public function testSuccessiveInvalidCallMustUseException()
61+
{
62+
$request = self::$messageFactory->createRequest(
63+
'GET',
64+
$this->getInvalidUri(),
65+
$this->defaultHeaders
66+
);
67+
68+
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
69+
$this->assertInstanceOf('Http\Client\Promise', $promise);
70+
71+
$exception = null;
72+
$response = null;
73+
$promise->then()->then()->then(function ($r) use(&$response) {
74+
$response = $r;
75+
}, function ($e) use (&$exception) {
76+
$exception = $e;
77+
});
78+
79+
$promise->wait();
80+
81+
$this->assertNull($response);
82+
$this->assertNotNull($exception);
83+
$this->assertInstanceOf('\Http\Client\Exception', $exception);
84+
}
85+
86+
/**
87+
* @dataProvider requestProvider
88+
* @group integration
89+
*/
90+
public function testAsyncSendRequest($method, $uri, array $headers, $body)
91+
{
92+
if ($body != null) {
93+
$headers['Content-Length'] = (string)strlen($body);
94+
}
95+
96+
$request = self::$messageFactory->createRequest(
97+
$method,
98+
$uri,
99+
$headers,
100+
$body
101+
);
102+
103+
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
104+
$this->assertInstanceOf('Http\Client\Promise', $promise);
105+
106+
$response = null;
107+
$promise->then(function ($r) use(&$response) {
108+
$response = $r;
109+
});
110+
111+
$promise->wait();
112+
$this->assertResponse(
113+
$response,
114+
[
115+
'body' => $method === 'HEAD' ? null : 'Ok',
116+
]
117+
);
118+
$this->assertRequest($method, $headers, $body, '1.1');
119+
}
120+
121+
/**
122+
* @group integration
123+
*/
124+
public function testSendAsyncWithInvalidUri()
125+
{
126+
$request = self::$messageFactory->createRequest(
127+
'GET',
128+
$this->getInvalidUri(),
129+
$this->defaultHeaders
130+
);
131+
132+
$exception = null;
133+
$response = null;
134+
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
135+
$this->assertInstanceOf('Http\Client\Promise', $promise);
136+
137+
$promise->then(function ($r) use(&$response) {
138+
$response = $r;
139+
}, function ($e) use (&$exception) {
140+
$exception = $e;
141+
});
142+
$promise->wait();
143+
144+
$this->assertNull($response);
145+
$this->assertNotNull($exception);
146+
$this->assertInstanceOf('\Http\Client\Exception', $exception);
147+
}
148+
149+
/**
150+
* @dataProvider requestWithOutcomeProvider
151+
* @group integration
152+
*/
153+
public function testSendAsyncRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body)
154+
{
155+
if ($protocolVersion === '1.0') {
156+
$body = null;
157+
}
158+
159+
if ($body != null) {
160+
$headers['Content-Length'] = (string)strlen($body);
161+
}
162+
163+
$request = self::$messageFactory->createRequest(
164+
$method = 'GET',
165+
$uriAndOutcome[0],
166+
$headers,
167+
$body,
168+
$protocolVersion
169+
);
170+
171+
$outcome = $uriAndOutcome[1];
172+
$outcome['protocolVersion'] = $protocolVersion;
173+
174+
$response = null;
175+
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
176+
$promise->then(function ($r) use(&$response) {
177+
$response = $r;
178+
});
179+
180+
$this->assertInstanceOf('Http\Client\Promise', $promise);
181+
$promise->wait();
182+
$this->assertResponse(
183+
$response,
184+
$outcome
185+
);
186+
$this->assertRequest($method, $headers, $body, $protocolVersion);
187+
}
188+
}
189+

src/HttpAdapterTest.php renamed to src/HttpBaseTest.php

+6-119
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,14 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Http\Adapter\Tests;
12+
namespace Http\Client\Tests;
1313

14-
use Http\Client\HttpClient;
15-
use Http\Client\Exception\RequestException;
16-
use Http\Client\Exception\BatchException;
17-
use Http\Message\MessageFactory;
1814
use Http\Discovery\MessageFactoryDiscovery;
15+
use Http\Message\MessageFactory;
1916
use Nerd\CartesianProduct\CartesianProduct;
20-
use Psr\Http\Message\RequestInterface;
2117
use Psr\Http\Message\ResponseInterface;
2218

23-
/**
24-
* @author GeLo <[email protected]>
25-
*/
26-
abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase
19+
abstract class HttpBaseTest extends \PHPUnit_Framework_TestCase
2720
{
2821
/**
2922
* @var string
@@ -35,11 +28,6 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase
3528
*/
3629
protected static $messageFactory;
3730

38-
/**
39-
* @var HttpClient
40-
*/
41-
protected $httpAdapter;
42-
4331
/**
4432
* @var array
4533
*/
@@ -79,107 +67,6 @@ public static function tearDownAfterClass()
7967
}
8068
}
8169

82-
/**
83-
* {@inheritdoc}
84-
*/
85-
protected function setUp()
86-
{
87-
$this->httpAdapter = $this->createHttpAdapter();
88-
}
89-
90-
/**
91-
* {@inheritdoc}
92-
*/
93-
protected function tearDown()
94-
{
95-
unset($this->httpAdapter);
96-
}
97-
98-
/**
99-
* @return HttpClient
100-
*/
101-
abstract protected function createHttpAdapter();
102-
103-
/**
104-
* @dataProvider requestProvider
105-
* @group integration
106-
*/
107-
public function testSendRequest($method, $uri, array $headers, $body)
108-
{
109-
if ($body != null) {
110-
$headers['Content-Length'] = (string)strlen($body);
111-
}
112-
113-
$request = self::$messageFactory->createRequest(
114-
$method,
115-
$uri,
116-
$headers,
117-
$body,
118-
'1.1'
119-
);
120-
121-
$response = $this->httpAdapter->sendRequest($request);
122-
123-
$this->assertResponse(
124-
$response,
125-
[
126-
'body' => $method === 'HEAD' ? null : 'Ok',
127-
]
128-
);
129-
$this->assertRequest($method, $headers, $body, '1.1');
130-
}
131-
132-
/**
133-
* @dataProvider requestWithOutcomeProvider
134-
* @group integration
135-
*/
136-
public function testSendRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body)
137-
{
138-
if ($protocolVersion === '1.0') {
139-
$body = null;
140-
}
141-
142-
if ($body != null) {
143-
$headers['Content-Length'] = (string)strlen($body);
144-
}
145-
146-
$request = self::$messageFactory->createRequest(
147-
$method = 'GET',
148-
$uriAndOutcome[0],
149-
$headers,
150-
$body,
151-
$protocolVersion
152-
);
153-
154-
$response = $this->httpAdapter->sendRequest($request);
155-
156-
$outcome = $uriAndOutcome[1];
157-
$outcome['protocolVersion'] = $protocolVersion;
158-
159-
$this->assertResponse(
160-
$response,
161-
$outcome
162-
);
163-
$this->assertRequest($method, $headers, $body, $protocolVersion);
164-
}
165-
166-
/**
167-
* @expectedException \Http\Client\Exception
168-
* @group integration
169-
*/
170-
public function testSendWithInvalidUri()
171-
{
172-
$request = self::$messageFactory->createRequest(
173-
'GET',
174-
$this->getInvalidUri(),
175-
$this->defaultHeaders,
176-
null,
177-
'1.1'
178-
);
179-
180-
$this->httpAdapter->sendRequest($request);
181-
}
182-
18370
/**
18471
* @return array
18572
*/
@@ -235,7 +122,7 @@ private function getMethods()
235122
*
236123
* @return string|null
237124
*/
238-
private function getUri(array $query = [])
125+
protected function getUri(array $query = [])
239126
{
240127
return !empty($query)
241128
? PHPUnitUtility::getUri().'?'.http_build_query($query, null, '&')
@@ -245,7 +132,7 @@ private function getUri(array $query = [])
245132
/**
246133
* @return string
247134
*/
248-
private function getInvalidUri()
135+
protected function getInvalidUri()
249136
{
250137
return 'http://invalid.php-http.org';
251138
}
@@ -390,7 +277,7 @@ protected function assertRequest(
390277
/**
391278
* @return array
392279
*/
393-
private function getRequest()
280+
protected function getRequest()
394281
{
395282
$file = fopen(self::$logPath, 'r');
396283
flock($file, LOCK_EX);

0 commit comments

Comments
 (0)