818b3193ff
With the new routes, all pages are not all at the same folder level anymore (e.g. /shaare and /shaare/123), so we can't just use './' everywhere. The most consistent way to handle this is to prefix all path with the proper variable, and handle the actual path in controllers.
85 lines
2.6 KiB
PHP
85 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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 Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
use Slim\Http\Uri;
|
|
|
|
class ShaarliMiddlewareTest extends TestCase
|
|
{
|
|
/** @var ShaarliContainer */
|
|
protected $container;
|
|
|
|
/** @var ShaarliMiddleware */
|
|
protected $middleware;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->container = $this->createMock(ShaarliContainer::class);
|
|
$this->middleware = new ShaarliMiddleware($this->container);
|
|
}
|
|
|
|
public function testMiddlewareExecution(): void
|
|
{
|
|
$request = $this->createMock(Request::class);
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
$uri = $this->createMock(Uri::class);
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
return $uri;
|
|
});
|
|
|
|
$response = new Response();
|
|
$controller = function (Request $request, Response $response): Response {
|
|
return $response->withStatus(418); // I'm a tea pot
|
|
};
|
|
|
|
/** @var Response $result */
|
|
$result = $this->middleware->__invoke($request, $response, $controller);
|
|
|
|
static::assertInstanceOf(Response::class, $result);
|
|
static::assertSame(418, $result->getStatusCode());
|
|
}
|
|
|
|
public function testMiddlewareExecutionWithException(): void
|
|
{
|
|
$request = $this->createMock(Request::class);
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
$uri = $this->createMock(Uri::class);
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
return $uri;
|
|
});
|
|
|
|
$response = new Response();
|
|
$controller = function (): void {
|
|
$exception = new LoginBannedException();
|
|
|
|
throw new $exception;
|
|
};
|
|
|
|
$pageBuilder = $this->createMock(PageBuilder::class);
|
|
$pageBuilder->method('render')->willReturnCallback(function (string $message): string {
|
|
return $message;
|
|
});
|
|
$this->container->pageBuilder = $pageBuilder;
|
|
|
|
$conf = $this->createMock(ConfigManager::class);
|
|
$this->container->conf = $conf;
|
|
|
|
/** @var Response $result */
|
|
$result = $this->middleware->__invoke($request, $response, $controller);
|
|
|
|
static::assertInstanceOf(Response::class, $result);
|
|
static::assertSame(401, $result->getStatusCode());
|
|
static::assertContains('error', (string) $result->getBody());
|
|
}
|
|
}
|