2018-01-18 19:01:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Http;
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2018-01-18 19:01:34 +01:00
|
|
|
use Engelsystem\Http\Response;
|
2018-08-29 13:58:50 +02:00
|
|
|
use Engelsystem\Renderer\Renderer;
|
2019-04-24 10:45:00 +02:00
|
|
|
use InvalidArgumentException;
|
2018-08-29 13:58:50 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-01-18 19:01:34 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-08-11 15:05:55 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2018-01-18 19:01:34 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
2019-11-27 19:11:37 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
2018-01-18 19:01:34 +01:00
|
|
|
|
|
|
|
class ResponseTest extends TestCase
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
use ArraySubsetAsserts;
|
|
|
|
|
2018-01-18 19:01:34 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response
|
|
|
|
*/
|
|
|
|
public function testCreate(): void
|
|
|
|
{
|
|
|
|
$response = new Response();
|
|
|
|
$this->assertInstanceOf(SymfonyResponse::class, $response);
|
2018-08-11 15:05:55 +02:00
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::getReasonPhrase
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Http\Response::withStatus
|
2018-08-11 15:05:55 +02:00
|
|
|
*/
|
|
|
|
public function testWithStatus(): void
|
|
|
|
{
|
|
|
|
$response = new Response();
|
|
|
|
$newResponse = $response->withStatus(503);
|
|
|
|
$this->assertNotEquals($response, $newResponse);
|
|
|
|
$this->assertNotEquals('', $newResponse->getReasonPhrase());
|
|
|
|
$this->assertEquals(503, $newResponse->getStatusCode());
|
|
|
|
|
|
|
|
$newResponse = $response->withStatus(503, 'Foo');
|
|
|
|
$this->assertEquals('Foo', $newResponse->getReasonPhrase());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::withContent
|
|
|
|
*/
|
|
|
|
public function testWithContent(): void
|
|
|
|
{
|
|
|
|
$response = new Response();
|
|
|
|
$newResponse = $response->withContent('Lorem Ipsum?');
|
|
|
|
|
|
|
|
$this->assertNotEquals($response, $newResponse);
|
|
|
|
$this->assertEquals('Lorem Ipsum?', $newResponse->getContent());
|
2018-01-18 19:01:34 +01:00
|
|
|
}
|
2018-08-29 13:58:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::withView
|
2019-10-08 14:07:28 +02:00
|
|
|
* @covers \Engelsystem\Http\Response::setRenderer
|
2018-08-29 13:58:50 +02:00
|
|
|
*/
|
|
|
|
public function testWithView(): void
|
|
|
|
{
|
2022-04-16 10:38:28 +02:00
|
|
|
/** @var Renderer|MockObject $renderer */
|
2018-08-29 13:58:50 +02:00
|
|
|
$renderer = $this->createMock(Renderer::class);
|
|
|
|
|
|
|
|
$renderer->expects($this->once())
|
|
|
|
->method('render')
|
|
|
|
->with('foo', ['lorem' => 'ipsum'])
|
|
|
|
->willReturn('Foo ipsum!');
|
|
|
|
|
|
|
|
$response = new Response('', 200, [], $renderer);
|
|
|
|
$newResponse = $response->withView('foo', ['lorem' => 'ipsum'], 505, ['test' => 'er']);
|
|
|
|
|
|
|
|
$this->assertNotEquals($response, $newResponse);
|
|
|
|
$this->assertEquals('Foo ipsum!', $newResponse->getContent());
|
|
|
|
$this->assertEquals(505, $newResponse->getStatusCode());
|
|
|
|
$this->assertArraySubset(['test' => ['er']], $newResponse->getHeaders());
|
2019-10-08 14:07:28 +02:00
|
|
|
|
2022-04-16 10:38:28 +02:00
|
|
|
/** @var Renderer|MockObject $renderer */
|
2019-10-08 14:07:28 +02:00
|
|
|
$anotherRenderer = $this->createMock(Renderer::class);
|
|
|
|
$anotherRenderer->expects($this->once())
|
|
|
|
->method('render')
|
|
|
|
->with('bar')
|
|
|
|
->willReturn('Stuff');
|
|
|
|
|
|
|
|
$response->setRenderer($anotherRenderer);
|
|
|
|
$response = $response->withView('bar');
|
|
|
|
$this->assertEquals('Stuff', $response->getContent());
|
2018-08-29 13:58:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::withView
|
|
|
|
*/
|
|
|
|
public function testWithViewNoRenderer(): void
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2018-08-29 13:58:50 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$response->withView('foo');
|
|
|
|
}
|
2018-10-25 18:53:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::redirectTo
|
|
|
|
*/
|
|
|
|
public function testRedirectTo(): void
|
|
|
|
{
|
|
|
|
$response = new Response();
|
|
|
|
$newResponse = $response->redirectTo('http://foo.bar/lorem', 301, ['test' => 'ing']);
|
|
|
|
|
|
|
|
$this->assertNotEquals($response, $newResponse);
|
|
|
|
$this->assertEquals(301, $newResponse->getStatusCode());
|
|
|
|
$this->assertArraySubset(
|
|
|
|
[
|
|
|
|
'location' => ['http://foo.bar/lorem'],
|
|
|
|
'test' => ['ing'],
|
|
|
|
],
|
|
|
|
$newResponse->getHeaders()
|
|
|
|
);
|
|
|
|
}
|
2019-11-27 19:11:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::with
|
|
|
|
*/
|
|
|
|
public function testWith(): void
|
|
|
|
{
|
|
|
|
$session = new Session(new MockArraySessionStorage());
|
|
|
|
$response = new Response('', 200, [], null, $session);
|
|
|
|
|
|
|
|
$response->with('foo', 'bar');
|
|
|
|
$this->assertEquals('bar', $session->get('foo'));
|
|
|
|
|
|
|
|
$response->with('lorem', ['ipsum', 'dolor' => ['foo' => 'bar']]);
|
|
|
|
$this->assertEquals(['ipsum', 'dolor' => ['foo' => 'bar']], $session->get('lorem'));
|
|
|
|
|
|
|
|
$response->with('lorem', ['dolor' => ['test' => 'er']]);
|
|
|
|
$this->assertEquals(['ipsum', 'dolor' => ['foo' => 'bar', 'test' => 'er']], $session->get('lorem'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::with
|
|
|
|
*/
|
|
|
|
public function testWithNoSession(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$response->with('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::withInput
|
|
|
|
*/
|
|
|
|
public function testWithInput(): void
|
|
|
|
{
|
|
|
|
$session = new Session(new MockArraySessionStorage());
|
|
|
|
$response = new Response('', 200, [], null, $session);
|
|
|
|
|
|
|
|
$response->withInput(['some' => 'value']);
|
|
|
|
$this->assertEquals(['some' => 'value'], $session->get('form-data'));
|
|
|
|
|
|
|
|
$response->withInput(['lorem' => 'ipsum']);
|
|
|
|
$this->assertEquals(['lorem' => 'ipsum'], $session->get('form-data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\Response::withInput
|
|
|
|
*/
|
|
|
|
public function testWithInputNoSession(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$response->withInput(['some' => 'value']);
|
|
|
|
}
|
2019-04-24 11:01:37 +02:00
|
|
|
}
|