use PHPUnit\Framework\TestCase;
use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
use Shaarli\Front\Exception\LoginBannedException;
-use Shaarli\Render\PageBuilder;
-use Shaarli\Security\LoginManager;
use Slim\Http\Request;
use Slim\Http\Response;
class LoginControllerTest extends TestCase
{
- /** @var ShaarliContainer */
- protected $container;
+ use FrontControllerMockHelper;
/** @var LoginController */
protected $controller;
public function setUp(): void
{
- $this->container = $this->createMock(ShaarliContainer::class);
+ $this->createContainer();
+
$this->controller = new LoginController($this->container);
}
$assignedVariables = [];
$this->container->pageBuilder
- ->expects(static::exactly(3))
->method('assign')
->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
$assignedVariables[$key] = $value;
})
;
+ $this->container->loginManager->method('canLogin')->willReturn(true);
+
$result = $this->controller->index($request, $response);
static::assertInstanceOf(Response::class, $result);
$assignedVariables = [];
$this->container->pageBuilder
- ->expects(static::exactly(4))
->method('assign')
->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
$assignedVariables[$key] = $value;
})
;
+ $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true);
+
$result = $this->controller->index($request, $response);
static::assertInstanceOf(Response::class, $result);
public function testLoginControllerWhileLoggedIn(): void
{
+ $this->createValidContainerMockSet();
+
$request = $this->createMock(Request::class);
$response = new Response();
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
- $this->container->loginManager = $loginManager;
+ $this->container->loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
$result = $this->controller->index($request, $response);
$request = $this->createMock(Request::class);
$response = new Response();
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->method('isLoggedIn')->willReturn(false);
- $loginManager->method('canLogin')->willReturn(false);
- $this->container->loginManager = $loginManager;
+ $this->container->loginManager->method('isLoggedIn')->willReturn(false);
+ $this->container->loginManager->method('canLogin')->willReturn(false);
$this->expectException(LoginBannedException::class);
$this->controller->index($request, $response);
}
-
- protected function createValidContainerMockSet(): void
- {
- // User logged out
- $loginManager = $this->createMock(LoginManager::class);
- $loginManager->method('isLoggedIn')->willReturn(false);
- $loginManager->method('canLogin')->willReturn(true);
- $this->container->loginManager = $loginManager;
-
- // Config
- $conf = $this->createMock(ConfigManager::class);
- $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
- return $default;
- });
- $this->container->conf = $conf;
-
- // PageBuilder
- $pageBuilder = $this->createMock(PageBuilder::class);
- $pageBuilder
- ->method('render')
- ->willReturnCallback(function (string $template): string {
- return $template;
- })
- ;
- $this->container->pageBuilder = $pageBuilder;
- }
}