mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-23 06:02:57 +02:00
Added CacheMiddlewareTest and ThemeMiddlewareTest and updated some code
This commit is contained in:
@@ -109,8 +109,8 @@ return [
|
||||
* ]
|
||||
*/
|
||||
'http_cache' => [
|
||||
'application/json' => '300',
|
||||
'application/zip' => '300',
|
||||
'application/json' => 300,
|
||||
'application/zip' => 300,
|
||||
],
|
||||
|
||||
];
|
||||
|
@@ -31,14 +31,16 @@ class ThemeMiddleware
|
||||
/** Determine the theme from the request. */
|
||||
private function getThemeFromRequest(Request $request): string
|
||||
{
|
||||
if (! isset($request->getCookieParams()['theme'])) {
|
||||
$cookies = $request->getCookieParams();
|
||||
|
||||
if (! isset($cookies['theme'])) {
|
||||
return 'light';
|
||||
}
|
||||
|
||||
if (! in_array($request->getCookieParams()['theme'], self::VALID_THEMES)) {
|
||||
if (! in_array($cookies['theme'], self::VALID_THEMES)) {
|
||||
return 'light';
|
||||
}
|
||||
|
||||
return $request->getCookieParams()['theme'];
|
||||
return $cookies['theme'];
|
||||
}
|
||||
}
|
||||
|
53
tests/Middlewares/CacheMiddlewareTest.php
Normal file
53
tests/Middlewares/CacheMiddlewareTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Middlewares;
|
||||
|
||||
use App\Middlewares\CacheMiddleware;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Slim\Psr7\Headers;
|
||||
use Slim\Psr7\Response;
|
||||
use Tests\TestCase;
|
||||
|
||||
/** @covers \App\Middlewares\CacheMiddleware */
|
||||
class CacheMiddlewareTest extends TestCase
|
||||
{
|
||||
/** @var RequestHandlerInterface&MockObject */
|
||||
protected $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->request = $this->createMock(ServerRequestInterface::class);
|
||||
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
||||
}
|
||||
|
||||
public function test_it_adds_a_response_cache_header_with_age_of_zero_by_defualt(): void
|
||||
{
|
||||
$this->handler->expects($this->once())->method('handle')->willReturn(
|
||||
new Response(StatusCodeInterface::STATUS_OK, new Headers([
|
||||
'Content-Type' => 'text/html',
|
||||
]))
|
||||
);
|
||||
|
||||
$response = (new CacheMiddleware($this->config))($this->request, $this->handler);
|
||||
|
||||
$this->assertEquals(['max-age=0, must-revalidate'], $response->getHeader('Cache-Control'));
|
||||
}
|
||||
|
||||
public function test_it_adds_a_response_cache_header_for_a_pre_configured_http_cache_option(): void
|
||||
{
|
||||
$this->handler->expects($this->once())->method('handle')->willReturn(
|
||||
new Response(StatusCodeInterface::STATUS_OK, new Headers([
|
||||
'Content-Type' => 'application/json',
|
||||
]))
|
||||
);
|
||||
|
||||
$response = (new CacheMiddleware($this->config))($this->request, $this->handler);
|
||||
|
||||
$this->assertEquals(['max-age=300'], $response->getHeader('Cache-Control'));
|
||||
}
|
||||
}
|
72
tests/Middlewares/ThemeMiddlewareTest.php
Normal file
72
tests/Middlewares/ThemeMiddlewareTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Middlewares;
|
||||
|
||||
use App\Middlewares\ThemeMiddleware;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Slim\Views\Twig;
|
||||
use Tests\TestCase;
|
||||
|
||||
/** @covers \App\Middlewares\ThemeMiddleware */
|
||||
class ThemeMiddlewareTest extends TestCase
|
||||
{
|
||||
/** @var Twig Twig templating component */
|
||||
protected $view;
|
||||
|
||||
/** @var ServerRequestInterface&MockObject */
|
||||
protected $request;
|
||||
|
||||
/** @var RequestHandlerInterface&MockObject */
|
||||
protected $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->view = $this->container->get(Twig::class);
|
||||
$this->request = $this->createMock(ServerRequestInterface::class);
|
||||
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
||||
}
|
||||
|
||||
public function test_it_sets_the_theme_view_variabe_to_light_by_default(): void
|
||||
{
|
||||
(new ThemeMiddleware($this->view))($this->request, $this->handler);
|
||||
|
||||
$this->assertEquals(['theme' => 'light'], $this->view->getEnvironment()->getGlobals());
|
||||
}
|
||||
|
||||
public function test_it_sets_the_theme_view_variabe_to_dark_when_the_theme_cookie_is_dark(): void
|
||||
{
|
||||
$this->request->expects($this->once())->method('getCookieParams')->willReturn([
|
||||
'theme' => 'dark',
|
||||
]);
|
||||
|
||||
(new ThemeMiddleware($this->view))($this->request, $this->handler);
|
||||
|
||||
$this->assertEquals(['theme' => 'dark'], $this->view->getEnvironment()->getGlobals());
|
||||
}
|
||||
|
||||
public function test_it_sets_the_theme_view_variabe_to_light_when_the_theme_cookie_is_light(): void
|
||||
{
|
||||
$this->request->expects($this->once())->method('getCookieParams')->willReturn([
|
||||
'theme' => 'dark',
|
||||
]);
|
||||
|
||||
(new ThemeMiddleware($this->view))($this->request, $this->handler);
|
||||
|
||||
$this->assertEquals(['theme' => 'dark'], $this->view->getEnvironment()->getGlobals());
|
||||
}
|
||||
|
||||
public function test_it_sets_the_theme_view_variabe_to_light_for_an_unknown_value(): void
|
||||
{
|
||||
$this->request->expects($this->once())->method('getCookieParams')->willReturn([
|
||||
'theme' => 'dim',
|
||||
]);
|
||||
|
||||
(new ThemeMiddleware($this->view))($this->request, $this->handler);
|
||||
|
||||
$this->assertEquals(['theme' => 'light'], $this->view->getEnvironment()->getGlobals());
|
||||
}
|
||||
}
|
@@ -22,8 +22,10 @@ class TestCase extends BaseTestCase
|
||||
protected $testFilesPath = __DIR__ . '/_files';
|
||||
|
||||
/** This method is called before each test. */
|
||||
public function setUp(): void
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Dotenv::createUnsafeImmutable(__DIR__)->safeLoad();
|
||||
|
||||
$this->container = BootManager::createContainer(
|
||||
|
Reference in New Issue
Block a user