diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..efc11656 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM composer:latest as setup + +RUN mkdir /guzzle + +WORKDIR /guzzle + +RUN set -xe \ + && composer init --name=guzzlehttp/test --description="Simple project for testing Guzzle scripts" --author="Márk Sági-Kazár " --no-interaction \ + && composer require guzzlehttp/guzzle + + +FROM php:7.2 + +RUN mkdir /guzzle + +WORKDIR /guzzle + +COPY --from=setup /guzzle /guzzle diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 16be901c..eb6d6983 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -184,10 +184,10 @@ requests. 'webp' => $client->getAsync('/image/webp') ]; - // Wait on all of the requests to complete. Throws a ConnectException + // Wait on all of the requests to complete. Throws a ConnectException // if any of the requests fail $results = Promise\unwrap($promises); - + // Wait for the requests to complete, even if some of them fail $results = Promise\settle($promises)->wait(); @@ -229,7 +229,7 @@ amount of requests you wish to send. // Force the pool of requests to complete. $promise->wait(); - + Or using a closure that will return a promise once the pool calls the closure. .. code-block:: php @@ -246,7 +246,7 @@ Or using a closure that will return a promise once the pool calls the closure. }; $pool = new Pool($client, $requests(100)); - + Using Responses =============== @@ -447,6 +447,43 @@ to use a shared cookie jar for all requests. $client = new \GuzzleHttp\Client(['cookies' => true]); $r = $client->request('GET', 'http://httpbin.org/cookies'); +Different implementations exist for the ``GuzzleHttp\Cookie\CookieJarInterface`` +: + +- The ``GuzzleHttp\Cookie\CookieJar`` class stores cookies as an array. +- The ``GuzzleHttp\Cookie\FileCookieJar`` class persists non-session cookies + using a JSON formatted file. +- The ``GuzzleHttp\Cookie\SessionCookieJar`` class persists cookies in the + client session. + +You can manually set cookies into a cookie jar with the named constructor +``fromArray(array $cookies, $domain)``. + +.. code-block:: php + + $jar = \GuzzleHttp\Cookie\CookieJar::fromArray( + [ + 'some_cookie' => 'foo', + 'other_cookie' => 'barbaz1234' + ], + 'example.org' + ); + +You can get a cookie by its name with the ``getCookieByName($name)`` method +which returns a ``GuzzleHttp\Cookie\SetCookie`` instance. + +.. code-block:: php + + $cookie = $jar->getCookieByName('some_cookie'); + + $cookie->getValue(); // 'foo' + $cookie->getDomain(); // 'example.org' + $cookie->getExpires(); // expiration date as a Unix timestamp + +The cookies can be also fetched into an array thanks to the `toArray()` method. +The ``GuzzleHttp\Cookie\CookieJarInterface`` interface extends +``Traversable`` so it can be iterated in a foreach loop. + Redirects ========= @@ -499,7 +536,7 @@ on each other. │ └── ClientException ├── ConnectionException └── TooManyRedirectsException - + Guzzle throws exceptions for errors that occur during a transfer. - In the event of a networking error (connection timeout, DNS errors, etc.), @@ -568,7 +605,7 @@ behavior of the library. the timeout. ``HTTP_PROXY`` Defines the proxy to use when sending requests using the "http" protocol. - + Note: because the HTTP_PROXY variable may contain arbitrary user input on some (CGI) environments, the variable is only used on the CLI SAPI. See https://httpoxy.org for more information. ``HTTPS_PROXY`` Defines the proxy to use when sending requests using the "https" protocol. diff --git a/docs/testing.rst b/docs/testing.rst index 49d42f13..5ac06691 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -34,7 +34,7 @@ a response or exception by shifting return values off of a queue. $mock = new MockHandler([ new Response(200, ['X-Foo' => 'Bar']), new Response(202, ['Content-Length' => 0]), - new RequestException("Error Communicating with Server", new Request('GET', 'test')) + new RequestException('Error Communicating with Server', new Request('GET', 'test')) ]); $handlerStack = HandlerStack::create($mock); diff --git a/src/Cookie/CookieJar.php b/src/Cookie/CookieJar.php index 78f2b79f..28626426 100644 --- a/src/Cookie/CookieJar.php +++ b/src/Cookie/CookieJar.php @@ -120,7 +120,7 @@ class CookieJar implements CookieJarInterface } elseif (!$path) { $this->cookies = array_filter( $this->cookies, - function (SetCookie $cookie) use ($path, $domain) { + function (SetCookie $cookie) use ($domain) { return !$cookie->matchesDomain($domain); } ); diff --git a/src/Handler/StreamHandler.php b/src/Handler/StreamHandler.php index 8e9bc889..741e02d2 100644 --- a/src/Handler/StreamHandler.php +++ b/src/Handler/StreamHandler.php @@ -42,7 +42,7 @@ class StreamHandler // Append a content-length header if body size is zero to match // cURL's behavior. if (0 === $request->getBody()->getSize()) { - $request = $request->withHeader('Content-Length', 0); + $request = $request->withHeader('Content-Length', '0'); } return $this->createResponse( diff --git a/src/HandlerStack.php b/src/HandlerStack.php index 24c46fd9..f0016861 100644 --- a/src/HandlerStack.php +++ b/src/HandlerStack.php @@ -206,7 +206,7 @@ class HandlerStack } /** - * @param $name + * @param string $name * @return int */ private function findByName($name) @@ -223,10 +223,10 @@ class HandlerStack /** * Splices a function into the middleware list at a specific position. * - * @param $findName - * @param $withName + * @param string $findName + * @param string $withName * @param callable $middleware - * @param $before + * @param bool $before */ private function splice($findName, $withName, callable $middleware, $before) { diff --git a/src/Middleware.php b/src/Middleware.php index d4ad75c9..2d762a01 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -58,7 +58,7 @@ final class Middleware return $handler($request, $options); } return $handler($request, $options)->then( - function (ResponseInterface $response) use ($request, $handler) { + function (ResponseInterface $response) use ($request) { $code = $response->getStatusCode(); if ($code < 400) { return $response; diff --git a/src/RetryMiddleware.php b/src/RetryMiddleware.php index f27090fd..7d40ecaf 100644 --- a/src/RetryMiddleware.php +++ b/src/RetryMiddleware.php @@ -19,6 +19,9 @@ class RetryMiddleware /** @var callable */ private $decider; + /** @var callable */ + private $delay; + /** * @param callable $decider Function that accepts the number of retries, * a request, [response], and [exception] and @@ -42,7 +45,7 @@ class RetryMiddleware /** * Default exponential backoff delay function. * - * @param $retries + * @param int $retries * * @return int */ diff --git a/tests/Cookie/FileCookieJarTest.php b/tests/Cookie/FileCookieJarTest.php index af378f40..f93ab37f 100644 --- a/tests/Cookie/FileCookieJarTest.php +++ b/tests/Cookie/FileCookieJarTest.php @@ -34,7 +34,7 @@ class FileCookieJarTest extends TestCase } /** - * @dataProvider testPersistsToFileFileParameters + * @dataProvider providerPersistsToFileFileParameters */ public function testPersistsToFile($testSaveSessionCookie = false) { @@ -78,7 +78,7 @@ class FileCookieJarTest extends TestCase unlink($this->file); } - public function testPersistsToFileFileParameters() + public function providerPersistsToFileFileParameters() { return array( array(false), diff --git a/tests/Cookie/SessionCookieJarTest.php b/tests/Cookie/SessionCookieJarTest.php index 8d6024a5..20bea844 100644 --- a/tests/Cookie/SessionCookieJarTest.php +++ b/tests/Cookie/SessionCookieJarTest.php @@ -38,7 +38,7 @@ class SessionCookieJarTest extends TestCase } /** - * @dataProvider testPersistsToSessionParameters + * @dataProvider providerPersistsToSessionParameters */ public function testPersistsToSession($testSaveSessionCookie = false) { @@ -82,7 +82,7 @@ class SessionCookieJarTest extends TestCase unset($_SESSION[$this->sessionVar]); } - public function testPersistsToSessionParameters() + public function providerPersistsToSessionParameters() { return array( array(false), diff --git a/tests/Handler/CurlHandlerTest.php b/tests/Handler/CurlHandlerTest.php index 8c380fa3..25c49902 100644 --- a/tests/Handler/CurlHandlerTest.php +++ b/tests/Handler/CurlHandlerTest.php @@ -37,8 +37,8 @@ class CurlHandlerTest extends TestCase Server::enqueue([$response, $response]); $a = new CurlHandler(); $request = new Request('GET', Server::$url); - $a($request, []); - $a($request, []); + $this->assertInstanceOf('GuzzleHttp\Promise\FulfilledPromise', $a($request, [])); + $this->assertInstanceOf('GuzzleHttp\Promise\FulfilledPromise', $a($request, [])); } public function testDoesSleep() diff --git a/tests/Handler/CurlMultiHandlerTest.php b/tests/Handler/CurlMultiHandlerTest.php index e9b14065..945ea899 100644 --- a/tests/Handler/CurlMultiHandlerTest.php +++ b/tests/Handler/CurlMultiHandlerTest.php @@ -46,6 +46,10 @@ class CurlMultiHandlerTest extends TestCase $response->cancel(); $responses[] = $response; } + + foreach($responses as $r) { + $this->assertEquals('rejected', $response->getState()); + } } public function testCannotCancelFinished() @@ -56,6 +60,7 @@ class CurlMultiHandlerTest extends TestCase $response = $a(new Request('GET', Server::$url), []); $response->wait(); $response->cancel(); + $this->assertEquals('fulfilled', $response->getState()); } public function testDelaysConcurrently() diff --git a/tests/Handler/StreamHandlerTest.php b/tests/Handler/StreamHandlerTest.php index 208c4889..0c97e682 100644 --- a/tests/Handler/StreamHandlerTest.php +++ b/tests/Handler/StreamHandlerTest.php @@ -307,7 +307,8 @@ class StreamHandlerTest extends TestCase public function testVerifyCanBeDisabled() { - $this->getSendResult(['verify' => false]); + $handler = $this->getSendResult(['verify' => false]); + $this->assertInstanceOf('GuzzleHttp\Psr7\Response', $handler); } /** @@ -337,6 +338,8 @@ class StreamHandlerTest extends TestCase $opts = stream_context_get_options($res->getBody()->detach()); if (PHP_VERSION_ID < 50600) { $this->assertEquals($path, $opts['ssl']['cafile']); + } else { + $this->assertArrayNotHasKey('cafile', $opts['ssl']); } } diff --git a/tests/HandlerStackTest.php b/tests/HandlerStackTest.php index 1d86c356..b7489864 100644 --- a/tests/HandlerStackTest.php +++ b/tests/HandlerStackTest.php @@ -18,6 +18,9 @@ class HandlerStackTest extends TestCase $this->assertTrue($h->hasHandler()); } + /** + * @doesNotPerformAssertions + */ public function testCanSetDifferentHandlerAfterConstruction() { $f = function () {}; diff --git a/tests/PoolTest.php b/tests/PoolTest.php index 3d3d2258..f2ba9430 100644 --- a/tests/PoolTest.php +++ b/tests/PoolTest.php @@ -34,6 +34,9 @@ class PoolTest extends TestCase $p->promise()->wait(); } + /** + * @doesNotPerformAssertions + */ public function testSendsAndRealizesFuture() { $c = $this->getClient(); @@ -41,6 +44,9 @@ class PoolTest extends TestCase $p->promise()->wait(); } + /** + * @doesNotPerformAssertions + */ public function testExecutesPendingWhenWaiting() { $r1 = new Promise(function () use (&$r1) { $r1->resolve(new Response()); });