X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ffront%2FShaarliMiddlewareTest.php;h=655c5bba635ff2ec8ce3df3ba5a9bafacbf17051;hb=a5a9cf23acd1248585173aa32757d9720b5f2d62;hp=81ea134409ffdd8e9f03d4dff90da8c4f1bebde6;hpb=1a8ac737e52cb25a5c346232ee398f5908cee7d7;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php index 81ea1344..655c5bba 100644 --- a/tests/front/ShaarliMiddlewareTest.php +++ b/tests/front/ShaarliMiddlewareTest.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shaarli\Front; -use PHPUnit\Framework\TestCase; use Shaarli\Config\ConfigManager; use Shaarli\Container\ShaarliContainer; use Shaarli\Front\Exception\LoginBannedException; @@ -12,6 +11,7 @@ use Shaarli\Front\Exception\UnauthorizedException; use Shaarli\Render\PageBuilder; use Shaarli\Render\PageCacheManager; use Shaarli\Security\LoginManager; +use Shaarli\TestCase; use Shaarli\Updater\Updater; use Slim\Http\Request; use Slim\Http\Response; @@ -19,6 +19,8 @@ use Slim\Http\Uri; class ShaarliMiddlewareTest extends TestCase { + protected const TMP_MOCK_FILE = '.tmp'; + /** @var ShaarliContainer */ protected $container; @@ -29,12 +31,23 @@ class ShaarliMiddlewareTest extends TestCase { $this->container = $this->createMock(ShaarliContainer::class); + touch(static::TMP_MOCK_FILE); + $this->container->conf = $this->createMock(ConfigManager::class); + $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE); + $this->container->loginManager = $this->createMock(LoginManager::class); + $this->container->environment = ['REQUEST_URI' => 'http://shaarli/subfolder/path']; + $this->middleware = new ShaarliMiddleware($this->container); } + public function tearDown(): void + { + unlink(static::TMP_MOCK_FILE); + } + /** * Test middleware execution with valid controller call */ @@ -61,7 +74,8 @@ class ShaarliMiddlewareTest extends TestCase } /** - * Test middleware execution with controller throwing a known front exception + * Test middleware execution with controller throwing a known front exception. + * The exception should be thrown to be later handled by the error handler. */ public function testMiddlewareExecutionWithFrontException(): void { @@ -86,16 +100,14 @@ class ShaarliMiddlewareTest extends TestCase }); $this->container->pageBuilder = $pageBuilder; - /** @var Response $result */ - $result = $this->middleware->__invoke($request, $response, $controller); + $this->expectException(LoginBannedException::class); - static::assertInstanceOf(Response::class, $result); - static::assertSame(401, $result->getStatusCode()); - static::assertContains('error', (string) $result->getBody()); + $this->middleware->__invoke($request, $response, $controller); } /** * Test middleware execution with controller throwing a not authorized exception + * The middle should send a redirection response to the login page. */ public function testMiddlewareExecutionWithUnauthorizedException(): void { @@ -116,13 +128,17 @@ class ShaarliMiddlewareTest extends TestCase $result = $this->middleware->__invoke($request, $response, $controller); static::assertSame(302, $result->getStatusCode()); - static::assertSame('/subfolder/login', $result->getHeader('location')[0]); + static::assertSame( + '/subfolder/login?returnurl=' . urlencode('http://shaarli/subfolder/path'), + $result->getHeader('location')[0] + ); } /** - * Test middleware execution with controller throwing a not authorized exception + * Test middleware execution with controller throwing a not authorized exception. + * The exception should be thrown to be later handled by the error handler. */ - public function testMiddlewareExecutionWithServerExceptionWith(): void + public function testMiddlewareExecutionWithServerException(): void { $request = $this->createMock(Request::class); $request->method('getUri')->willReturnCallback(function (): Uri { @@ -132,9 +148,11 @@ class ShaarliMiddlewareTest extends TestCase return $uri; }); + $dummyException = new class() extends \Exception {}; + $response = new Response(); - $controller = function (): void { - throw new \Exception(); + $controller = function () use ($dummyException): void { + throw $dummyException; }; $parameters = []; @@ -149,12 +167,9 @@ class ShaarliMiddlewareTest extends TestCase }) ; - /** @var Response $result */ - $result = $this->middleware->__invoke($request, $response, $controller); + $this->expectException(get_class($dummyException)); - static::assertSame(500, $result->getStatusCode()); - static::assertContains('error', (string) $result->getBody()); - static::assertSame('An unexpected error occurred.', $parameters['message']); + $this->middleware->__invoke($request, $response, $controller); } public function testMiddlewareExecutionWithUpdates(): void @@ -179,6 +194,7 @@ class ShaarliMiddlewareTest extends TestCase $this->container->conf->method('get')->willReturnCallback(function (string $key): string { return $key; }); + $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE); $this->container->pageCacheManager = $this->createMock(PageCacheManager::class); $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches');