aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/LoginControllerTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-20 12:43:40 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commitdd09ec52b20b4a548ecf5c847627575e506e3a50 (patch)
treeb598b84727df2c604622b4db1dfee831dc7c5dca /tests/front/controller/LoginControllerTest.php
parent5ec4708ced1cdca01eddd7e52377ab5e5f8b3290 (diff)
downloadShaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.tar.gz
Shaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.tar.zst
Shaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.zip
Refactor front controller tests to create container mock using a trait
Diffstat (limited to 'tests/front/controller/LoginControllerTest.php')
-rw-r--r--tests/front/controller/LoginControllerTest.php58
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);
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
10use Shaarli\Container\ShaarliContainer;
11use Shaarli\Front\Exception\LoginBannedException; 9use Shaarli\Front\Exception\LoginBannedException;
12use Shaarli\Plugin\PluginManager;
13use Shaarli\Render\PageBuilder;
14use Shaarli\Security\LoginManager;
15use Slim\Http\Request; 10use Slim\Http\Request;
16use Slim\Http\Response; 11use Slim\Http\Response;
17 12
18class LoginControllerTest extends TestCase 13class 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}