From a13c0bb61249cbba467db8a7035e9260370117e7 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 20 Mar 2020 17:48:39 +0100 Subject: [PATCH] Tests: Extract trait for building requests --- tests/integration/BuildsHttpRequests.php | 48 ++++++++++++++++++++++++ tests/integration/TestCase.php | 32 ++++------------ 2 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 tests/integration/BuildsHttpRequests.php diff --git a/tests/integration/BuildsHttpRequests.php b/tests/integration/BuildsHttpRequests.php new file mode 100644 index 000000000..d0a21b61c --- /dev/null +++ b/tests/integration/BuildsHttpRequests.php @@ -0,0 +1,48 @@ +withHeader('Content-Type', 'application/json') + ->withBody( + new CallbackStream(function () use ($json) { + return json_encode($json); + }) + ); + } + + protected function requestWithCookiesFrom(Request $req, Response $previous): Request + { + $cookies = array_reduce( + $previous->getHeader('Set-Cookie'), + function ($memo, $setCookieString) { + $setCookie = SetCookie::fromSetCookieString($setCookieString); + $memo[$setCookie->getName()] = $setCookie->getValue(); + + return $memo; + }, + [] + ); + + return $req->withCookieParams($cookies); + } +} diff --git a/tests/integration/TestCase.php b/tests/integration/TestCase.php index 687023dbe..65af8cdca 100644 --- a/tests/integration/TestCase.php +++ b/tests/integration/TestCase.php @@ -9,11 +9,9 @@ namespace Flarum\Tests\integration; -use Dflydev\FigCookies\SetCookie; use Flarum\Extend\ExtenderInterface; use Flarum\Foundation\InstalledSite; use Illuminate\Database\ConnectionInterface; -use Laminas\Diactoros\CallbackStream; use Laminas\Diactoros\ServerRequest; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -21,6 +19,8 @@ use Psr\Http\Server\RequestHandlerInterface; abstract class TestCase extends \PHPUnit\Framework\TestCase { + use BuildsHttpRequests; + /** * @var \Flarum\Foundation\InstalledApp */ @@ -131,7 +131,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase /** * Build a HTTP request that can be passed through middleware. * - * This method simplifies building HTTP request for use in our HTTP-level + * This method simplifies building HTTP requests for use in our HTTP-level * integration tests. It provides options for all features repeatedly being * used in those tests. * @@ -155,32 +155,16 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase // Do we want a JSON request body? if (isset($options['json'])) { - $request = $request - ->withHeader('Content-Type', 'application/json') - ->withBody( - new CallbackStream(function () use ($options) { - return json_encode($options['json']); - }) - ); + $request = $this->requestWithJsonBody( + $request, $options['json'] + ); } // Let's copy the cookies from a previous response if (isset($options['cookiesFrom'])) { - /** @var ResponseInterface $previousResponse */ - $previousResponse = $options['cookiesFrom']; - - $cookies = array_reduce( - $previousResponse->getHeader('Set-Cookie'), - function ($memo, $setCookieString) { - $setCookie = SetCookie::fromSetCookieString($setCookieString); - $memo[$setCookie->getName()] = $setCookie->getValue(); - - return $memo; - }, - [] + $request = $this->requestWithCookiesFrom( + $request, $options['cookiesFrom'] ); - - $request = $request->withCookieParams($cookies); } return $request;