diff --git a/composer.json b/composer.json index b7df41c..62e2a9f 100644 --- a/composer.json +++ b/composer.json @@ -12,10 +12,10 @@ ], "require": { "php": ">=5.4", - "phpunit/phpunit": "~4.4" - }, - "require-dev": { - "php-http/adapter-core": "dev-initial_import" + "phpunit/phpunit": "~4.4", + "php-http/message-factory-guesser": "^0.1@dev", + "guzzlehttp/psr7": "~1.0", + "th3n3rd/cartesian-product": "^0.3" }, "autoload": { "psr-4": { diff --git a/src/HttpAdapterTest.php b/src/HttpAdapterTest.php index a915cf6..0a562bc 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\Message\RequestInterface; -use Http\Adapter\Message\ResponseInterface; -use Http\Adapter\MultiHttpAdapterException; +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; /** * @author GeLo @@ -28,14 +31,33 @@ abstract class HttpAdapterTest extends \PHPUnit_Framework_TestCase private static $logPath; /** - * @var CoreHttpAdapter + * @var MessageFactory + */ + protected static $messageFactory; + + /** + * @var HttpAdapter */ protected $httpAdapter; /** * @var array */ - protected $defaultOptions; + protected $defaultOptions = [ + 'protocolVersion' => '1.1', + 'statusCode' => 200, + 'reasonPhrase' => 'OK', + 'headers' => ['Content-Type' => 'text/html'], + 'body' => 'Ok', + ]; + + /** + * @var array + */ + protected $defaultHeaders = [ + 'Connection' => 'close', + 'User-Agent' => 'PHP HTTP Adapter', + ]; /** * {@inheritdoc} @@ -43,6 +65,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(); } /** @@ -60,14 +83,6 @@ public static function tearDownAfterClass() */ protected function setUp() { - $this->defaultOptions = [ - 'protocol_version' => RequestInterface::PROTOCOL_VERSION_1_1, - 'status_code' => 200, - 'reason_phrase' => 'OK', - 'headers' => ['Content-Type' => 'text/html'], - 'body' => 'Ok', - ]; - $this->httpAdapter = $this->createHttpAdapter(); } @@ -82,146 +97,98 @@ protected function tearDown() abstract public function testGetName(); /** - * @dataProvider simpleProvider - * @group integration + * @return HttpAdapter */ - public function testGet($uri, array $headers = []) - { - $this->assertResponse($this->httpAdapter->get($uri, $headers)); - $this->assertRequest(RequestInterface::METHOD_GET, $headers); - } - - /** - * @dataProvider simpleProvider - * @group integration - */ - public function testHead($uri, array $headers = []) - { - $this->assertResponse($this->httpAdapter->head($uri, $headers), ['body' => null]); - $this->assertRequest(RequestInterface::METHOD_HEAD, $headers); - } - - /** - * @dataProvider simpleProvider - * @group integration - */ - public function testTrace($uri, array $headers = []) - { - $this->assertResponse($this->httpAdapter->trace($uri, $headers)); - $this->assertRequest(RequestInterface::METHOD_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(RequestInterface::METHOD_POST, $headers, $data, $files); - } + abstract protected function createHttpAdapter(); /** - * @dataProvider fullProvider - * @group integration + * @dataProvider requestProvider + * @group integration */ - public function testPut($uri, array $headers = [], array $data = [], array $files = []) + public function testSendRequest($method, $uri, array $headers, $body) { - $this->assertResponse($this->httpAdapter->put($uri, $headers, $data, $files)); - $this->assertRequest(RequestInterface::METHOD_PUT, $headers, $data, $files); - } + $request = self::$messageFactory->createRequest( + $method, + $uri, + '1.1', + $headers, + $body + ); - /** - * @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(RequestInterface::METHOD_PATCH, $headers, $data, $files); - } + $response = $this->httpAdapter->sendRequest($request); - /** - * @dataProvider fullProvider - * @group integration - */ - 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->assertResponse( + $response, + [ + 'body' => $method === 'HEAD' ? null : 'Ok', + ] + ); + $this->assertRequest($method, $headers, $body, '1.1'); } /** - * @dataProvider fullProvider - * @group integration + * @dataProvider requestWithOutcomeProvider + * @group integration */ - public function testOptions($uri, array $headers = [], array $data = [], array $files = []) + public function testSendRequestWithOutcome($uriAndOutcome, $protocolVersion, array $headers, $body) { - $this->assertResponse($this->httpAdapter->options($uri, $headers, $data, $files)); - $this->assertRequest(RequestInterface::METHOD_OPTIONS, $headers, $data, $files); - } + if ($protocolVersion === '1.0') { + $body = null; + } - /** - * @dataProvider requestProvider - * @group integration - */ - public function testSendRequest($uri, $method, array $headers = [], array $data = []) - { - $request = $this->httpAdapter->getConfiguration()->getMessageFactory()->createRequest( - $method, - $uri, - RequestInterface::PROTOCOL_VERSION_1_1, + $request = self::$messageFactory->createRequest( + $method = 'GET', + $uriAndOutcome[0], + $protocolVersion, $headers, - http_build_query($data, null, '&') + $body ); + $response = $this->httpAdapter->sendRequest($request); + + $outcome = $uriAndOutcome[1]; + $outcome['protocolVersion'] = $protocolVersion; + $this->assertResponse( - $this->httpAdapter->sendRequest($request), - $method === RequestInterface::METHOD_HEAD ? ['body' => null] : [] + $response, + $outcome ); - - $this->assertRequest($method, $headers, $data); + $this->assertRequest($method, $headers, $body, $protocolVersion); } /** - * @dataProvider internalRequestProvider - * @group integration + * @expectedException \Http\Adapter\Exception\HttpAdapterException + * @group integration */ - public function testSendInternalRequest($uri, $method, array $headers = [], array $data = [], array $files = []) + public function testSendWithInvalidUri() { - $request = $this->httpAdapter->getConfiguration()->getMessageFactory()->createInternalRequest( - $method, - $uri, - RequestInterface::PROTOCOL_VERSION_1_1, - $headers, - $data, - $files - ); - - $this->assertResponse( - $this->httpAdapter->sendRequest($request), - $method === RequestInterface::METHOD_HEAD ? ['body' => null] : [] + $request = self::$messageFactory->createRequest( + 'GET', + $this->getInvalidUri(), + '1.1', + $this->defaultHeaders ); - $this->assertRequest($method, $headers, $data, $files); + $this->httpAdapter->sendRequest($request); } /** - * @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 erroredRequestsProvider + * @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(); @@ -232,223 +199,155 @@ public function testSendErroredRequests() } /** - * @group integration + * @return array */ - public function testSendWithCustomArgSeparatorOutput() + public function requestProvider() { - $argSeparatorOutput = ini_get('arg_separator.output'); - ini_set('arg_separator.output', '&'); - - $this->assertResponse($this->httpAdapter->post( - $this->getUri(), - $headers = $this->getHeaders(), - $data = $this->getData() - )); + $sets = [ + 'methods' => $this->getMethods(), + 'uris' => [$this->getUri()], + 'headers' => $this->getHeaders(), + 'body' => $this->getBodies(), + ]; - $this->assertRequest(RequestInterface::METHOD_POST, $headers, $data); + $cartesianProduct = new CartesianProduct($sets); - ini_set('arg_separator.output', $argSeparatorOutput); + return $cartesianProduct->compute(); } /** - * @group integration + * @return array */ - public function testSendWithProtocolVersion10() + public function requestWithOutcomeProvider() { - $this->httpAdapter->getConfiguration()->setProtocolVersion( - $protocolVersion = RequestInterface::PROTOCOL_VERSION_1_0 - ); + $sets = [ + 'urisAndOutcomes' => $this->getUrisAndOutcomes(), + 'protocolVersions' => $this->getProtocolVersions(), + 'headers' => $this->getHeaders(), + 'body' => $this->getBodies(), + ]; - $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $this->getUri()), - ['protocol_version' => $protocolVersion] - ); + $cartesianProduct = new CartesianProduct($sets); - $this->assertRequest($method, [], [], [], $protocolVersion); + return $cartesianProduct->compute(); } /** - * @group integration + * @return array */ - public function testSendWithUserAgent() + public function requestsProvider() { - $this->httpAdapter->getConfiguration()->setUserAgent($userAgent = 'foo'); - - $this->assertResponse($this->httpAdapter->send($method = RequestInterface::METHOD_GET, $this->getUri())); - $this->assertRequest($method, ['User-Agent' => $userAgent]); - } + $requests = $this->requestProvider(); + $messageFactory = MessageFactoryGuesser::guess(); - /** - * @group integration - */ - public function testSendWithClientError() - { - $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $uri = $this->getClientErrorUri()), - [ - 'status_code' => 400, - 'reason_phrase' => 'Bad Request', - ] - ); - - $this->assertRequest($method); - } - - /** - * @group integration - */ - public function testSendWithServerError() - { - $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $uri = $this->getServerErrorUri()), - [ - 'status_code' => 500, - 'reason_phrase' => 'Internal Server Error', - ] - ); + foreach ($requests as &$request) { + $request = $messageFactory->createRequest( + $request[0], + $request[1], + '1.1', + $request[2], + $request[3] + ); + } - $this->assertRequest($method); + return [[$requests]]; } /** - * @group integration + * @return array */ - public function testSendWithRedirect() + public function erroredRequestsProvider() { - $this->assertResponse( - $this->httpAdapter->send($method = RequestInterface::METHOD_GET, $uri = $this->getRedirectUri()), - [ - 'status_code' => 302, - 'reason_phrase' => 'Found', - 'body' => 'Redirect', - ] - ); + $requests = []; + $erroredRequests = []; + $messageFactory = MessageFactoryGuesser::guess(); + + $sets = [ + 'methods' => ['GET'], + 'uris' => [$this->getUri(), $this->getInvalidUri()], + 'headers' => $this->getHeaders(), + 'body' => $this->getBodies(), + ]; - $this->assertRequest($method); - } + $cartesianProduct = new CartesianProduct($sets); - /** - * @expectedException \Http\Adapter\HttpAdapterException - * @group integration - */ - public function testSendWithInvalidUri() - { - $this->httpAdapter->send(RequestInterface::METHOD_GET, $this->getInvalidUri()); - } + foreach ($cartesianProduct as $request) { + $requests[] = $messageFactory->createRequest( + $request[0], + $request[1], + '1.1', + $request[2], + $request[3] + ); + } - /** - * @dataProvider timeoutProvider - * @expectedException \Http\Adapter\HttpAdapterException - * @group integration - */ - public function testSendWithTimeoutExceeded($timeout) - { - $this->httpAdapter->getConfiguration()->setTimeout($timeout); - $this->httpAdapter->send(RequestInterface::METHOD_GET, $this->getDelayUri($timeout)); + // First x are simple requests, all-x are errored requests + return [array_chunk($requests, count($requests)/2)]; } /** * @return array */ - public function simpleProvider() + private function getMethods() { return [ - [$this->getUri()], - [$this->getUri(), $this->getHeaders()], + 'GET', + 'HEAD', + 'TRACE', + 'POST', + 'PUT', + 'DELETE', + 'OPTIONS', ]; } /** - * @return array + * @param string[] $query + * + * @return string|null */ - public function fullProvider() + private function getUri(array $query = []) { - return array_merge( - $this->simpleProvider(), - [ - [$this->getUri(), $this->getHeaders(), $this->getData()], - [$this->getUri(), $this->getHeaders(), $this->getData(), $this->getFiles()], - ] - ); + return !empty($query) + ? PHPUnitUtility::getUri().'?'.http_build_query($query, null, '&') + : PHPUnitUtility::getUri(); } /** - * @return array + * @return string */ - public function requestProvider() + private function getInvalidUri() { - $requests = []; - - foreach ($this->internalRequestProvider() as $request) { - if (!isset($request[4])) { - $requests[] = $request; - } - } - - return $requests; + return 'http://invalid.php-http.org'; } /** * @return array */ - public function internalRequestProvider() + private function getUrisAndOutcomes() { 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(), - RequestInterface::METHOD_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(), - RequestInterface::METHOD_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(), - RequestInterface::METHOD_PATCH, - $this->getHeaders(), - $this->getData(), - $this->getFiles(), + $this->getUri(['client_error' => true]), + [ + 'statusCode' => 400, + 'reasonPhrase' => 'Bad Request', + ], ], - [$this->getUri(), RequestInterface::METHOD_DELETE], - [$this->getUri(), RequestInterface::METHOD_DELETE, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_DELETE, $this->getHeaders(), $this->getData()], [ - $this->getUri(), - RequestInterface::METHOD_DELETE, - $this->getHeaders(), - $this->getData(), - $this->getFiles(), + $this->getUri(['server_error' => true]), + [ + 'statusCode' => 500, + 'reasonPhrase' => 'Internal Server Error', + ], ], - [$this->getUri(), RequestInterface::METHOD_OPTIONS], - [$this->getUri(), RequestInterface::METHOD_OPTIONS, $this->getHeaders()], - [$this->getUri(), RequestInterface::METHOD_OPTIONS, $this->getHeaders(), $this->getData()], [ - $this->getUri(), - RequestInterface::METHOD_OPTIONS, - $this->getHeaders(), - $this->getData(), - $this->getFiles(), + $this->getUri(['redirect' => true]), + [ + 'statusCode' => 302, + 'reasonPhrase' => 'Found', + 'body' => 'Redirect', + ], ], ]; } @@ -456,65 +355,44 @@ public function internalRequestProvider() /** * @return array */ - public function requestsProvider() + private function getProtocolVersions() { - $requests = [[RequestInterface::METHOD_GET, $this->getUri()]]; - - foreach ($this->requestProvider() as $request) { - $requests[] = [ - $request[1], - $request[0], - RequestInterface::PROTOCOL_VERSION_1_1, - isset($request[2]) ? $request[2] : [], - isset($request[3]) ? $request[3] : [], - isset($request[4]) ? $request[4] : [], - ]; - } - - foreach ($this->requestProvider() as $request) { - $requests[] = $this->httpAdapter->getConfiguration()->getMessageFactory()->createRequest( - $request[1], - $request[0], - RequestInterface::PROTOCOL_VERSION_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( - $request[1], - $request[0], - RequestInterface::PROTOCOL_VERSION_1_1, - isset($request[2]) ? $request[2] : [], - isset($request[3]) ? $request[3] : [], - isset($request[4]) ? $request[4] : [] - ); - } - - return $requests; + return ['1.1', '1.0']; } /** - * @return array + * @return string[] */ - public function erroredRequestsProvider() + private function getHeaders() { - return [[RequestInterface::METHOD_GET, $this->getInvalidUri()]]; + $headers = $this->defaultHeaders; + $headers['Accept-Charset'] = 'utf-8'; + $headers['Accept-Language'] = 'en'; + + return [ + $this->defaultHeaders, + $headers, + ]; } /** * @return array */ - public function timeoutProvider() + private function getBodies() { - return [[0.5], [1]]; + return [ + null, + http_build_query($this->getData(), null, '&'), + ]; } /** - * @return CoreHttpAdapter + * @return array */ - abstract protected function createHttpAdapter(); + private function getData() + { + return ['param1' => 'foo', 'param2' => ['bar', ['baz']]]; + } /** * @param ResponseInterface $response @@ -522,13 +400,13 @@ 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); - $this->assertSame($options['protocol_version'], $response->getProtocolVersion()); - $this->assertSame($options['status_code'], $response->getStatusCode()); - $this->assertSame($options['reason_phrase'], $response->getReasonPhrase()); + $this->assertSame($options['protocolVersion'], $response->getProtocolVersion()); + $this->assertSame($options['statusCode'], $response->getStatusCode()); + $this->assertSame($options['reasonPhrase'], $response->getReasonPhrase()); $this->assertNotEmpty($response->getHeaders()); @@ -544,33 +422,19 @@ protected function assertResponse($response, array $options = []) $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 $body * @param string $protocolVersion */ protected function assertRequest( $method, array $headers = [], - array $data = [], - array $files = [], - $protocolVersion = RequestInterface::PROTOCOL_VERSION_1_1 + $body = null, + $protocolVersion = '1.1' ) { $request = $this->getRequest(); @@ -579,7 +443,7 @@ protected function assertRequest( $defaultHeaders = [ 'Connection' => 'close', - 'User-Agent' => 'PHP Http Adapter', + 'User-Agent' => 'PHP HTTP Adapter', ]; $headers = array_merge($defaultHeaders, $headers); @@ -594,243 +458,6 @@ protected function assertRequest( $this->assertArrayHasKey($name, $request['SERVER']); $this->assertSame($value, $request['SERVER'][$name]); } - - $inputMethods = [ - RequestInterface::METHOD_PUT, - RequestInterface::METHOD_PATCH, - RequestInterface::METHOD_DELETE, - RequestInterface::METHOD_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); - } - } - - /** - * @param string[] $query - * - * @return string|null - */ - private function getUri(array $query = []) - { - return !empty($query) - ? PHPUnitUtility::getUri().'?'.http_build_query($query, null, '&') - : PHPUnitUtility::getUri(); - } - - /** - * @return string - */ - private function getInvalidUri() - { - return 'http://invalid.php-http.org'; - } - - /** - * @return string - */ - private function getClientErrorUri() - { - return $this->getUri(['client_error' => true]); - } - - /** - * @return string - */ - private function getServerErrorUri() - { - return $this->getUri(['server_error' => true]); - } - - /** - * @param float $delay - * - * @return string - */ - private function getDelayUri($delay = 1.0) - { - return $this->getUri(['delay' => $delay + 0.01]); - } - - /** - * @return string - */ - private function getRedirectUri() - { - return $this->getUri(['redirect' => true]); - } - - /** - * @return string[] - */ - private function getHeaders() - { - return ['Accept-Charset' => 'utf-8', 'Accept-Language:fr']; - } - - /** - * @return array - */ - private function getData() - { - return ['param1' => 'foo', 'param2' => ['bar', ['baz']]]; - } - - /** - * @return array - */ - private function getFiles() - { - $fixturePath = realpath(__DIR__.'/../fixture/files'); - - return [ - 'file1' => [$fixturePath.'/file1.txt'], - 'file2' => [ - $fixturePath.'/file2.txt', - [$fixturePath.'/file3.txt'], - ], - ]; - } - - /** - * @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]); - } - } - - /** - * @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); - } - } - - /** - * @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'] - ); - } - } - - /** - * @param array $request - * @param array $files - */ - private function assertRequestFiles(array $request, array $files) - { - foreach ($files as $name => $file) { - $this->assertRequestFile($request, $name, $file); - } - } - - /** - * @param array $request - * @param string $name - * @param string $file - */ - 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(); - - $this->assertRequestPropertyFile($fileName, 'name', $fileRequest, $levels); - $this->assertRequestPropertyFile($fileSize, 'size', $fileRequest, $levels); - $this->assertRequestPropertyFile(0, 'error', $fileRequest, $levels); - } - } - - /** - * @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]); - } - } - - /** - * @param array $request - * @param array $files - */ - private function assertRequestInputFiles(array $request, array $files) - { - foreach ($files as $name => $file) { - $this->assertRequestInputFile($request, $name, $file); - } - } - - /** - * @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); - } - } 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'; - - $this->assertRegExp($pattern, $request['INPUT']); - } } /** @@ -840,14 +467,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\InternalRequestInterface', - $response->getParameter('request') - ); - } } /** @@ -861,7 +480,7 @@ private function assertMultiExceptions(array $exceptions, array $requests) foreach ($exceptions as $exception) { $this->assertTrue($exception->hasRequest()); $this->assertInstanceOf( - 'Http\Adapter\Message\InternalRequestInterface', + 'Psr\Http\Message\RequestInterface', $exception->getRequest() ); }