Skip to content

Add test for async client #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"phpunit/phpunit": "^4.4",
"php-http/discovery": "~0.2@dev",
"php-http/httplug": "~1.0@dev",
"php-http/httplug-async": "~0.1@dev",
"guzzlehttp/psr7": "^1.0",
"th3n3rd/cartesian-product": "^0.3"
},
"autoload": {
"psr-4": {
"Http\\Adapter\\Tests\\": "src/"
"Http\\Client\\Tests\\": "src/"
}
},
"bin": [
Expand Down
2 changes: 1 addition & 1 deletion fixture/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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

use Http\Adapter\Tests\PHPUnitUtility;
use Http\Client\Tests\PHPUnitUtility;

$file = fopen(PHPUnitUtility::getFile(true, 'php-http-adapter.log'), 'c');
flock($file, LOCK_EX);
Expand Down
189 changes: 189 additions & 0 deletions src/HttpAsyncClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

namespace Http\Client\Tests;

use Http\Client\HttpAsyncClient;

abstract class HttpAsyncClientTest extends HttpBaseTest
{
/**
* @var HttpAsyncClient
*/
protected $httpAsyncClient;

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->httpAsyncClient = $this->createHttpAsyncClient();
}

/**
* {@inheritdoc}
*/
protected function tearDown()
{
unset($this->httpAdapter);
}

/**
* @return HttpAsyncClient
*/
abstract protected function createHttpAsyncClient();

public function testSuccessiveCallMustUseResponseInterface()
{
$request = self::$messageFactory->createRequest(
'GET',
$this->getUri(),
$this->defaultHeaders
);

$promise = $this->httpAsyncClient->sendAsyncRequest($request);
$this->assertInstanceOf('Http\Client\Promise', $promise);

$response = null;
$promise->then()->then()->then(function ($r) use(&$response) {
$response = $r;
});

$promise->wait();
$this->assertResponse(
$response,
[
'body' => 'Ok',
]
);
}

public function testSuccessiveInvalidCallMustUseException()
{
$request = self::$messageFactory->createRequest(
'GET',
$this->getInvalidUri(),
$this->defaultHeaders
);

$promise = $this->httpAsyncClient->sendAsyncRequest($request);
$this->assertInstanceOf('Http\Client\Promise', $promise);

$exception = null;
$response = null;
$promise->then()->then()->then(function ($r) use(&$response) {
$response = $r;
}, function ($e) use (&$exception) {
$exception = $e;
});

$promise->wait();

$this->assertNull($response);
$this->assertNotNull($exception);
$this->assertInstanceOf('\Http\Client\Exception', $exception);
}

/**
* @dataProvider requestProvider
* @group integration
*/
public function testAsyncSendRequest($method, $uri, array $headers, $body)
{
if ($body != null) {
$headers['Content-Length'] = (string)strlen($body);
}

$request = self::$messageFactory->createRequest(
$method,
$uri,
$headers,
$body
);

$promise = $this->httpAsyncClient->sendAsyncRequest($request);
$this->assertInstanceOf('Http\Client\Promise', $promise);

$response = null;
$promise->then(function ($r) use(&$response) {
$response = $r;
});

$promise->wait();
$this->assertResponse(
$response,
[
'body' => $method === 'HEAD' ? null : 'Ok',
]
);
$this->assertRequest($method, $headers, $body, '1.1');
}

/**
* @group integration
*/
public function testSendAsyncWithInvalidUri()
{
$request = self::$messageFactory->createRequest(
'GET',
$this->getInvalidUri(),
$this->defaultHeaders
);

$exception = null;
$response = null;
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
$this->assertInstanceOf('Http\Client\Promise', $promise);

$promise->then(function ($r) use(&$response) {
$response = $r;
}, function ($e) use (&$exception) {
$exception = $e;
});
$promise->wait();

$this->assertNull($response);
$this->assertNotNull($exception);
$this->assertInstanceOf('\Http\Client\Exception', $exception);
}

/**
* @dataProvider requestWithOutcomeProvider
* @group integration
*/
public function testSendAsyncRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body)
{
if ($protocolVersion === '1.0') {
$body = null;
}

if ($body != null) {
$headers['Content-Length'] = (string)strlen($body);
}

$request = self::$messageFactory->createRequest(
$method = 'GET',
$uriAndOutcome[0],
$headers,
$body,
$protocolVersion
);

$outcome = $uriAndOutcome[1];
$outcome['protocolVersion'] = $protocolVersion;

$response = null;
$promise = $this->httpAsyncClient->sendAsyncRequest($request);
$promise->then(function ($r) use(&$response) {
$response = $r;
});

$this->assertInstanceOf('Http\Client\Promise', $promise);
$promise->wait();
$this->assertResponse(
$response,
$outcome
);
$this->assertRequest($method, $headers, $body, $protocolVersion);
}
}

