mirror of
https://github.com/flarum/framework.git
synced 2024-11-26 18:33:40 +08:00
Tests: Extract trait for building requests
This commit is contained in:
parent
fe8df6df4e
commit
6540049421
48
framework/core/tests/integration/BuildsHttpRequests.php
Normal file
48
framework/core/tests/integration/BuildsHttpRequests.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration;
|
||||
|
||||
use Dflydev\FigCookies\SetCookie;
|
||||
use Laminas\Diactoros\CallbackStream;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
/**
|
||||
* A collection of helpers for building PSR-7 requests for integration tests.
|
||||
*/
|
||||
trait BuildsHttpRequests
|
||||
{
|
||||
protected function requestWithJsonBody(Request $req, array $json): Request
|
||||
{
|
||||
return $req
|
||||
->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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user