diff --git a/composer.json b/composer.json index 6c7c33a..bdd7d14 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/fixture/server.php b/fixture/server.php index 2ceb3f6..f45a65c 100644 --- a/fixture/server.php +++ b/fixture/server.php @@ -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); diff --git a/src/HttpAsyncClientTest.php b/src/HttpAsyncClientTest.php new file mode 100644 index 0000000..2daa27f --- /dev/null +++ b/src/HttpAsyncClientTest.php @@ -0,0 +1,189 @@ +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); + } +} + \ No newline at end of file diff --git a/src/HttpAdapterTest.php b/src/HttpBaseTest.php similarity index 70% rename from src/HttpAdapterTest.php rename to src/HttpBaseTest.php index d192869..55e5262 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpBaseTest.php @@ -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 - */ -abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase +abstract class HttpBaseTest extends \PHPUnit_Framework_TestCase { /** * @var string @@ -35,11 +28,6 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase */ protected static $messageFactory; - /** - * @var HttpClient - */ - protected $httpAdapter; - /** * @var array */ @@ -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 */ @@ -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, '&') @@ -245,7 +132,7 @@ private function getUri(array $query = []) /** * @return string */ - private function getInvalidUri() + protected function getInvalidUri() { return 'http://invalid.php-http.org'; } @@ -390,7 +277,7 @@ protected function assertRequest( /** * @return array */ - private function getRequest() + protected function getRequest() { $file = fopen(self::$logPath, 'r'); flock($file, LOCK_EX); diff --git a/src/HttpClientTest.php b/src/HttpClientTest.php new file mode 100644 index 0000000..c1d0e8b --- /dev/null +++ b/src/HttpClientTest.php @@ -0,0 +1,123 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Http\Client\Tests; + +use Http\Client\HttpClient; + +/** + * @author GeLo + */ +abstract class HttpClientTest extends HttpBaseTest +{ + /** + * @var HttpClient + */ + protected $httpAdapter; + + /** + * {@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 + ); + + $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 + ); + + $this->httpAdapter->sendRequest($request); + } +} diff --git a/src/PHPUnitUtility.php b/src/PHPUnitUtility.php index 53fefce..2f12ef2 100644 --- a/src/PHPUnitUtility.php +++ b/src/PHPUnitUtility.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Http\Adapter\Tests; +namespace Http\Client\Tests; /** * PHPUnit utility.