125 changes: 6 additions & 119 deletions src/HttpAdapterTest.php → src/HttpBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@
* file that was distributed with this source code.
*/

namespace Http\Adapter\Tests;
namespace Http\Client\Tests;

use Http\Client\HttpClient;
use Http\Client\Exception\RequestException;
use Http\Client\Exception\BatchException;
use Http\Message\MessageFactory;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MessageFactory;
use Nerd\CartesianProduct\CartesianProduct;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @author GeLo <[email protected]>
*/
abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase
abstract class HttpBaseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
Expand All @@ -35,11 +28,6 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase
*/
protected static $messageFactory;

/**
* @var HttpClient
*/
protected $httpAdapter;

/**
* @var array
*/
Expand Down Expand Up @@ -79,107 +67,6 @@ public static function tearDownAfterClass()
}
}

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->httpAdapter = $this->createHttpAdapter();
}

/**
* {@inheritdoc}
*/
protected function tearDown()
{
unset($this->httpAdapter);
}

/**
* @return HttpClient
*/
abstract protected function createHttpAdapter();

/**
* @dataProvider requestProvider
* @group integration
*/
public function testSendRequest($method, $uri, array $headers, $body)
{
if ($body != null) {
$headers['Content-Length'] = (string)strlen($body);
}

$request = self::$messageFactory->createRequest(
$method,
$uri,
$headers,
$body,
'1.1'
);

$response = $this->httpAdapter->sendRequest($request);

$this->assertResponse(
$response,
[
'body' => $method === 'HEAD' ? null : 'Ok',
]
);
$this->assertRequest($method, $headers, $body, '1.1');
}

/**
* @dataProvider requestWithOutcomeProvider
* @group integration
*/
public function testSendRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body)
{
if ($protocolVersion === '1.0') {
$body = null;
}

if ($body != null) {
$headers['Content-Length'] = (string)strlen($body);
}

$request = self::$messageFactory->createRequest(
$method = 'GET',
$uriAndOutcome[0],
$headers,
$body,
$protocolVersion
);

$response = $this->httpAdapter->sendRequest($request);

$outcome = $uriAndOutcome[1];
$outcome['protocolVersion'] = $protocolVersion;

$this->assertResponse(
$response,
$outcome
);
$this->assertRequest($method, $headers, $body, $protocolVersion);
}

/**
* @expectedException \Http\Client\Exception
* @group integration
*/
public function testSendWithInvalidUri()
{
$request = self::$messageFactory->createRequest(
'GET',
$this->getInvalidUri(),
$this->defaultHeaders,
null,
'1.1'
);

$this->httpAdapter->sendRequest($request);
}

/**
* @return array
*/
Expand Down Expand Up @@ -235,7 +122,7 @@ private function getMethods()
*
* @return string|null
*/
private function getUri(array $query = [])
protected function getUri(array $query = [])
{
return !empty($query)
? PHPUnitUtility::getUri().'?'.http_build_query($query, null, '&')
Expand All @@ -245,7 +132,7 @@ private function getUri(array $query = [])
/**
* @return string
*/
private function getInvalidUri()
protected function getInvalidUri()
{
return 'http://invalid.php-http.org';
}
Expand Down Expand Up @@ -390,7 +277,7 @@ protected function assertRequest(
/**
* @return array
*/
private function getRequest()
protected function getRequest()
{
$file = fopen(self::$logPath, 'r');
flock($file, LOCK_EX);
Expand Down
Loading