namespace Shaarli\Front;
-use PHPUnit\Framework\TestCase;
use Shaarli\Config\ConfigManager;
use Shaarli\Container\ShaarliContainer;
use Shaarli\Front\Exception\LoginBannedException;
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;
$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()
+ public function tearDown(): void
{
unlink(static::TMP_MOCK_FILE);
}
}
/**
- * 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
{
});
$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
{
$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 {
return $uri;
});
+ $dummyException = new class() extends \Exception {};
+
$response = new Response();
- $controller = function (): void {
- throw new \Exception();
+ $controller = function () use ($dummyException): void {
+ throw $dummyException;
};
$parameters = [];
})
;
- /** @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