From 6b951860b826124616359434f2a793d45e7de08c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sun, 17 May 2015 19:13:03 +0200 Subject: [PATCH 01/12] Contract changes --- composer.json | 2 +- src/HttpAdapterTest.php | 146 ++++++++++++++++++++-------------------- 2 files changed, 73 insertions(+), 75 deletions(-) diff --git a/composer.json b/composer.json index b7df41c..41fc219 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "phpunit/phpunit": "~4.4" }, "require-dev": { - "php-http/adapter-core": "dev-initial_import" + "php-http/adapter-core": "dev-contract_changes" }, "autoload": { "psr-4": { diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index a915cf6..356a67d 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -13,9 +13,9 @@ use Http\Adapter\CoreHttpAdapter; use Http\Adapter\HttpAdapterException; -use Http\Adapter\Message\RequestInterface; -use Http\Adapter\Message\ResponseInterface; -use Http\Adapter\MultiHttpAdapterException; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Http\Adapter\Exception\MultiHttpAdapterException; /** * @author GeLo @@ -61,7 +61,7 @@ public static function tearDownAfterClass() protected function setUp() { $this->defaultOptions = [ - 'protocol_version' => RequestInterface::PROTOCOL_VERSION_1_1, + 'protocol_version' => '1.1', 'status_code' => 200, 'reason_phrase' => 'OK', 'headers' => ['Content-Type' => 'text/html'], @@ -88,7 +88,7 @@ abstract public function testGetName(); public function testGet($uri, array $headers = []) { $this->assertResponse($this->httpAdapter->get($uri, $headers)); - $this->assertRequest(RequestInterface::METHOD_GET, $headers); + $this->assertRequest('GET', $headers); } /** @@ -98,7 +98,7 @@ public function testGet($uri, array $headers = []) public function testHead($uri, array $headers = []) { $this->assertResponse($this->httpAdapter->head($uri, $headers), ['body' => null]); - $this->assertRequest(RequestInterface::METHOD_HEAD, $headers); + $this->assertRequest('HEAD', $headers); } /** @@ -108,7 +108,7 @@ public function testHead($uri, array $headers = []) public function testTrace($uri, array $headers = []) { $this->assertResponse($this->httpAdapter->trace($uri, $headers)); - $this->assertRequest(RequestInterface::METHOD_TRACE, $headers); + $this->assertRequest('TRACE', $headers); } /** @@ -118,7 +118,7 @@ public function testTrace($uri, array $headers = []) public function testPost($uri, array $headers = [], array $data = [], array $files = []) { $this->assertResponse($this->httpAdapter->post($uri, $headers, $data, $files)); - $this->assertRequest(RequestInterface::METHOD_POST, $headers, $data, $files); + $this->assertRequest('POST', $headers, $data, $files); } /** @@ -128,7 +128,7 @@ public function testPost($uri, array $headers = [], array $data = [], array $fil public function testPut($uri, array $headers = [], array $data = [], array $files = []) { $this->assertResponse($this->httpAdapter->put($uri, $headers, $data, $files)); - $this->assertRequest(RequestInterface::METHOD_PUT, $headers, $data, $files); + $this->assertRequest('PUT', $headers, $data, $files); } /** @@ -138,7 +138,7 @@ public function testPut($uri, array $headers = [], array $data = [], array $file public function testPatch($uri, array $headers = [], array $data = [], array $files = []) { $this->assertResponse($this->httpAdapter->patch($uri, $headers, $data, $files)); - $this->assertRequest(RequestInterface::METHOD_PATCH, $headers, $data, $files); + $this->assertRequest('PATCH', $headers, $data, $files); } /** @@ -148,7 +148,7 @@ public function testPatch($uri, array $headers = [], array $data = [], array $fi public function testDelete($uri, array $headers = [], array $data = [], array $files = []) { $this->assertResponse($this->httpAdapter->delete($uri, $headers, $data, $files)); - $this->assertRequest(RequestInterface::METHOD_DELETE, $headers, $data, $files); + $this->assertRequest('DELETE', $headers, $data, $files); } /** @@ -158,7 +158,7 @@ public function testDelete($uri, array $headers = [], array $data = [], array $f public function testOptions($uri, array $headers = [], array $data = [], array $files = []) { $this->assertResponse($this->httpAdapter->options($uri, $headers, $data, $files)); - $this->assertRequest(RequestInterface::METHOD_OPTIONS, $headers, $data, $files); + $this->assertRequest('OPTIONS', $headers, $data, $files); } /** @@ -167,17 +167,17 @@ public function testOptions($uri, array $headers = [], array $data = [], array $ */ public function testSendRequest($uri, $method, array $headers = [], array $data = []) { - $request = $this->httpAdapter->getConfiguration()->getMessageFactory()->createRequest( + $request = $this->httpAdapter->getMessageFactory()->createRequest( $method, $uri, - RequestInterface::PROTOCOL_VERSION_1_1, + '1.1', $headers, http_build_query($data, null, '&') ); $this->assertResponse( $this->httpAdapter->sendRequest($request), - $method === RequestInterface::METHOD_HEAD ? ['body' => null] : [] + $method === 'HEAD' ? ['body' => null] : [] ); $this->assertRequest($method, $headers, $data); @@ -189,10 +189,10 @@ public function testSendRequest($uri, $method, array $headers = [], array $data */ public function testSendInternalRequest($uri, $method, array $headers = [], array $data = [], array $files = []) { - $request = $this->httpAdapter->getConfiguration()->getMessageFactory()->createInternalRequest( + $request = $this->httpAdapter->getMessageFactory()->createInternalRequest( $method, $uri, - RequestInterface::PROTOCOL_VERSION_1_1, + '1.1', $headers, $data, $files @@ -200,7 +200,7 @@ public function testSendInternalRequest($uri, $method, array $headers = [], arra $this->assertResponse( $this->httpAdapter->sendRequest($request), - $method === RequestInterface::METHOD_HEAD ? ['body' => null] : [] + $method === 'HEAD' ? ['body' => null] : [] ); $this->assertRequest($method, $headers, $data, $files); @@ -245,7 +245,7 @@ public function testSendWithCustomArgSeparatorOutput() $data = $this->getData() )); - $this->assertRequest(RequestInterface::METHOD_POST, $headers, $data); + $this->assertRequest('POST', $headers, $data); ini_set('arg_separator.output', $argSeparatorOutput); } @@ -255,12 +255,10 @@ public function testSendWithCustomArgSeparatorOutput() */ public function testSendWithProtocolVersion10() { - $this->httpAdapter->getConfiguration()->setProtocolVersion( - $protocolVersion = RequestInterface::PROTOCOL_VERSION_1_0 - ); + $this->httpAdapter->setOption('protocolVersion', '1.0'); $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $this->getUri()), + $this->httpAdapter->send($method = 'GET', $this->getUri()), ['protocol_version' => $protocolVersion] ); @@ -272,9 +270,9 @@ public function testSendWithProtocolVersion10() */ public function testSendWithUserAgent() { - $this->httpAdapter->getConfiguration()->setUserAgent($userAgent = 'foo'); + $this->httpAdapter->setOption('userAgent', 'foo'); - $this->assertResponse($this->httpAdapter->send($method = RequestInterface::METHOD_GET, $this->getUri())); + $this->assertResponse($this->httpAdapter->send($method = 'GET', $this->getUri())); $this->assertRequest($method, ['User-Agent' => $userAgent]); } @@ -284,7 +282,7 @@ public function testSendWithUserAgent() public function testSendWithClientError() { $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $uri = $this->getClientErrorUri()), + $this->httpAdapter->send($method = 'GET', $uri = $this->getClientErrorUri()), [ 'status_code' => 400, 'reason_phrase' => 'Bad Request', @@ -300,7 +298,7 @@ public function testSendWithClientError() public function testSendWithServerError() { $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $uri = $this->getServerErrorUri()), + $this->httpAdapter->send($method = 'GET', $uri = $this->getServerErrorUri()), [ 'status_code' => 500, 'reason_phrase' => 'Internal Server Error', @@ -316,7 +314,7 @@ public function testSendWithServerError() public function testSendWithRedirect() { $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $uri = $this->getRedirectUri()), + $this->httpAdapter->send($method = 'GET', $uri = $this->getRedirectUri()), [ 'status_code' => 302, 'reason_phrase' => 'Found', @@ -328,23 +326,23 @@ public function testSendWithRedirect() } /** - * @expectedException \Http\Adapter\HttpAdapterException + * @expectedException \Http\Adapter\Exception\HttpAdapterException * @group integration */ public function testSendWithInvalidUri() { - $this->httpAdapter->send(RequestInterface::METHOD_GET, $this->getInvalidUri()); + $this->httpAdapter->send('GET', $this->getInvalidUri()); } /** * @dataProvider timeoutProvider - * @expectedException \Http\Adapter\HttpAdapterException + * @expectedException \Http\Adapter\Exception\HttpAdapterException * @group integration */ public function testSendWithTimeoutExceeded($timeout) { - $this->httpAdapter->getConfiguration()->setTimeout($timeout); - $this->httpAdapter->send(RequestInterface::METHOD_GET, $this->getDelayUri($timeout)); + $this->httpAdapter->setOption('timeout', $timeout); + $this->httpAdapter->send('GET', $this->getDelayUri($timeout)); } /** @@ -394,58 +392,58 @@ public function requestProvider() public function internalRequestProvider() { return [ - [$this->getUri(), RequestInterface::METHOD_GET], - [$this->getUri(), RequestInterface::METHOD_GET, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_HEAD], - [$this->getUri(), RequestInterface::METHOD_HEAD, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_TRACE], - [$this->getUri(), RequestInterface::METHOD_TRACE, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_POST], - [$this->getUri(), RequestInterface::METHOD_POST, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_POST, $this->getHeaders(), $this->getData()], + [$this->getUri(), 'GET'], + [$this->getUri(), 'GET', $this->getHeaders()], + [$this->getUri(), 'HEAD'], + [$this->getUri(), 'HEAD', $this->getHeaders()], + [$this->getUri(), 'TRACE'], + [$this->getUri(), 'TRACE', $this->getHeaders()], + [$this->getUri(), 'POST'], + [$this->getUri(), 'POST', $this->getHeaders()], + [$this->getUri(), 'POST', $this->getHeaders(), $this->getData()], [ $this->getUri(), - RequestInterface::METHOD_POST, + 'POST', $this->getHeaders(), $this->getData(), $this->getFiles(), ], - [$this->getUri(), RequestInterface::METHOD_PUT], - [$this->getUri(), RequestInterface::METHOD_PUT, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_PUT, $this->getHeaders(), $this->getData()], + [$this->getUri(), 'PUT'], + [$this->getUri(), 'PUT', $this->getHeaders()], + [$this->getUri(), 'PUT', $this->getHeaders(), $this->getData()], [ $this->getUri(), - RequestInterface::METHOD_PUT, + 'PUT', $this->getHeaders(), $this->getData(), $this->getFiles(), ], - [$this->getUri(), RequestInterface::METHOD_PATCH], - [$this->getUri(), RequestInterface::METHOD_PATCH, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_PATCH, $this->getHeaders(), $this->getData()], + [$this->getUri(), 'PATCH'], + [$this->getUri(), 'PATCH', $this->getHeaders()], + [$this->getUri(), 'PATCH', $this->getHeaders(), $this->getData()], [ $this->getUri(), - RequestInterface::METHOD_PATCH, + 'PATCH', $this->getHeaders(), $this->getData(), $this->getFiles(), ], - [$this->getUri(), RequestInterface::METHOD_DELETE], - [$this->getUri(), RequestInterface::METHOD_DELETE, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_DELETE, $this->getHeaders(), $this->getData()], + [$this->getUri(), 'DELETE'], + [$this->getUri(), 'DELETE', $this->getHeaders()], + [$this->getUri(), 'DELETE', $this->getHeaders(), $this->getData()], [ $this->getUri(), - RequestInterface::METHOD_DELETE, + 'DELETE', $this->getHeaders(), $this->getData(), $this->getFiles(), ], - [$this->getUri(), RequestInterface::METHOD_OPTIONS], - [$this->getUri(), RequestInterface::METHOD_OPTIONS, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_OPTIONS, $this->getHeaders(), $this->getData()], + [$this->getUri(), 'OPTIONS'], + [$this->getUri(), 'OPTIONS', $this->getHeaders()], + [$this->getUri(), 'OPTIONS', $this->getHeaders(), $this->getData()], [ $this->getUri(), - RequestInterface::METHOD_OPTIONS, + 'OPTIONS', $this->getHeaders(), $this->getData(), $this->getFiles(), @@ -458,13 +456,13 @@ public function internalRequestProvider() */ public function requestsProvider() { - $requests = [[RequestInterface::METHOD_GET, $this->getUri()]]; + $requests = [['GET', $this->getUri()]]; foreach ($this->requestProvider() as $request) { $requests[] = [ $request[1], $request[0], - RequestInterface::PROTOCOL_VERSION_1_1, + '1.1', isset($request[2]) ? $request[2] : [], isset($request[3]) ? $request[3] : [], isset($request[4]) ? $request[4] : [], @@ -472,20 +470,20 @@ public function requestsProvider() } foreach ($this->requestProvider() as $request) { - $requests[] = $this->httpAdapter->getConfiguration()->getMessageFactory()->createRequest( + $requests[] = $this->httpAdapter->getMessageFactory()->createRequest( $request[1], $request[0], - RequestInterface::PROTOCOL_VERSION_1_1, + '1.1', isset($request[2]) ? $request[2] : [], http_build_query(isset($request[3]) ? $request[3] : [], null, '&') ); } foreach ($this->requestProvider() as $request) { - $requests[] = $this->httpAdapter->getConfiguration()->getMessageFactory()->createInternalRequest( + $requests[] = $this->httpAdapter->getMessageFactory()->createInternalRequest( $request[1], $request[0], - RequestInterface::PROTOCOL_VERSION_1_1, + '1.1', isset($request[2]) ? $request[2] : [], isset($request[3]) ? $request[3] : [], isset($request[4]) ? $request[4] : [] @@ -500,7 +498,7 @@ public function requestsProvider() */ public function erroredRequestsProvider() { - return [[RequestInterface::METHOD_GET, $this->getInvalidUri()]]; + return [['GET', $this->getInvalidUri()]]; } /** @@ -522,7 +520,7 @@ abstract protected function createHttpAdapter(); */ protected function assertResponse($response, array $options = []) { - $this->assertInstanceOf('Http\Adapter\Message\ResponseInterface', $response); + $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); $options = array_merge($this->defaultOptions, $options); @@ -570,7 +568,7 @@ protected function assertRequest( array $headers = [], array $data = [], array $files = [], - $protocolVersion = RequestInterface::PROTOCOL_VERSION_1_1 + $protocolVersion = '1.1' ) { $request = $this->getRequest(); @@ -596,10 +594,10 @@ protected function assertRequest( } $inputMethods = [ - RequestInterface::METHOD_PUT, - RequestInterface::METHOD_PATCH, - RequestInterface::METHOD_DELETE, - RequestInterface::METHOD_OPTIONS, + 'PUT', + 'PATCH', + 'DELETE', + 'OPTIONS', ]; if (in_array($method, $inputMethods)) { @@ -844,7 +842,7 @@ private function assertMultiResponses(array $responses, array $requests) foreach ($responses as $response) { $this->assertTrue($response->hasParameter('request')); $this->assertInstanceOf( - 'Http\Adapter\Message\InternalRequestInterface', + 'Http\Adapter\Message\InternalRequest', $response->getParameter('request') ); } @@ -861,7 +859,7 @@ private function assertMultiExceptions(array $exceptions, array $requests) foreach ($exceptions as $exception) { $this->assertTrue($exception->hasRequest()); $this->assertInstanceOf( - 'Http\Adapter\Message\InternalRequestInterface', + 'Http\Adapter\Message\InternalRequest', $exception->getRequest() ); } From c8f11f16716cd93c95e4a497e175637f4933570d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Mon, 18 May 2015 07:58:14 +0200 Subject: [PATCH 02/12] Updates dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 41fc219..c8bce8f 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "phpunit/phpunit": "~4.4" }, "require-dev": { - "php-http/adapter-core": "dev-contract_changes" + "php-http/adapter-core": "dev-internal_separation" }, "autoload": { "psr-4": { From 070fd0ed9d3294982abeeec4728e206643dd8021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Mon, 18 May 2015 08:07:40 +0200 Subject: [PATCH 03/12] Fixes some tests --- src/HttpAdapterTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index 356a67d..9a354b3 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -553,7 +553,7 @@ protected function assertResponse($response, array $options = []) $parameters['effective_uri'] = $options['effective_uri']; } - $this->assertSame($parameters, $response->getParameters()); + // $this->assertSame($parameters, $response->getParameters()); } /** @@ -577,7 +577,7 @@ protected function assertRequest( $defaultHeaders = [ 'Connection' => 'close', - 'User-Agent' => 'PHP Http Adapter', + 'User-Agent' => 'PHP HTTP Adapter', ]; $headers = array_merge($defaultHeaders, $headers); From 63949de39340f1effc7c45945e6571158356ba13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 03:40:14 +0200 Subject: [PATCH 04/12] Removes Client related tests --- composer.json | 4 +- src/HttpAdapterTest.php | 720 +++++++++++----------------------------- 2 files changed, 188 insertions(+), 536 deletions(-) diff --git a/composer.json b/composer.json index c8bce8f..ec9cb1c 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,9 @@ "phpunit/phpunit": "~4.4" }, "require-dev": { - "php-http/adapter-core": "dev-internal_separation" + "php-http/common": "dev-master", + "guzzlehttp/psr7": "~1.0", + "th3n3rd/cartesian-product": "^0.2" }, "autoload": { "psr-4": { diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index 9a354b3..6c50a75 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -11,11 +11,14 @@ namespace Http\Adapter\Tests; -use Http\Adapter\CoreHttpAdapter; +use Http\Adapter\HttpAdapter; use Http\Adapter\HttpAdapterException; +use Http\Adapter\Exception\MultiHttpAdapterException; +use Http\Message\MessageFactory; +use Http\Common\Message\MessageFactoryGuesser; +use Nerd\CartesianProduct\CartesianProduct; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Http\Adapter\Exception\MultiHttpAdapterException; /** * @author GeLo @@ -28,7 +31,12 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase private static $logPath; /** - * @var CoreHttpAdapter + * @var MessageFactory + */ + protected static $messageFactory; + + /** + * @var HttpAdapter */ protected $httpAdapter; @@ -43,6 +51,7 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase public static function setUpBeforeClass() { self::$logPath = PHPUnitUtility::getFile(true, 'php-http-adapter.log'); + self::$messageFactory = MessageFactoryGuesser::guess(); } /** @@ -61,11 +70,11 @@ public static function tearDownAfterClass() protected function setUp() { $this->defaultOptions = [ - 'protocol_version' => '1.1', - 'status_code' => 200, - 'reason_phrase' => 'OK', - 'headers' => ['Content-Type' => 'text/html'], - 'body' => 'Ok', + 'protocolVersion' => '1.1', + 'statusCode' => 200, + 'reasonPhrase' => 'OK', + 'headers' => ['Content-Type' => 'text/html'], + 'body' => 'Ok', ]; $this->httpAdapter = $this->createHttpAdapter(); @@ -82,146 +91,53 @@ protected function tearDown() abstract public function testGetName(); /** - * @dataProvider simpleProvider - * @group integration - */ - public function testGet($uri, array $headers = []) - { - $this->assertResponse($this->httpAdapter->get($uri, $headers)); - $this->assertRequest('GET', $headers); - } - - /** - * @dataProvider simpleProvider - * @group integration - */ - public function testHead($uri, array $headers = []) - { - $this->assertResponse($this->httpAdapter->head($uri, $headers), ['body' => null]); - $this->assertRequest('HEAD', $headers); - } - - /** - * @dataProvider simpleProvider - * @group integration - */ - public function testTrace($uri, array $headers = []) - { - $this->assertResponse($this->httpAdapter->trace($uri, $headers)); - $this->assertRequest('TRACE', $headers); - } - - /** - * @dataProvider fullProvider - * @group integration - */ - public function testPost($uri, array $headers = [], array $data = [], array $files = []) - { - $this->assertResponse($this->httpAdapter->post($uri, $headers, $data, $files)); - $this->assertRequest('POST', $headers, $data, $files); - } - - /** - * @dataProvider fullProvider - * @group integration - */ - public function testPut($uri, array $headers = [], array $data = [], array $files = []) - { - $this->assertResponse($this->httpAdapter->put($uri, $headers, $data, $files)); - $this->assertRequest('PUT', $headers, $data, $files); - } - - /** - * @dataProvider fullProvider - * @group integration - */ - public function testPatch($uri, array $headers = [], array $data = [], array $files = []) - { - $this->assertResponse($this->httpAdapter->patch($uri, $headers, $data, $files)); - $this->assertRequest('PATCH', $headers, $data, $files); - } - - /** - * @dataProvider fullProvider - * @group integration + * @return HttpAdapter */ - public function testDelete($uri, array $headers = [], array $data = [], array $files = []) - { - $this->assertResponse($this->httpAdapter->delete($uri, $headers, $data, $files)); - $this->assertRequest('DELETE', $headers, $data, $files); - } - - /** - * @dataProvider fullProvider - * @group integration - */ - public function testOptions($uri, array $headers = [], array $data = [], array $files = []) - { - $this->assertResponse($this->httpAdapter->options($uri, $headers, $data, $files)); - $this->assertRequest('OPTIONS', $headers, $data, $files); - } + abstract protected function createHttpAdapter(); /** * @dataProvider requestProvider - * @group integration + * @group integration */ - public function testSendRequest($uri, $method, array $headers = [], array $data = []) + public function testSendRequest($method, $uri, $protocolVersion, array $headers, $body) { - $request = $this->httpAdapter->getMessageFactory()->createRequest( + $request = self::$messageFactory->createRequest( $method, $uri, - '1.1', + $protocolVersion, $headers, - http_build_query($data, null, '&') + $body ); - $this->assertResponse( - $this->httpAdapter->sendRequest($request), - $method === 'HEAD' ? ['body' => null] : [] - ); - - $this->assertRequest($method, $headers, $data); - } - - /** - * @dataProvider internalRequestProvider - * @group integration - */ - public function testSendInternalRequest($uri, $method, array $headers = [], array $data = [], array $files = []) - { - $request = $this->httpAdapter->getMessageFactory()->createInternalRequest( - $method, - $uri, - '1.1', - $headers, - $data, - $files - ); + $response = $this->httpAdapter->sendRequest($request); $this->assertResponse( - $this->httpAdapter->sendRequest($request), - $method === 'HEAD' ? ['body' => null] : [] + $response, + [ + 'protocolVersion' => $protocolVersion, + 'body' => $method === 'HEAD' ? null : $body, + ] ); - - $this->assertRequest($method, $headers, $data, $files); + $this->assertRequest($method, $headers, $body); } /** - * @group integration + * @dataProvider requestsProvider + * @group integration */ - public function testSendRequests() + public function testSendRequests(array $requests) { - $this->assertMultiResponses($this->httpAdapter->sendRequests($requests = $this->requestsProvider()), $requests); + $responses = $this->httpAdapter->sendRequests($requests); + + $this->assertMultiResponses($responses, $requests); } /** - * @group integration + * @dataProvider erroredRequestProvider + * @group integration */ - public function testSendErroredRequests() + public function testSendErroredRequests(array $requests, array $erroredRequests) { - $requests = $this->requestsProvider(); - $erroredRequests = $this->erroredRequestsProvider(); - try { $this->httpAdapter->sendRequests(array_merge($requests, $erroredRequests)); $this->fail(); @@ -234,58 +150,20 @@ public function testSendErroredRequests() /** * @group integration */ - public function testSendWithCustomArgSeparatorOutput() - { - $argSeparatorOutput = ini_get('arg_separator.output'); - ini_set('arg_separator.output', '&'); - - $this->assertResponse($this->httpAdapter->post( - $this->getUri(), - $headers = $this->getHeaders(), - $data = $this->getData() - )); - - $this->assertRequest('POST', $headers, $data); - - ini_set('arg_separator.output', $argSeparatorOutput); - } - - /** - * @group integration - */ - public function testSendWithProtocolVersion10() + public function testSendWithClientError() { - $this->httpAdapter->setOption('protocolVersion', '1.0'); - - $this->assertResponse( - $this->httpAdapter->send($method = 'GET', $this->getUri()), - ['protocol_version' => $protocolVersion] + $request = self::$messageFactory->createRequest( + 'GET', + $this->getClientErrorUri() ); - $this->assertRequest($method, [], [], [], $protocolVersion); - } + $response = $this->httpAdapter->sendRequest($request); - /** - * @group integration - */ - public function testSendWithUserAgent() - { - $this->httpAdapter->setOption('userAgent', 'foo'); - - $this->assertResponse($this->httpAdapter->send($method = 'GET', $this->getUri())); - $this->assertRequest($method, ['User-Agent' => $userAgent]); - } - - /** - * @group integration - */ - public function testSendWithClientError() - { $this->assertResponse( - $this->httpAdapter->send($method = 'GET', $uri = $this->getClientErrorUri()), + $response, [ - 'status_code' => 400, - 'reason_phrase' => 'Bad Request', + 'statusCode' => 400, + 'reasonPhrase' => 'Bad Request', ] ); @@ -297,11 +175,18 @@ public function testSendWithClientError() */ public function testSendWithServerError() { + $request = self::$messageFactory->createRequest( + 'GET', + $this->getServerErrorUri() + ); + + $response = $this->httpAdapter->sendRequest($request); + $this->assertResponse( - $this->httpAdapter->send($method = 'GET', $uri = $this->getServerErrorUri()), + $response, [ - 'status_code' => 500, - 'reason_phrase' => 'Internal Server Error', + 'statusCode' => 500, + 'reasonPhrase' => 'Internal Server Error', ] ); @@ -313,12 +198,19 @@ public function testSendWithServerError() */ public function testSendWithRedirect() { + $request = self::$messageFactory->createRequest( + 'GET', + $this->getRedirectUri() + ); + + $response = $this->httpAdapter->sendRequest($request); + $this->assertResponse( - $this->httpAdapter->send($method = 'GET', $uri = $this->getRedirectUri()), + $response, [ - 'status_code' => 302, - 'reason_phrase' => 'Found', - 'body' => 'Redirect', + 'statusCode' => 302, + 'reasonPhrase' => 'Found', + 'body' => 'Redirect', ] ); @@ -327,178 +219,106 @@ public function testSendWithRedirect() /** * @expectedException \Http\Adapter\Exception\HttpAdapterException - * @group integration + * @group integration */ public function testSendWithInvalidUri() { - $this->httpAdapter->send('GET', $this->getInvalidUri()); + $request = self::$messageFactory->createRequest( + 'GET', + $this->getInvalidUri() + ); + + $this->httpAdapter->sendRequest($request); } /** - * @dataProvider timeoutProvider + * @dataProvider timeoutProvider * @expectedException \Http\Adapter\Exception\HttpAdapterException - * @group integration + * @group integration */ - public function testSendWithTimeoutExceeded($timeout) - { - $this->httpAdapter->setOption('timeout', $timeout); - $this->httpAdapter->send('GET', $this->getDelayUri($timeout)); - } + // public function testSendWithTimeoutExceeded($timeout) + // { + // $this->httpAdapter->setOption('timeout', $timeout); + // $this->httpAdapter->send('GET', $this->getDelayUri($timeout)); + // } /** * @return array */ - public function simpleProvider() + public function requestProvider() { - return [ - [$this->getUri()], - [$this->getUri(), $this->getHeaders()], + $sets = [ + 'methods' => $this->getMethods(), + 'uri' => [$this->getUri()], + 'protocolVersions' => $this->getProtocolVersions(), + 'headers' => [[], $this->getHeaders()], + 'body' => [null, http_build_query($this->getData(), null, '&')], ]; - } - /** - * @return array - */ - public function fullProvider() - { - return array_merge( - $this->simpleProvider(), - [ - [$this->getUri(), $this->getHeaders(), $this->getData()], - [$this->getUri(), $this->getHeaders(), $this->getData(), $this->getFiles()], - ] - ); + $cartesianProduct = new CartesianProduct($sets); + + return $cartesianProduct->compute(); } /** * @return array */ - public function requestProvider() + public function requestsProvider() { $requests = []; - foreach ($this->internalRequestProvider() as $request) { - if (!isset($request[4])) { - $requests[] = $request; - } + foreach ($this->requestProvider() as $request) { + $requests[] = self::$messageFactory->createRequest( + $request[0], + $request[1], + $request[3], + $request[4], + ); } - return $requests; + return array_chunk($requests, 3); } /** * @return array */ - public function internalRequestProvider() - { - return [ - [$this->getUri(), 'GET'], - [$this->getUri(), 'GET', $this->getHeaders()], - [$this->getUri(), 'HEAD'], - [$this->getUri(), 'HEAD', $this->getHeaders()], - [$this->getUri(), 'TRACE'], - [$this->getUri(), 'TRACE', $this->getHeaders()], - [$this->getUri(), 'POST'], - [$this->getUri(), 'POST', $this->getHeaders()], - [$this->getUri(), 'POST', $this->getHeaders(), $this->getData()], - [ - $this->getUri(), - 'POST', - $this->getHeaders(), - $this->getData(), - $this->getFiles(), - ], - [$this->getUri(), 'PUT'], - [$this->getUri(), 'PUT', $this->getHeaders()], - [$this->getUri(), 'PUT', $this->getHeaders(), $this->getData()], - [ - $this->getUri(), - 'PUT', - $this->getHeaders(), - $this->getData(), - $this->getFiles(), - ], - [$this->getUri(), 'PATCH'], - [$this->getUri(), 'PATCH', $this->getHeaders()], - [$this->getUri(), 'PATCH', $this->getHeaders(), $this->getData()], - [ - $this->getUri(), - 'PATCH', - $this->getHeaders(), - $this->getData(), - $this->getFiles(), - ], - [$this->getUri(), 'DELETE'], - [$this->getUri(), 'DELETE', $this->getHeaders()], - [$this->getUri(), 'DELETE', $this->getHeaders(), $this->getData()], - [ - $this->getUri(), - 'DELETE', - $this->getHeaders(), - $this->getData(), - $this->getFiles(), - ], - [$this->getUri(), 'OPTIONS'], - [$this->getUri(), 'OPTIONS', $this->getHeaders()], - [$this->getUri(), 'OPTIONS', $this->getHeaders(), $this->getData()], - [ - $this->getUri(), - 'OPTIONS', - $this->getHeaders(), - $this->getData(), - $this->getFiles(), - ], - ]; - } - - /** - * @return array - */ - public function requestsProvider() + public function erroredRequestsProvider() { - $requests = [['GET', $this->getUri()]]; + $requests = []; + $erroredRequests = []; + $requestList = []; foreach ($this->requestProvider() as $request) { - $requests[] = [ - $request[1], - $request[0], - '1.1', - isset($request[2]) ? $request[2] : [], - isset($request[3]) ? $request[3] : [], - isset($request[4]) ? $request[4] : [], - ]; - } + if ($request[0] !== 'GET') { + continue; + } - foreach ($this->requestProvider() as $request) { - $requests[] = $this->httpAdapter->getMessageFactory()->createRequest( - $request[1], + $requests[] = self::$messageFactory->createRequest( $request[0], - '1.1', - isset($request[2]) ? $request[2] : [], - http_build_query(isset($request[3]) ? $request[3] : [], null, '&') + $request[1], + $request[3], + $request[4], ); - } - foreach ($this->requestProvider() as $request) { - $requests[] = $this->httpAdapter->getMessageFactory()->createInternalRequest( - $request[1], + $erroredRequests[] = self::$messageFactory->createRequest( $request[0], - '1.1', - isset($request[2]) ? $request[2] : [], - isset($request[3]) ? $request[3] : [], - isset($request[4]) ? $request[4] : [] + $this->getInvalidUri(), + $request[3], + $request[4], ); } - return $requests; - } + $requests = array_chunk($requests, 3); + $erroredRequests = array_chunk($erroredRequests, 3); - /** - * @return array - */ - public function erroredRequestsProvider() - { - return [['GET', $this->getInvalidUri()]]; + foreach ($requests as $key => $threeRequests) { + $requestList[] = [ + $threeRequests, + $erroredRequests[$key], + ]; + } + + return $requestList; } /** @@ -510,103 +330,19 @@ public function timeoutProvider() } /** - * @return CoreHttpAdapter - */ - abstract protected function createHttpAdapter(); - - /** - * @param ResponseInterface $response - * @param array $options + * @return array */ - protected function assertResponse($response, array $options = []) + public function getMethods() { - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); - - $options = array_merge($this->defaultOptions, $options); - - $this->assertSame($options['protocol_version'], $response->getProtocolVersion()); - $this->assertSame($options['status_code'], $response->getStatusCode()); - $this->assertSame($options['reason_phrase'], $response->getReasonPhrase()); - - $this->assertNotEmpty($response->getHeaders()); - - foreach ($options['headers'] as $name => $value) { - $this->assertTrue($response->hasHeader($name)); - $this->assertStringStartsWith($value, $response->getHeaderLine($name)); - } - - if ($options['body'] === null) { - $this->assertEmpty($response->getBody()->getContents()); - $this->assertEmpty((string) $response->getBody()); - } else { - $this->assertContains($options['body'], $response->getBody()->getContents()); - $this->assertContains($options['body'], (string) $response->getBody()); - } - - $parameters = []; - - if (isset($options['redirect_count'])) { - $parameters['redirect_count'] = $options['redirect_count']; - } - - if (isset($options['effective_uri'])) { - $parameters['effective_uri'] = $options['effective_uri']; - } - - // $this->assertSame($parameters, $response->getParameters()); - } - - /** - * @param string $method - * @param string[] $headers - * @param array $data - * @param array $files - * @param string $protocolVersion - */ - protected function assertRequest( - $method, - array $headers = [], - array $data = [], - array $files = [], - $protocolVersion = '1.1' - ) { - $request = $this->getRequest(); - - $this->assertSame($protocolVersion, substr($request['SERVER']['SERVER_PROTOCOL'], 5)); - $this->assertSame($method, $request['SERVER']['REQUEST_METHOD']); - - $defaultHeaders = [ - 'Connection' => 'close', - 'User-Agent' => 'PHP HTTP Adapter', - ]; - - $headers = array_merge($defaultHeaders, $headers); - - foreach ($headers as $name => $value) { - if (is_int($name)) { - list($name, $value) = explode(':', $value); - } - - $name = strtoupper(str_replace('-', '_', 'http-'.$name)); - - $this->assertArrayHasKey($name, $request['SERVER']); - $this->assertSame($value, $request['SERVER'][$name]); - } - - $inputMethods = [ + return [ + 'GET', + 'HEAD', + 'TRACE', + 'POST', 'PUT', - 'PATCH', 'DELETE', 'OPTIONS', ]; - - if (in_array($method, $inputMethods)) { - $this->assertRequestInputData($request, $data, !empty($files)); - $this->assertRequestInputFiles($request, $files); - } else { - $this->assertRequestData($request, $data); - $this->assertRequestFiles($request, $files); - } } /** @@ -621,6 +357,14 @@ private function getUri(array $query = []) : PHPUnitUtility::getUri(); } + /** + * @return array + */ + public function getProtocolVersions() + { + return ['1.1', '1.0']; + } + /** * @return string */ @@ -680,154 +424,68 @@ private function getData() } /** - * @return array + * @param ResponseInterface $response + * @param array $options */ - private function getFiles() + protected function assertResponse($response, array $options = []) { - $fixturePath = realpath(__DIR__.'/../fixture/files'); + $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); - return [ - 'file1' => [$fixturePath.'/file1.txt'], - 'file2' => [ - $fixturePath.'/file2.txt', - [$fixturePath.'/file3.txt'], - ], - ]; - } + $options = array_merge($this->defaultOptions, $options); - /** - * @param array $request - * @param array $data - */ - private function assertRequestData(array $request, array $data) - { - foreach ($data as $name => $value) { - $this->assertArrayHasKey($name, $request['POST']); - $this->assertSame($value, $request['POST'][$name]); - } - } + $this->assertSame($options['protocolVersion'], $response->getProtocolVersion()); + $this->assertSame($options['statusCode'], $response->getStatusCode()); + $this->assertSame($options['reasonPhrase'], $response->getReasonPhrase()); - /** - * @param array $request - * @param array $data - * @param boolean $multipart - */ - private function assertRequestInputData(array $request, array $data, $multipart) - { - if ($multipart) { - foreach ($data as $name => $value) { - $this->assertRequestMultipartData($request, $name, $value); - } - } else { - parse_str($request['INPUT'], $request['POST']); - $this->assertRequestData($request, $data); - } - } + $this->assertNotEmpty($response->getHeaders()); - /** - * @param array $request - * @param string $name - * @param array|string $data - */ - private function assertRequestMultipartData(array $request, $name, $data) - { - if (is_array($data)) { - foreach ($data as $subName => $subValue) { - $this->assertRequestMultipartData($request, $name.'['.$subName.']', $subValue); - } - } else { - $this->assertRegExp( - '/Content-Disposition: form-data; name="'.preg_quote($name).'"\s+'.preg_quote($data).'/', - $request['INPUT'] - ); + foreach ($options['headers'] as $name => $value) { + $this->assertTrue($response->hasHeader($name)); + $this->assertStringStartsWith($value, $response->getHeaderLine($name)); } - } - /** - * @param array $request - * @param array $files - */ - private function assertRequestFiles(array $request, array $files) - { - foreach ($files as $name => $file) { - $this->assertRequestFile($request, $name, $file); + if ($options['body'] === null) { + $this->assertEmpty($response->getBody()->getContents()); + $this->assertEmpty((string) $response->getBody()); + } else { + $this->assertContains($options['body'], $response->getBody()->getContents()); + $this->assertContains($options['body'], (string) $response->getBody()); } } /** - * @param array $request - * @param string $name - * @param string $file + * @param string $method + * @param string[] $headers + * @param string $body + * @param string $protocolVersion */ - private function assertRequestFile(array $request, $name, $file) - { - if (is_array($file)) { - foreach ($file as $subName => $subFile) { - $this->assertRequestFile($request, $name.'['.$subName.']', $subFile); - } - } else { - if (!preg_match('/^([^\[]+)/', $name, $nameMatches)) { - $this->fail(); - } - - $this->assertArrayHasKey($nameMatches[1], $request['FILES']); - - $fileRequest = $request['FILES'][$nameMatches[1]]; - $fileName = basename($file); - $fileSize = strlen(file_get_contents($file)); - $levels = preg_match_all('/\[(\d+)\]/', $name, $indexMatches) ? $indexMatches[1] : array(); + protected function assertRequest( + $method, + array $headers = [], + $body = null, + $protocolVersion = '1.1' + ) { + $request = $this->getRequest(); - $this->assertRequestPropertyFile($fileName, 'name', $fileRequest, $levels); - $this->assertRequestPropertyFile($fileSize, 'size', $fileRequest, $levels); - $this->assertRequestPropertyFile(0, 'error', $fileRequest, $levels); - } - } + $this->assertSame($protocolVersion, substr($request['SERVER']['SERVER_PROTOCOL'], 5)); + $this->assertSame($method, $request['SERVER']['REQUEST_METHOD']); - /** - * @param mixed $expected - * @param string $property - * @param array $file - * @param array $levels - */ - private function assertRequestPropertyFile($expected, $property, array $file, array $levels = []) - { - if (!empty($levels)) { - $this->assertRequestPropertyFile($expected, $levels[0], $file[$property], array_slice($levels, 1)); - } else { - $this->assertSame($expected, $file[$property]); - } - } + $defaultHeaders = [ + 'Connection' => 'close', + 'User-Agent' => 'PHP HTTP Adapter', + ]; - /** - * @param array $request - * @param array $files - */ - private function assertRequestInputFiles(array $request, array $files) - { - foreach ($files as $name => $file) { - $this->assertRequestInputFile($request, $name, $file); - } - } + $headers = array_merge($defaultHeaders, $headers); - /** - * @param array $request - * @param string $name - * @param array|string $file - */ - private function assertRequestInputFile(array $request, $name, $file) - { - if (is_array($file)) { - foreach ($file as $subName => $subFile) { - $this->assertRequestInputFile($request, $name.'['.$subName.']', $subFile); + foreach ($headers as $name => $value) { + if (is_int($name)) { + list($name, $value) = explode(':', $value); } - } else { - $namePattern = '; name="'.preg_quote($name).'"'; - $filenamePattern = '; filename=".*'.preg_quote(basename($file)).'"'; - $subPattern = '('.$namePattern.$filenamePattern.'|'.$filenamePattern.$namePattern.')'; - $pattern = '/Content-Disposition: form-data'.$subPattern.'.*'.preg_quote(file_get_contents($file)).'/sm'; + $name = strtoupper(str_replace('-', '_', 'http-'.$name)); - $this->assertRegExp($pattern, $request['INPUT']); + $this->assertArrayHasKey($name, $request['SERVER']); + $this->assertSame($value, $request['SERVER'][$name]); } } @@ -838,14 +496,6 @@ private function assertRequestInputFile(array $request, $name, $file) private function assertMultiResponses(array $responses, array $requests) { $this->assertCount(count($requests), $responses); - - foreach ($responses as $response) { - $this->assertTrue($response->hasParameter('request')); - $this->assertInstanceOf( - 'Http\Adapter\Message\InternalRequest', - $response->getParameter('request') - ); - } } /** @@ -859,7 +509,7 @@ private function assertMultiExceptions(array $exceptions, array $requests) foreach ($exceptions as $exception) { $this->assertTrue($exception->hasRequest()); $this->assertInstanceOf( - 'Http\Adapter\Message\InternalRequest', + 'Psr\Http\Message\RequestInterface', $exception->getRequest() ); } From 2ed81a4bf9ab10c131df8cb83a4f5c7bc778255f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 03:50:01 +0200 Subject: [PATCH 05/12] Fixes php errors --- src/HttpAdapterTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index 6c50a75..aa22664 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -272,7 +272,7 @@ public function requestsProvider() $request[0], $request[1], $request[3], - $request[4], + $request[4] ); } @@ -297,14 +297,14 @@ public function erroredRequestsProvider() $request[0], $request[1], $request[3], - $request[4], + $request[4] ); $erroredRequests[] = self::$messageFactory->createRequest( $request[0], $this->getInvalidUri(), $request[3], - $request[4], + $request[4] ); } From eae2f83f42c848b1410cab31b042382cd8fb67ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 03:52:49 +0200 Subject: [PATCH 06/12] Moves dev dependency --- composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index ec9cb1c..043ef3e 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,7 @@ ], "require": { "php": ">=5.4", - "phpunit/phpunit": "~4.4" - }, - "require-dev": { + "phpunit/phpunit": "~4.4", "php-http/common": "dev-master", "guzzlehttp/psr7": "~1.0", "th3n3rd/cartesian-product": "^0.2" From 4e58cddc5f294fd77ba6d4abac79fdcb19c7bfef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 03:59:20 +0200 Subject: [PATCH 07/12] Removes global usage of messageFactory from providers --- src/HttpAdapterTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index aa22664..70fe2dd 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -266,9 +266,10 @@ public function requestProvider() public function requestsProvider() { $requests = []; + $messageFactory = MessageFactoryGuesser::guess(); foreach ($this->requestProvider() as $request) { - $requests[] = self::$messageFactory->createRequest( + $requests[] = $messageFactory->createRequest( $request[0], $request[1], $request[3], @@ -287,20 +288,21 @@ public function erroredRequestsProvider() $requests = []; $erroredRequests = []; $requestList = []; + $messageFactory = MessageFactoryGuesser::guess(); foreach ($this->requestProvider() as $request) { if ($request[0] !== 'GET') { continue; } - $requests[] = self::$messageFactory->createRequest( + $requests[] = $messageFactory->createRequest( $request[0], $request[1], $request[3], $request[4] ); - $erroredRequests[] = self::$messageFactory->createRequest( + $erroredRequests[] = $messageFactory->createRequest( $request[0], $this->getInvalidUri(), $request[3], From 8c0cde74f949cd0fffd6771a4d188d9389697cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 04:30:19 +0200 Subject: [PATCH 08/12] Fixes wrong argument count --- src/HttpAdapterTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index 70fe2dd..3fc8baf 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -272,6 +272,7 @@ public function requestsProvider() $requests[] = $messageFactory->createRequest( $request[0], $request[1], + $request[2], $request[3], $request[4] ); @@ -298,6 +299,7 @@ public function erroredRequestsProvider() $requests[] = $messageFactory->createRequest( $request[0], $request[1], + $request[2], $request[3], $request[4] ); @@ -305,6 +307,7 @@ public function erroredRequestsProvider() $erroredRequests[] = $messageFactory->createRequest( $request[0], $this->getInvalidUri(), + $request[2], $request[3], $request[4] ); From 2a4df41ac8cc01637996773863e9347f5efa9377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 05:46:55 +0200 Subject: [PATCH 09/12] Finishes tests --- src/HttpAdapterTest.php | 54 ++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index 3fc8baf..0563eca 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -45,6 +45,14 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase */ protected $defaultOptions; + /** + * @var array + */ + protected $defaultHeaders = [ + 'Connection' => 'close', + 'User-Agent' => 'PHP HTTP Adapter', + ]; + /** * {@inheritdoc} */ @@ -115,10 +123,10 @@ public function testSendRequest($method, $uri, $protocolVersion, array $headers, $response, [ 'protocolVersion' => $protocolVersion, - 'body' => $method === 'HEAD' ? null : $body, + 'body' => $method === 'HEAD' ? null : 'Ok', ] ); - $this->assertRequest($method, $headers, $body); + $this->assertRequest($method, $headers, $body, $protocolVersion); } /** @@ -133,7 +141,7 @@ public function testSendRequests(array $requests) } /** - * @dataProvider erroredRequestProvider + * @dataProvider erroredRequestsProvider * @group integration */ public function testSendErroredRequests(array $requests, array $erroredRequests) @@ -153,8 +161,10 @@ public function testSendErroredRequests(array $requests, array $erroredRequests) public function testSendWithClientError() { $request = self::$messageFactory->createRequest( - 'GET', - $this->getClientErrorUri() + $method = 'GET', + $this->getClientErrorUri(), + '1.1', + $this->defaultHeaders ); $response = $this->httpAdapter->sendRequest($request); @@ -176,8 +186,10 @@ public function testSendWithClientError() public function testSendWithServerError() { $request = self::$messageFactory->createRequest( - 'GET', - $this->getServerErrorUri() + $method = 'GET', + $this->getServerErrorUri(), + '1.1', + $this->defaultHeaders ); $response = $this->httpAdapter->sendRequest($request); @@ -199,8 +211,10 @@ public function testSendWithServerError() public function testSendWithRedirect() { $request = self::$messageFactory->createRequest( - 'GET', - $this->getRedirectUri() + $method = 'GET', + $this->getRedirectUri(), + '1.1', + $this->defaultHeaders ); $response = $this->httpAdapter->sendRequest($request); @@ -225,7 +239,9 @@ public function testSendWithInvalidUri() { $request = self::$messageFactory->createRequest( 'GET', - $this->getInvalidUri() + $this->getInvalidUri(), + '1.1', + $this->defaultHeaders ); $this->httpAdapter->sendRequest($request); @@ -251,7 +267,7 @@ public function requestProvider() 'methods' => $this->getMethods(), 'uri' => [$this->getUri()], 'protocolVersions' => $this->getProtocolVersions(), - 'headers' => [[], $this->getHeaders()], + 'headers' => [[$this->defaultHeaders], [array_merge($this->defaultHeaders, $this->getHeaders())]], 'body' => [null, http_build_query($this->getData(), null, '&')], ]; @@ -266,6 +282,7 @@ public function requestProvider() public function requestsProvider() { $requests = []; + $requestList = []; $messageFactory = MessageFactoryGuesser::guess(); foreach ($this->requestProvider() as $request) { @@ -278,7 +295,13 @@ public function requestsProvider() ); } - return array_chunk($requests, 3); + $requests = array_chunk($requests, 3); + + foreach ($requests as $threeRequests) { + $requestList[] = [$threeRequests]; + } + + return $requestList; } /** @@ -367,7 +390,7 @@ private function getUri(array $query = []) */ public function getProtocolVersions() { - return ['1.1', '1.0']; + return ['1.1']; } /** @@ -417,7 +440,10 @@ private function getRedirectUri() */ private function getHeaders() { - return ['Accept-Charset' => 'utf-8', 'Accept-Language:fr']; + return [ + 'Accept-Charset' => 'utf-8', + 'Accept-Language:fr', + ]; } /** From 4e66ce319fcf8fb1028c8ab58dbf3ac82d4faa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 21:08:38 +0200 Subject: [PATCH 10/12] Improves tests --- composer.json | 2 +- src/HttpAdapterTest.php | 311 ++++++++++++++++------------------------ 2 files changed, 122 insertions(+), 191 deletions(-) diff --git a/composer.json b/composer.json index 043ef3e..b0da867 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "phpunit/phpunit": "~4.4", "php-http/common": "dev-master", "guzzlehttp/psr7": "~1.0", - "th3n3rd/cartesian-product": "^0.2" + "th3n3rd/cartesian-product": "^0.3" }, "autoload": { "psr-4": { diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index 0563eca..fa94824 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -43,7 +43,13 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase /** * @var array */ - protected $defaultOptions; + protected $defaultOptions = [ + 'protocolVersion' => '1.1', + 'statusCode' => 200, + 'reasonPhrase' => 'OK', + 'headers' => ['Content-Type' => 'text/html'], + 'body' => 'Ok', + ]; /** * @var array @@ -77,14 +83,6 @@ public static function tearDownAfterClass() */ protected function setUp() { - $this->defaultOptions = [ - 'protocolVersion' => '1.1', - 'statusCode' => 200, - 'reasonPhrase' => 'OK', - 'headers' => ['Content-Type' => 'text/html'], - 'body' => 'Ok', - ]; - $this->httpAdapter = $this->createHttpAdapter(); } @@ -107,12 +105,12 @@ abstract protected function createHttpAdapter(); * @dataProvider requestProvider * @group integration */ - public function testSendRequest($method, $uri, $protocolVersion, array $headers, $body) + public function testSendRequest($method, $uri, array $headers, $body) { $request = self::$messageFactory->createRequest( $method, $uri, - $protocolVersion, + '1.1', $headers, $body ); @@ -122,153 +120,103 @@ public function testSendRequest($method, $uri, $protocolVersion, array $headers, $this->assertResponse( $response, [ - 'protocolVersion' => $protocolVersion, - 'body' => $method === 'HEAD' ? null : 'Ok', + 'body' => $method === 'HEAD' ? null : 'Ok', ] ); - $this->assertRequest($method, $headers, $body, $protocolVersion); - } - - /** - * @dataProvider requestsProvider - * @group integration - */ - public function testSendRequests(array $requests) - { - $responses = $this->httpAdapter->sendRequests($requests); - - $this->assertMultiResponses($responses, $requests); + $this->assertRequest($method, $headers, $body, '1.1'); } /** - * @dataProvider erroredRequestsProvider + * @dataProvider requestWithOutcomeProvider * @group integration */ - public function testSendErroredRequests(array $requests, array $erroredRequests) - { - try { - $this->httpAdapter->sendRequests(array_merge($requests, $erroredRequests)); - $this->fail(); - } catch (MultiHttpAdapterException $e) { - $this->assertMultiResponses($e->getResponses(), $requests); - $this->assertMultiExceptions($e->getExceptions(), $erroredRequests); - } - } - - /** - * @group integration - */ - public function testSendWithClientError() + public function testSendRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body) { $request = self::$messageFactory->createRequest( - $method = 'GET', - $this->getClientErrorUri(), - '1.1', - $this->defaultHeaders + 'GET', + $uriAndOutcome[0], + $protocolVersion, + $headers, + $body ); $response = $this->httpAdapter->sendRequest($request); $this->assertResponse( $response, - [ - 'statusCode' => 400, - 'reasonPhrase' => 'Bad Request', - ] + $uriAndOutcome[1] ); - - $this->assertRequest($method); + $this->assertRequest($method, $headers, $body, $protocolVersion); } /** - * @group integration + * @expectedException \Http\Adapter\Exception\HttpAdapterException + * @group integration */ - public function testSendWithServerError() + public function testSendWithInvalidUri() { $request = self::$messageFactory->createRequest( - $method = 'GET', - $this->getServerErrorUri(), + 'GET', + $this->getInvalidUri(), '1.1', $this->defaultHeaders ); - $response = $this->httpAdapter->sendRequest($request); - - $this->assertResponse( - $response, - [ - 'statusCode' => 500, - 'reasonPhrase' => 'Internal Server Error', - ] - ); - - $this->assertRequest($method); + $this->httpAdapter->sendRequest($request); } /** - * @group integration + * @dataProvider requestsProvider + * @group integration */ - public function testSendWithRedirect() + public function testSendRequests(array $requests) { - $request = self::$messageFactory->createRequest( - $method = 'GET', - $this->getRedirectUri(), - '1.1', - $this->defaultHeaders - ); - - $response = $this->httpAdapter->sendRequest($request); - - $this->assertResponse( - $response, - [ - 'statusCode' => 302, - 'reasonPhrase' => 'Found', - 'body' => 'Redirect', - ] - ); + $responses = $this->httpAdapter->sendRequests($requests); - $this->assertRequest($method); + $this->assertMultiResponses($responses, $requests); } /** - * @expectedException \Http\Adapter\Exception\HttpAdapterException - * @group integration + * @dataProvider erroredRequestsProvider + * @group integration */ - public function testSendWithInvalidUri() + public function testSendErroredRequests(array $requests, array $erroredRequests) { - $request = self::$messageFactory->createRequest( - 'GET', - $this->getInvalidUri(), - '1.1', - $this->defaultHeaders - ); - - $this->httpAdapter->sendRequest($request); + try { + $this->httpAdapter->sendRequests(array_merge($requests, $erroredRequests)); + $this->fail(); + } catch (MultiHttpAdapterException $e) { + $this->assertMultiResponses($e->getResponses(), $requests); + $this->assertMultiExceptions($e->getExceptions(), $erroredRequests); + } } /** - * @dataProvider timeoutProvider - * @expectedException \Http\Adapter\Exception\HttpAdapterException - * @group integration + * @return array */ - // public function testSendWithTimeoutExceeded($timeout) - // { - // $this->httpAdapter->setOption('timeout', $timeout); - // $this->httpAdapter->send('GET', $this->getDelayUri($timeout)); - // } + public function requestProvider() + { + $sets = [ + 'methods' => $this->getMethods(), + 'uris' => [$this->getUri()], + 'headers' => $this->getHeaders(), + 'body' => $this->getBodies(), + ]; + + $cartesianProduct = new CartesianProduct($sets); + + return $cartesianProduct->compute(); + } /** * @return array */ - public function requestProvider() + public function requestWithOutcomeProvider() { $sets = [ - 'methods' => $this->getMethods(), - 'uri' => [$this->getUri()], - 'protocolVersions' => $this->getProtocolVersions(), - 'headers' => [[$this->defaultHeaders], [array_merge($this->defaultHeaders, $this->getHeaders())]], - 'body' => [null, http_build_query($this->getData(), null, '&')], + 'uriAndOutcome' => $this->getUrisAndOutcomes(), + 'headers' => $this->getHeaders(), + 'body' => $this->getBodies(), ]; $cartesianProduct = new CartesianProduct($sets); @@ -281,27 +229,20 @@ public function requestProvider() */ public function requestsProvider() { - $requests = []; - $requestList = []; + $requests = $this->requestProvider(); $messageFactory = MessageFactoryGuesser::guess(); - foreach ($this->requestProvider() as $request) { - $requests[] = $messageFactory->createRequest( + foreach ($requests as &$request) { + $request = $messageFactory->createRequest( $request[0], $request[1], + '1.1', $request[2], - $request[3], - $request[4] + $request[3] ); } - $requests = array_chunk($requests, 3); - - foreach ($requests as $threeRequests) { - $requestList[] = [$threeRequests]; - } - - return $requestList; + return [[$requests]]; } /** @@ -311,56 +252,35 @@ public function erroredRequestsProvider() { $requests = []; $erroredRequests = []; - $requestList = []; $messageFactory = MessageFactoryGuesser::guess(); - foreach ($this->requestProvider() as $request) { - if ($request[0] !== 'GET') { - continue; - } + $sets = [ + 'methods' => ['GET'], + 'uris' => [$this->getUri(), $this->getInvalidUri()], + 'headers' => $this->getHeaders(), + 'body' => $this->getBodies(), + ]; + + $cartesianProduct = new CartesianProduct($sets); + foreach ($cartesianProduct as $request) { $requests[] = $messageFactory->createRequest( $request[0], $request[1], + '1.1', $request[2], - $request[3], - $request[4] - ); - - $erroredRequests[] = $messageFactory->createRequest( - $request[0], - $this->getInvalidUri(), - $request[2], - $request[3], - $request[4] + $request[3] ); } - $requests = array_chunk($requests, 3); - $erroredRequests = array_chunk($erroredRequests, 3); - - foreach ($requests as $key => $threeRequests) { - $requestList[] = [ - $threeRequests, - $erroredRequests[$key], - ]; - } - - return $requestList; - } - - /** - * @return array - */ - public function timeoutProvider() - { - return [[0.5], [1]]; + // First x are simple requests, all-x are errored requests + return [[array_chunk($requests, count($requests)/2)]]; } /** * @return array */ - public function getMethods() + private function getMethods() { return [ 'GET', @@ -385,14 +305,6 @@ private function getUri(array $query = []) : PHPUnitUtility::getUri(); } - /** - * @return array - */ - public function getProtocolVersions() - { - return ['1.1']; - } - /** * @return string */ @@ -402,47 +314,66 @@ private function getInvalidUri() } /** - * @return string - */ - private function getClientErrorUri() - { - return $this->getUri(['client_error' => true]); - } - - /** - * @return string + * @return array */ - private function getServerErrorUri() + private function getUrisAndOutcomes() { - return $this->getUri(['server_error' => true]); + return [ + [ + $this->getUri(['client_error' => true]), + [ + 'statusCode' => 400, + 'reasonPhrase' => 'Bad Request', + ], + ], + [ + $this->getUri(['server_error' => true]), + [ + 'statusCode' => 500, + 'reasonPhrase' => 'Internal Server Error', + ], + ], + [ + $this->getUri(['redirect' => true]), + [ + 'statusCode' => 302, + 'reasonPhrase' => 'Found', + 'body' => 'Redirect', + ], + ], + ]; } /** - * @param float $delay - * - * @return string + * @return array */ - private function getDelayUri($delay = 1.0) + private function getProtocolVersions() { - return $this->getUri(['delay' => $delay + 0.01]); + return ['1.1', '1.0']; } /** - * @return string + * @return string[] */ - private function getRedirectUri() + private function getHeaders() { - return $this->getUri(['redirect' => true]); + return [ + $this->defaultHeaders, + array_merge($this->defaultHeaders, [ + 'Accept-Charset' => 'utf-8', + 'Accept-Language:fr', + ]), + ]; } /** - * @return string[] + * @return array */ - private function getHeaders() + private function getBodies() { return [ - 'Accept-Charset' => 'utf-8', - 'Accept-Language:fr', + null, + http_build_query($this->getData(), null, '&'), ]; } From a73ad4b871ae7e44f6ea8b584ea32bda0d188f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Sat, 30 May 2015 21:25:36 +0200 Subject: [PATCH 11/12] Fixes errors --- src/HttpAdapterTest.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index fa94824..0a562bc 100644 --- a/src/HttpAdapterTest.php +++ b/src/HttpAdapterTest.php @@ -132,8 +132,12 @@ public function testSendRequest($method, $uri, array $headers, $body) */ public function testSendRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body) { + if ($protocolVersion === '1.0') { + $body = null; + } + $request = self::$messageFactory->createRequest( - 'GET', + $method = 'GET', $uriAndOutcome[0], $protocolVersion, $headers, @@ -142,9 +146,12 @@ public function testSendRequestWithOutcome($uriAndOutcome, $protocolVersion, arr $response = $this->httpAdapter->sendRequest($request); + $outcome = $uriAndOutcome[1]; + $outcome['protocolVersion'] = $protocolVersion; + $this->assertResponse( $response, - $uriAndOutcome[1] + $outcome ); $this->assertRequest($method, $headers, $body, $protocolVersion); } @@ -214,9 +221,10 @@ public function requestProvider() public function requestWithOutcomeProvider() { $sets = [ - 'uriAndOutcome' => $this->getUrisAndOutcomes(), - 'headers' => $this->getHeaders(), - 'body' => $this->getBodies(), + 'urisAndOutcomes' => $this->getUrisAndOutcomes(), + 'protocolVersions' => $this->getProtocolVersions(), + 'headers' => $this->getHeaders(), + 'body' => $this->getBodies(), ]; $cartesianProduct = new CartesianProduct($sets); @@ -274,7 +282,7 @@ public function erroredRequestsProvider() } // First x are simple requests, all-x are errored requests - return [[array_chunk($requests, count($requests)/2)]]; + return [array_chunk($requests, count($requests)/2)]; } /** @@ -357,12 +365,13 @@ private function getProtocolVersions() */ private function getHeaders() { + $headers = $this->defaultHeaders; + $headers['Accept-Charset'] = 'utf-8'; + $headers['Accept-Language'] = 'en'; + return [ $this->defaultHeaders, - array_merge($this->defaultHeaders, [ - 'Accept-Charset' => 'utf-8', - 'Accept-Language:fr', - ]), + $headers, ]; } From f5393f6367cdb10cc2133c0fba05c1eb2539a90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1gi-Kaz=C3=A1r=20M=C3=A1rk?= Date: Tue, 2 Jun 2015 15:40:46 +0200 Subject: [PATCH 12/12] Updates dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b0da867..62e2a9f 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": ">=5.4", "phpunit/phpunit": "~4.4", - "php-http/common": "dev-master", + "php-http/message-factory-guesser": "^0.1@dev", "guzzlehttp/psr7": "~1.0", "th3n3rd/cartesian-product": "^0.3" },