Added / route with redirects

This commit is contained in:
Igor Scheller 2018-11-21 12:17:28 +01:00 committed by msquare
parent e948091066
commit 2e51fbff9d
9 changed files with 137 additions and 23 deletions

View File

@ -1,10 +1,14 @@
<?php <?php
use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
use FastRoute\RouteCollector; use FastRoute\RouteCollector;
/** @var RouteCollector $route */ /** @var RouteCollector $route */
// Pages // Pages
$route->get('/', function () {
throw new HttpTemporaryRedirect(auth()->user() ? config('home_site') : 'login');
});
$route->get('/credits', 'CreditsController@index'); $route->get('/credits', 'CreditsController@index');
// Stats // Stats

View File

@ -0,0 +1,17 @@
<?php
namespace Engelsystem\Http\Exceptions;
class HttpPermanentRedirect extends HttpRedirect
{
/**
* @param string $url
* @param array $headers
*/
public function __construct(
string $url,
array $headers = []
) {
parent::__construct($url, 301, $headers);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Engelsystem\Http\Exceptions;
class HttpRedirect extends HttpException
{
/**
* @param string $url
* @param int $statusCode
* @param array $headers
*/
public function __construct(
string $url,
int $statusCode = 302,
array $headers = []
) {
$headers = array_merge([
'Location' => $url,
], $headers);
parent::__construct($statusCode, '', $headers);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Engelsystem\Http\Exceptions;
class HttpTemporaryRedirect extends HttpRedirect
{
/**
* @param string $url
* @param array $headers
*/
public function __construct(
string $url,
array $headers = []
) {
parent::__construct($url, 302, $headers);
}
}

View File

@ -69,9 +69,6 @@ class LegacyMiddleware implements MiddlewareInterface
$page = $appRequest->path(); $page = $appRequest->path();
$page = str_replace('-', '_', $page); $page = str_replace('-', '_', $page);
} }
if ($page == '/') {
$page = $this->auth->user() ? config('home_site') : 'login';
}
$title = $content = ''; $title = $content = '';
if ( if (

View File

@ -0,0 +1,20 @@
<?php
namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Http\Exceptions\HttpPermanentRedirect;
use Engelsystem\Http\Exceptions\HttpRedirect;
use PHPUnit\Framework\TestCase;
class HttpPermanentRedirectTest extends TestCase
{
/**
* @covers \Engelsystem\Http\Exceptions\HttpPermanentRedirect::__construct
*/
public function testConstruct()
{
$exception = new HttpPermanentRedirect('https://lorem.ipsum/foo/bar');
$this->assertInstanceOf(HttpRedirect::class, $exception);
$this->assertEquals(301, $exception->getStatusCode());
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Engelsystem\Test\Unit\Http;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Http\Exceptions\HttpRedirect;
use PHPUnit\Framework\TestCase;
class HttpRedirectTest extends TestCase
{
use ArraySubsetAsserts;
/**
* @covers \Engelsystem\Http\Exceptions\HttpRedirect::__construct
*/
public function testConstruct()
{
$exception = new HttpRedirect('https://lorem.ipsum/foo/bar');
$this->assertEquals(302, $exception->getStatusCode());
$this->assertArraySubset(['Location' => 'https://lorem.ipsum/foo/bar'], $exception->getHeaders());
$exception = new HttpRedirect('/test', 301, ['lorem' => 'ipsum']);
$this->assertEquals(301, $exception->getStatusCode());
$this->assertArraySubset(['lorem' => 'ipsum'], $exception->getHeaders());
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Http\Exceptions\HttpRedirect;
use Engelsystem\Http\Exceptions\HttpTemporaryRedirect;
use PHPUnit\Framework\TestCase;
class HttpTemporaryRedirectTest extends TestCase
{
/**
* @covers \Engelsystem\Http\Exceptions\HttpTemporaryRedirect::__construct
*/
public function testConstruct()
{
$exception = new HttpTemporaryRedirect('https://lorem.ipsum/foo/bar');
$this->assertInstanceOf(HttpRedirect::class, $exception);
$this->assertEquals(302, $exception->getStatusCode());
}
}

View File

@ -46,36 +46,28 @@ class LegacyMiddlewareTest extends TestCase
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$middleware->expects($this->exactly(2)) $middleware->expects($this->once())
->method('loadPage') ->method('loadPage')
->withConsecutive(['user_worklog'], ['login']) ->with('user_worklog')
->willReturnOnConsecutiveCalls( ->willReturn(['title', 'content']);
['title', 'content'],
['title2', 'content2']
);
$middleware->expects($this->exactly(3)) $middleware->expects($this->exactly(2))
->method('renderPage') ->method('renderPage')
->withConsecutive( ->withConsecutive(
['user_worklog', 'title', 'content'], ['user_worklog', 'title', 'content'],
['404', 'Page not found', 'It\'s not available!'], ['404', 'Page not found', 'It\'s not available!']
['login', 'title2', 'content2']
) )
->willReturn($response); ->willReturn($response);
$container->expects($this->exactly(4)) $container->expects($this->exactly(3))
->method('get') ->method('get')
->withConsecutive(['request'], ['request'], ['translator'], ['request']) ->withConsecutive(['request'], ['request'], ['translator'])
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
$defaultRequest, $defaultRequest,
$defaultRequest, $defaultRequest,
$translator, $translator
$defaultRequest
); );
$auth->expects($this->atLeastOnce())
->method('user')
->willReturn(false);
$auth->expects($this->atLeastOnce()) $auth->expects($this->atLeastOnce())
->method('can') ->method('can')
->willReturn(false); ->willReturn(false);
@ -92,17 +84,15 @@ class LegacyMiddlewareTest extends TestCase
->method('path') ->method('path')
->willReturn('user-worklog'); ->willReturn('user-worklog');
$parameters->expects($this->exactly(3)) $parameters->expects($this->exactly(2))
->method('get') ->method('get')
->with('p') ->with('p')
->willReturnOnConsecutiveCalls( ->willReturnOnConsecutiveCalls(
null, null,
'foo', 'foo'
'/'
); );
$middleware->process($request, $handler); $middleware->process($request, $handler);
$middleware->process($request, $handler); $middleware->process($request, $handler);
$middleware->process($request, $handler);
} }
} }