diff options
Diffstat (limited to 'tests/front/controller/LoginControllerTest.php')
-rw-r--r-- | tests/front/controller/LoginControllerTest.php | 58 |
1 files changed, 12 insertions, 46 deletions
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); | |||
5 | namespace Shaarli\Front\Controller; | 5 | namespace Shaarli\Front\Controller; |
6 | 6 | ||
7 | use PHPUnit\Framework\TestCase; | 7 | use PHPUnit\Framework\TestCase; |
8 | use Shaarli\Bookmark\BookmarkServiceInterface; | ||
9 | use Shaarli\Config\ConfigManager; | 8 | use Shaarli\Config\ConfigManager; |
10 | use Shaarli\Container\ShaarliContainer; | ||
11 | use Shaarli\Front\Exception\LoginBannedException; | 9 | use Shaarli\Front\Exception\LoginBannedException; |
12 | use Shaarli\Plugin\PluginManager; | ||
13 | use Shaarli\Render\PageBuilder; | ||
14 | use Shaarli\Security\LoginManager; | ||
15 | use Slim\Http\Request; | 10 | use Slim\Http\Request; |
16 | use Slim\Http\Response; | 11 | use Slim\Http\Response; |
17 | 12 | ||
18 | class LoginControllerTest extends TestCase | 13 | class LoginControllerTest extends TestCase |
19 | { | 14 | { |
20 | /** @var ShaarliContainer */ | 15 | use FrontControllerMockHelper; |
21 | protected $container; | ||
22 | 16 | ||
23 | /** @var LoginController */ | 17 | /** @var LoginController */ |
24 | protected $controller; | 18 | protected $controller; |
25 | 19 | ||
26 | public function setUp(): void | 20 | public function setUp(): void |
27 | { | 21 | { |
28 | $this->container = $this->createMock(ShaarliContainer::class); | 22 | $this->createContainer(); |
23 | |||
29 | $this->controller = new LoginController($this->container); | 24 | $this->controller = new LoginController($this->container); |
30 | } | 25 | } |
31 | 26 | ||
@@ -47,6 +42,8 @@ class LoginControllerTest extends TestCase | |||
47 | }) | 42 | }) |
48 | ; | 43 | ; |
49 | 44 | ||
45 | $this->container->loginManager->method('canLogin')->willReturn(true); | ||
46 | |||
50 | $result = $this->controller->index($request, $response); | 47 | $result = $this->controller->index($request, $response); |
51 | 48 | ||
52 | static::assertInstanceOf(Response::class, $result); | 49 | static::assertInstanceOf(Response::class, $result); |
@@ -77,6 +74,8 @@ class LoginControllerTest extends TestCase | |||
77 | }) | 74 | }) |
78 | ; | 75 | ; |
79 | 76 | ||
77 | $this->container->loginManager->expects(static::once())->method('canLogin')->willReturn(true); | ||
78 | |||
80 | $result = $this->controller->index($request, $response); | 79 | $result = $this->controller->index($request, $response); |
81 | 80 | ||
82 | static::assertInstanceOf(Response::class, $result); | 81 | static::assertInstanceOf(Response::class, $result); |
@@ -91,12 +90,12 @@ class LoginControllerTest extends TestCase | |||
91 | 90 | ||
92 | public function testLoginControllerWhileLoggedIn(): void | 91 | public function testLoginControllerWhileLoggedIn(): void |
93 | { | 92 | { |
93 | $this->createValidContainerMockSet(); | ||
94 | |||
94 | $request = $this->createMock(Request::class); | 95 | $request = $this->createMock(Request::class); |
95 | $response = new Response(); | 96 | $response = new Response(); |
96 | 97 | ||
97 | $loginManager = $this->createMock(LoginManager::class); | 98 | $this->container->loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true); |
98 | $loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true); | ||
99 | $this->container->loginManager = $loginManager; | ||
100 | 99 | ||
101 | $result = $this->controller->index($request, $response); | 100 | $result = $this->controller->index($request, $response); |
102 | 101 | ||
@@ -135,44 +134,11 @@ class LoginControllerTest extends TestCase | |||
135 | $request = $this->createMock(Request::class); | 134 | $request = $this->createMock(Request::class); |
136 | $response = new Response(); | 135 | $response = new Response(); |
137 | 136 | ||
138 | $loginManager = $this->createMock(LoginManager::class); | 137 | $this->container->loginManager->method('isLoggedIn')->willReturn(false); |
139 | $loginManager->method('isLoggedIn')->willReturn(false); | 138 | $this->container->loginManager->method('canLogin')->willReturn(false); |
140 | $loginManager->method('canLogin')->willReturn(false); | ||
141 | $this->container->loginManager = $loginManager; | ||
142 | 139 | ||
143 | $this->expectException(LoginBannedException::class); | 140 | $this->expectException(LoginBannedException::class); |
144 | 141 | ||
145 | $this->controller->index($request, $response); | 142 | $this->controller->index($request, $response); |
146 | } | 143 | } |
147 | |||
148 | protected function createValidContainerMockSet(): void | ||
149 | { | ||
150 | // User logged out | ||
151 | $loginManager = $this->createMock(LoginManager::class); | ||
152 | $loginManager->method('isLoggedIn')->willReturn(false); | ||
153 | $loginManager->method('canLogin')->willReturn(true); | ||
154 | $this->container->loginManager = $loginManager; | ||
155 | |||
156 | // Config | ||
157 | $conf = $this->createMock(ConfigManager::class); | ||
158 | $conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
159 | return $default; | ||
160 | }); | ||
161 | $this->container->conf = $conf; | ||
162 | |||
163 | // PageBuilder | ||
164 | $pageBuilder = $this->createMock(PageBuilder::class); | ||
165 | $pageBuilder | ||
166 | ->method('render') | ||
167 | ->willReturnCallback(function (string $template): string { | ||
168 | return $template; | ||
169 | }) | ||
170 | ; | ||
171 | $this->container->pageBuilder = $pageBuilder; | ||
172 | |||
173 | $pluginManager = $this->createMock(PluginManager::class); | ||
174 | $this->container->pluginManager = $pluginManager; | ||
175 | $bookmarkService = $this->createMock(BookmarkServiceInterface::class); | ||
176 | $this->container->bookmarkService = $bookmarkService; | ||
177 | } | ||
178 | } | 144 | } |