2017-10-31 17:17:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Http;
|
|
|
|
|
|
|
|
use Engelsystem\Http\Request;
|
2019-04-17 10:09:50 +02:00
|
|
|
use Nyholm\Psr7\UploadedFile;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-10-31 17:17:32 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-08-16 18:13:53 +02:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
2018-08-20 17:35:07 +02:00
|
|
|
use Psr\Http\Message\UploadedFileInterface;
|
2018-08-16 18:13:53 +02:00
|
|
|
use Psr\Http\Message\UriInterface;
|
2018-08-20 17:35:07 +02:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile;
|
2018-01-18 19:01:34 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
|
2017-10-31 17:17:32 +01:00
|
|
|
|
|
|
|
class RequestTest extends TestCase
|
|
|
|
{
|
2018-01-18 19:01:34 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request
|
|
|
|
*/
|
|
|
|
public function testCreate(): void
|
|
|
|
{
|
|
|
|
$response = new Request();
|
|
|
|
$this->assertInstanceOf(SymfonyRequest::class, $response);
|
2018-08-16 18:13:53 +02:00
|
|
|
$this->assertInstanceOf(RequestInterface::class, $response);
|
2018-01-18 19:01:34 +01:00
|
|
|
}
|
|
|
|
|
2017-10-31 17:17:32 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::postData
|
|
|
|
*/
|
|
|
|
public function testPostData(): void
|
|
|
|
{
|
|
|
|
$request = new Request(
|
|
|
|
['foo' => 'I\'m a test!'],
|
|
|
|
['foo' => 'bar']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('bar', $request->postData('foo'));
|
|
|
|
$this->assertEquals('LoremIpsum', $request->postData('test-key', 'LoremIpsum'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::input
|
|
|
|
*/
|
|
|
|
public function testInput(): void
|
|
|
|
{
|
|
|
|
$request = new Request(
|
|
|
|
['foo' => 'I\'m a test!'],
|
|
|
|
['foo' => 'bar']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('I\'m a test!', $request->input('foo'));
|
|
|
|
$this->assertEquals('LoremIpsum', $request->input('test-key', 'LoremIpsum'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::has
|
|
|
|
*/
|
|
|
|
public function testHas(): void
|
|
|
|
{
|
|
|
|
$request = new Request([
|
|
|
|
'foo' => 'I\'m a test!',
|
|
|
|
'bar' => '',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($request->has('foo'));
|
2017-11-19 14:47:32 +01:00
|
|
|
$this->assertTrue($request->has('bar'));
|
|
|
|
$this->assertFalse($request->has('baz'));
|
2017-10-31 17:17:32 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 16:02:03 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::hasPostData
|
|
|
|
*/
|
|
|
|
public function testHasPostData(): void
|
|
|
|
{
|
|
|
|
$request = new Request([
|
|
|
|
'foo' => 'bar',
|
|
|
|
], [
|
|
|
|
'lorem' => 'ipsum',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($request->has('foo'));
|
|
|
|
$this->assertFalse($request->hasPostData('foo'));
|
|
|
|
|
|
|
|
$this->assertTrue($request->has('lorem'));
|
|
|
|
$this->assertTrue($request->hasPostData('lorem'));
|
|
|
|
}
|
|
|
|
|
2017-10-31 17:17:32 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::path
|
|
|
|
*/
|
|
|
|
public function testPath(): void
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Request|MockObject $request */
|
2017-10-31 17:17:32 +01:00
|
|
|
$request = $this
|
|
|
|
->getMockBuilder(Request::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['getPathInfo'])
|
2017-10-31 17:17:32 +01:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('getPathInfo')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
'/foo',
|
|
|
|
'/'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('foo', $request->path());
|
|
|
|
$this->assertEquals('/', $request->path());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::url
|
|
|
|
*/
|
|
|
|
public function testUrl(): void
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Request|MockObject $request */
|
2017-10-31 17:17:32 +01:00
|
|
|
$request = $this
|
|
|
|
->getMockBuilder(Request::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['getUri'])
|
2017-10-31 17:17:32 +01:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request
|
|
|
|
->expects($this->atLeastOnce())
|
|
|
|
->method('getUri')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
'http://foo.bar/bla/foo/',
|
|
|
|
'https://lorem.ipsum/dolor/sit?amet=consetetur&sadipscing=elitr'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('http://foo.bar/bla/foo', $request->url());
|
|
|
|
$this->assertEquals('https://lorem.ipsum/dolor/sit', $request->url());
|
|
|
|
}
|
2018-08-16 18:13:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getRequestTarget
|
|
|
|
*/
|
|
|
|
public function testGetRequestTarget(): void
|
|
|
|
{
|
|
|
|
/** @var Request|MockObject $request */
|
|
|
|
$request = $this
|
|
|
|
->getMockBuilder(Request::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['getQueryString', 'path'])
|
2018-08-16 18:13:53 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->exactly(2))
|
|
|
|
->method('getQueryString')
|
|
|
|
->willReturnOnConsecutiveCalls(null, 'foo=bar&lorem=ipsum');
|
|
|
|
$request->expects($this->exactly(2))
|
|
|
|
->method('path')
|
|
|
|
->willReturn('foo/bar');
|
|
|
|
|
|
|
|
$this->assertEquals('/foo/bar', $request->getRequestTarget());
|
|
|
|
$this->assertEquals('/foo/bar?foo=bar&lorem=ipsum', $request->getRequestTarget());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withRequestTarget
|
|
|
|
*/
|
|
|
|
public function testWithRequestTarget(): void
|
|
|
|
{
|
|
|
|
$request = new Request();
|
|
|
|
foreach (
|
|
|
|
[
|
|
|
|
'*',
|
|
|
|
'/foo/bar',
|
2019-04-24 11:01:37 +02:00
|
|
|
'https://lorem.ipsum/test?lor=em',
|
2018-08-16 18:13:53 +02:00
|
|
|
] as $target
|
|
|
|
) {
|
|
|
|
$new = $request->withRequestTarget($target);
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withMethod
|
|
|
|
*/
|
|
|
|
public function testWithMethod(): void
|
|
|
|
{
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
$new = $request->withMethod('PUT');
|
|
|
|
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
$this->assertEquals('PUT', $new->getMethod());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withUri
|
|
|
|
*/
|
|
|
|
public function testWithUri(): void
|
|
|
|
{
|
|
|
|
/** @var UriInterface|MockObject $uri */
|
|
|
|
$uri = $this->getMockForAbstractClass(UriInterface::class);
|
|
|
|
|
|
|
|
$uri->expects($this->atLeastOnce())
|
|
|
|
->method('__toString')
|
|
|
|
->willReturn('http://foo.bar/bla?foo=bar');
|
|
|
|
|
|
|
|
$request = Request::create('http://lor.em/');
|
|
|
|
|
|
|
|
$new = $request->withUri($uri);
|
|
|
|
$this->assertNotEquals($request, $new);
|
2021-12-19 19:48:20 +01:00
|
|
|
$this->assertEquals('http://foo.bar/bla?foo=bar', $new->getUri());
|
2018-08-16 18:13:53 +02:00
|
|
|
|
|
|
|
$new = $request->withUri($uri, true);
|
2021-12-19 19:48:20 +01:00
|
|
|
$this->assertEquals('http://lor.em/bla?foo=bar', $new->getUri());
|
2018-08-16 18:13:53 +02:00
|
|
|
}
|
2018-08-20 17:35:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getServerParams
|
|
|
|
*/
|
|
|
|
public function testGetServerParams(): void
|
|
|
|
{
|
|
|
|
$server = ['foo' => 'bar'];
|
|
|
|
$request = new Request([], [], [], [], [], $server);
|
|
|
|
|
|
|
|
$this->assertEquals($server, $request->getServerParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getCookieParams
|
|
|
|
*/
|
|
|
|
public function testGetCookieParams(): void
|
|
|
|
{
|
|
|
|
$cookies = ['session' => 'LoremIpsumDolorSit'];
|
|
|
|
$request = new Request([], [], [], $cookies);
|
|
|
|
|
|
|
|
$this->assertEquals($cookies, $request->getCookieParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withCookieParams
|
|
|
|
*/
|
|
|
|
public function testWithCookieParams(): void
|
|
|
|
{
|
|
|
|
$cookies = ['lor' => 'em'];
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
$new = $request->withCookieParams($cookies);
|
|
|
|
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
$this->assertEquals($cookies, $new->getCookieParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getQueryParams
|
|
|
|
*/
|
|
|
|
public function testGetQueryParams(): void
|
|
|
|
{
|
|
|
|
$params = ['foo' => 'baz'];
|
|
|
|
$request = new Request($params);
|
|
|
|
|
|
|
|
$this->assertEquals($params, $request->getQueryParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withQueryParams
|
|
|
|
*/
|
|
|
|
public function testWithQueryParams(): void
|
|
|
|
{
|
|
|
|
$params = ['test' => 'ing'];
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
$new = $request->withQueryParams($params);
|
|
|
|
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
$this->assertEquals($params, $new->getQueryParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getUploadedFiles
|
|
|
|
*/
|
|
|
|
public function testGetUploadedFiles(): void
|
|
|
|
{
|
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'test');
|
|
|
|
file_put_contents($filename, 'LoremIpsum!');
|
2019-05-31 17:09:50 +02:00
|
|
|
$files = [new SymfonyFile($filename, 'foo.txt', 'text/plain', UPLOAD_ERR_PARTIAL)];
|
2018-08-20 17:35:07 +02:00
|
|
|
$request = new Request([], [], [], [], $files);
|
|
|
|
|
|
|
|
$uploadedFiles = $request->getUploadedFiles();
|
|
|
|
$this->assertNotEmpty($uploadedFiles);
|
|
|
|
|
|
|
|
/** @var UploadedFileInterface $file */
|
|
|
|
$file = $uploadedFiles[0];
|
|
|
|
$this->assertInstanceOf(UploadedFileInterface::class, $file);
|
2019-03-17 18:17:33 +01:00
|
|
|
$this->assertEquals('foo.txt', $file->getClientFilename());
|
|
|
|
$this->assertEquals('text/plain', $file->getClientMediaType());
|
2018-08-20 17:35:07 +02:00
|
|
|
$this->assertEquals(11, $file->getSize());
|
2019-05-31 17:09:50 +02:00
|
|
|
$this->assertEquals(UPLOAD_ERR_PARTIAL, $file->getError());
|
2018-08-20 17:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withUploadedFiles
|
|
|
|
*/
|
|
|
|
public function testWithUploadedFiles(): void
|
|
|
|
{
|
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'test');
|
|
|
|
file_put_contents($filename, 'LoremIpsum!');
|
2019-04-17 10:09:50 +02:00
|
|
|
$file = new UploadedFile($filename, 11, UPLOAD_ERR_OK, 'test.txt', 'text/plain');
|
2018-08-20 17:35:07 +02:00
|
|
|
|
|
|
|
$request = new Request();
|
|
|
|
$new = $request->withUploadedFiles([$file]);
|
|
|
|
$uploadedFiles = $new->getUploadedFiles();
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
$this->assertNotEmpty($uploadedFiles);
|
|
|
|
|
|
|
|
/** @var UploadedFileInterface $file */
|
|
|
|
$file = $uploadedFiles[0];
|
|
|
|
$this->assertEquals('test.txt', $file->getClientFilename());
|
|
|
|
$this->assertEquals('text/plain', $file->getClientMediaType());
|
|
|
|
$this->assertEquals(11, $file->getSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getParsedBody
|
|
|
|
*/
|
|
|
|
public function testGetParsedBody(): void
|
|
|
|
{
|
|
|
|
$body = ['foo' => 'lorem'];
|
|
|
|
$request = new Request();
|
|
|
|
$request->request->add($body);
|
|
|
|
|
|
|
|
$this->assertEquals($body, $request->getParsedBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withParsedBody
|
|
|
|
*/
|
|
|
|
public function testWithParsedBody(): void
|
|
|
|
{
|
|
|
|
$data = ['test' => 'er'];
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
$new = $request->withParsedBody($data);
|
|
|
|
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
$this->assertEquals($data, $new->getParsedBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getAttributes
|
|
|
|
*/
|
|
|
|
public function testGetAttributes(): void
|
|
|
|
{
|
|
|
|
$attributes = ['foo' => 'lorem', 'ipsum' => 'dolor'];
|
|
|
|
$request = new Request([], [], $attributes);
|
|
|
|
|
|
|
|
$this->assertEquals($attributes, $request->getAttributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::getAttribute
|
|
|
|
*/
|
|
|
|
public function testGetAttribute(): void
|
|
|
|
{
|
|
|
|
$attributes = ['foo' => 'lorem', 'ipsum' => 'dolor'];
|
|
|
|
$request = new Request([], [], $attributes);
|
|
|
|
|
|
|
|
$this->assertEquals($attributes['ipsum'], $request->getAttribute('ipsum'));
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->assertNull($request->getAttribute('dolor'));
|
2018-08-20 17:35:07 +02:00
|
|
|
$this->assertEquals(1234, $request->getAttribute('test', 1234));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withAttribute
|
|
|
|
*/
|
|
|
|
public function testWithAttribute(): void
|
|
|
|
{
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
$new = $request->withAttribute('lorem', 'ipsum');
|
|
|
|
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
$this->assertEquals('ipsum', $new->getAttribute('lorem'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Request::withoutAttribute
|
|
|
|
*/
|
|
|
|
public function testWithoutAttribute(): void
|
|
|
|
{
|
|
|
|
$attributes = ['foo' => 'lorem', 'ipsum' => 'dolor'];
|
|
|
|
$request = new Request([], [], $attributes);
|
|
|
|
|
|
|
|
$new = $request->withoutAttribute('ipsum');
|
|
|
|
|
|
|
|
$this->assertNotEquals($request, $new);
|
|
|
|
$this->assertEquals(['foo' => 'lorem'], $new->getAttributes());
|
|
|
|
}
|
2017-10-31 17:17:32 +01:00
|
|
|
}
|