mirror of
https://github.com/flarum/core.git
synced 2025-07-16 14:26:25 +02:00
Tests: Extract trait for building requests
This commit is contained in:
48
tests/integration/BuildsHttpRequests.php
Normal file
48
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;
|
namespace Flarum\Tests\integration;
|
||||||
|
|
||||||
use Dflydev\FigCookies\SetCookie;
|
|
||||||
use Flarum\Extend\ExtenderInterface;
|
use Flarum\Extend\ExtenderInterface;
|
||||||
use Flarum\Foundation\InstalledSite;
|
use Flarum\Foundation\InstalledSite;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use Laminas\Diactoros\CallbackStream;
|
|
||||||
use Laminas\Diactoros\ServerRequest;
|
use Laminas\Diactoros\ServerRequest;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
@ -21,6 +19,8 @@ use Psr\Http\Server\RequestHandlerInterface;
|
|||||||
|
|
||||||
abstract class TestCase extends \PHPUnit\Framework\TestCase
|
abstract class TestCase extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
|
use BuildsHttpRequests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Flarum\Foundation\InstalledApp
|
* @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.
|
* 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
|
* integration tests. It provides options for all features repeatedly being
|
||||||
* used in those tests.
|
* used in those tests.
|
||||||
*
|
*
|
||||||
@ -155,32 +155,16 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
// Do we want a JSON request body?
|
// Do we want a JSON request body?
|
||||||
if (isset($options['json'])) {
|
if (isset($options['json'])) {
|
||||||
$request = $request
|
$request = $this->requestWithJsonBody(
|
||||||
->withHeader('Content-Type', 'application/json')
|
$request, $options['json']
|
||||||
->withBody(
|
);
|
||||||
new CallbackStream(function () use ($options) {
|
|
||||||
return json_encode($options['json']);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's copy the cookies from a previous response
|
// Let's copy the cookies from a previous response
|
||||||
if (isset($options['cookiesFrom'])) {
|
if (isset($options['cookiesFrom'])) {
|
||||||
/** @var ResponseInterface $previousResponse */
|
$request = $this->requestWithCookiesFrom(
|
||||||
$previousResponse = $options['cookiesFrom'];
|
$request, $options['cookiesFrom']
|
||||||
|
|
||||||
$cookies = array_reduce(
|
|
||||||
$previousResponse->getHeader('Set-Cookie'),
|
|
||||||
function ($memo, $setCookieString) {
|
|
||||||
$setCookie = SetCookie::fromSetCookieString($setCookieString);
|
|
||||||
$memo[$setCookie->getName()] = $setCookie->getValue();
|
|
||||||
|
|
||||||
return $memo;
|
|
||||||
},
|
|
||||||
[]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$request = $request->withCookieParams($cookies);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $request;
|
return $request;
|
||||||
|
Reference in New Issue
Block a user