Response: Added with and withInput methods and back/redirect functions
parent
be39c63f46
commit
89742ecd55
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Http;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
|
||||
class RedirectServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind('redirect', Redirector::class);
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Http;
|
||||
|
||||
class Redirector
|
||||
{
|
||||
/** @var Request */
|
||||
protected $request;
|
||||
|
||||
/** @var Response */
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
*/
|
||||
public function __construct(Request $request, Response $response)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return Response
|
||||
*/
|
||||
public function to(string $path, int $status = 302, array $headers = []): Response
|
||||
{
|
||||
return $this->response->redirectTo($path, $status, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $status
|
||||
* @param array $headers
|
||||
* @return Response
|
||||
*/
|
||||
public function back(int $status = 302, array $headers = []): Response
|
||||
{
|
||||
return $this->to($this->getPreviousUrl(), $status, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getPreviousUrl(): string
|
||||
{
|
||||
if ($header = $this->request->getHeader('referer')) {
|
||||
return array_pop($header);
|
||||
}
|
||||
|
||||
return '/';
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Http\RedirectServiceProvider;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
|
||||
class RedirectServiceProviderTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\RedirectServiceProvider::register
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
$app = new Application();
|
||||
|
||||
$serviceProvider = new RedirectServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
|
||||
$this->assertTrue($app->has('redirect'));
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http;
|
||||
|
||||
use Engelsystem\Http\Redirector;
|
||||
use Engelsystem\Http\Request;
|
||||
use Engelsystem\Http\Response;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RedirectorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\Redirector::__construct
|
||||
* @covers \Engelsystem\Http\Redirector::to
|
||||
*/
|
||||
public function testTo()
|
||||
{
|
||||
$request = new Request();
|
||||
$response = new Response();
|
||||
$redirector = new Redirector($request, $response);
|
||||
|
||||
$return = $redirector->to('/test');
|
||||
$this->assertEquals(['/test'], $return->getHeader('location'));
|
||||
$this->assertEquals(302, $return->getStatusCode());
|
||||
|
||||
$return = $redirector->to('/foo', 303, ['test' => 'data']);
|
||||
$this->assertEquals(['/foo'], $return->getHeader('location'));
|
||||
$this->assertEquals(303, $return->getStatusCode());
|
||||
$this->assertEquals(['data'], $return->getHeader('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Http\Redirector::back
|
||||
* @covers \Engelsystem\Http\Redirector::getPreviousUrl
|
||||
*/
|
||||
public function testBack()
|
||||
{
|
||||
$request = new Request();
|
||||
$response = new Response();
|
||||
$redirector = new Redirector($request, $response);
|
||||
|
||||
$return = $redirector->back();
|
||||
$this->assertEquals(['/'], $return->getHeader('location'));
|
||||
$this->assertEquals(302, $return->getStatusCode());
|
||||
|
||||
$request = $request->withHeader('referer', '/old-page');
|
||||
$redirector = new Redirector($request, $response);
|
||||
$return = $redirector->back(303, ['foo' => 'bar']);
|
||||
$this->assertEquals(303, $return->getStatusCode());
|
||||
$this->assertEquals(['/old-page'], $return->getHeader('location'));
|
||||
$this->assertEquals(['bar'], $return->getHeader('foo'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue