X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ffront%2Fcontroller%2FLoginControllerTest.php;h=21937f3c6605254eeec9c813547fe87b74b0601f;hb=dd09ec52b20b4a548ecf5c847627575e506e3a50;hp=8cf8ece7982397d0e79f0dbeee6d0d3d8b3e82d1;hpb=5ec4708ced1cdca01eddd7e52377ab5e5f8b3290;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/front/controller/LoginControllerTest.php b/tests/front/controller/LoginControllerTest.php index 8cf8ece7..21937f3c 100644 --- a/tests/front/controller/LoginControllerTest.php +++ b/tests/front/controller/LoginControllerTest.php @@ -5,27 +5,22 @@ declare(strict_types=1); namespace Shaarli\Front\Controller; use PHPUnit\Framework\TestCase; -use Shaarli\Bookmark\BookmarkServiceInterface; use Shaarli\Config\ConfigManager; -use Shaarli\Container\ShaarliContainer; use Shaarli\Front\Exception\LoginBannedException; -use Shaarli\Plugin\PluginManager; -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); } @@ -47,6 +42,8 @@ class LoginControllerTest extends TestCase }) ; + $this->container->loginManager->method('canLogin')->willReturn(true); + $result = $this->controller->index($request, $response); static::assertInstanceOf(Response::class, $result); @@ -77,6 +74,8 @@ class LoginControllerTest extends TestCase }) ; + $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true); + $result = $this->controller->index($request, $response); static::assertInstanceOf(Response::class, $result); @@ -91,12 +90,12 @@ class LoginControllerTest extends TestCase 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); @@ -135,44 +134,11 @@ class LoginControllerTest extends TestCase $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; - - $pluginManager = $this->createMock(PluginManager::class); - $this->container->pluginManager = $pluginManager; - $bookmarkService = $this->createMock(BookmarkServiceInterface::class); - $this->container->bookmarkService = $bookmarkService; - } }