1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<?php
/**
* @noinspection PhpArrayWriteIsNotUsedInspection
*/
namespace Interop\Http\Factory;
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use const UPLOAD_ERR_OK;
abstract class ServerRequestFactoryTestCase extends TestCase
{
/**
* @var ServerRequestFactoryInterface
*/
protected $factory;
/**
* @return ServerRequestFactoryInterface
*/
abstract protected function createServerRequestFactory();
/**
* @param string $uri
*
* @return UriInterface
*/
abstract protected function createUri($uri);
public function setUp(): void
{
$this->factory = $this->createServerRequestFactory();
}
protected function assertServerRequest($request, $method, $uri)
{
static::assertInstanceOf(ServerRequestInterface::class, $request);
static::assertSame($method, $request->getMethod());
static::assertSame($uri, (string) $request->getUri());
}
public static function dataMethods()
{
return [
['GET'],
['POST'],
['PUT'],
['DELETE'],
['OPTIONS'],
['HEAD'],
];
}
public static function dataServer()
{
$data = [];
foreach (static::dataMethods() as $methodData) {
$data[$methodData[0]] = [
[
'REQUEST_METHOD' => $methodData[0],
'REQUEST_URI' => '/test',
'QUERY_STRING' => 'foo=1&bar=true',
'HTTP_HOST' => 'example.org',
]
];
}
return $data;
}
#[DataProvider('dataServer')]
public function testCreateServerRequest($server)
{
$method = $server['REQUEST_METHOD'];
$uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";
$request = $this->factory->createServerRequest($method, $uri);
$this->assertServerRequest($request, $method, $uri);
}
#[DataProvider('dataServer')]
public function testCreateServerRequestFromArray(array $server)
{
$method = $server['REQUEST_METHOD'];
$uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";
$request = $this->factory->createServerRequest($method, $uri, $server);
$this->assertServerRequest($request, $method, $uri);
}
#[DataProvider('dataServer')]
public function testCreateServerRequestWithUriObject($server)
{
$method = $server['REQUEST_METHOD'];
$uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";
$request = $this->factory->createServerRequest($method, $this->createUri($uri));
$this->assertServerRequest($request, $method, $uri);
}
#[BackupGlobals(true)]
public function testCreateServerRequestDoesNotReadServerSuperglobal()
{
$_SERVER = ['HTTP_X_FOO' => 'bar'];
$server = [
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/test',
'QUERY_STRING' => 'super=0',
'HTTP_HOST' => 'example.org',
];
$request = $this->factory->createServerRequest('PUT', '/test', $server);
$serverParams = $request->getServerParams();
static::assertNotEquals($_SERVER, $serverParams);
static::assertArrayNotHasKey('HTTP_X_FOO', $serverParams);
}
public function testCreateServerRequestDoesNotReadCookieSuperglobal()
{
$_COOKIE = ['foo' => 'bar'];
$request = $this->factory->createServerRequest('POST', 'http://example.org/test');
static::assertEmpty($request->getCookieParams());
}
public function testCreateServerRequestDoesNotReadGetSuperglobal()
{
$_GET = ['foo' => 'bar'];
$request = $this->factory->createServerRequest('POST', 'http://example.org/test');
static::assertEmpty($request->getQueryParams());
}
public function testCreateServerRequestDoesNotReadFilesSuperglobal()
{
$_FILES = [['name' => 'foobar.dat', 'type' => 'application/octet-stream', 'tmp_name' => '/tmp/php45sd3f', 'error' => UPLOAD_ERR_OK, 'size' => 4]];
$request = $this->factory->createServerRequest('POST', 'http://example.org/test');
static::assertEmpty($request->getUploadedFiles());
}
public function testCreateServerRequestDoesNotReadPostSuperglobal()
{
$_POST = ['foo' => 'bar'];
$request = $this->factory->createServerRequest('POST', 'http://example.org/test');
static::assertEmpty($request->getParsedBody());
}
}
|