aboutsummaryrefslogtreecommitdiffhomepage
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
parent5ec4708ced1cdca01eddd7e52377ab5e5f8b3290 (diff)
downloadShaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.tar.gz
Shaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.tar.zst
Shaarli-dd09ec52b20b4a548ecf5c847627575e506e3a50.zip
Refactor front controller tests to create container mock using a trait
-rw-r--r--tests/bootstrap.php2
-rw-r--r--tests/container/ShaarliTestContainer.php38
-rw-r--r--tests/front/controller/DailyControllerTest.php84
-rw-r--r--tests/front/controller/FeedControllerTest.php78
-rw-r--r--tests/front/controller/FrontControllerMockHelper.php114
-rw-r--r--tests/front/controller/LoginControllerTest.php58
-rw-r--r--tests/front/controller/LogoutControllerTest.php19
-rw-r--r--tests/front/controller/OpenSearchControllerTest.php60
-rw-r--r--tests/front/controller/PictureWallControllerTest.php65
-rw-r--r--tests/front/controller/ShaarliControllerTest.php68
-rw-r--r--tests/front/controller/TagCloudControllerTest.php62
-rw-r--r--tests/front/controller/TagControllerTest.php49
12 files changed, 232 insertions, 465 deletions
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index c80bcb33..6bb345c2 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -21,3 +21,5 @@ require_once 'application/http/HttpUtils.php';
21require_once 'tests/utils/ReferenceLinkDB.php'; 21require_once 'tests/utils/ReferenceLinkDB.php';
22require_once 'tests/utils/ReferenceHistory.php'; 22require_once 'tests/utils/ReferenceHistory.php';
23require_once 'tests/utils/FakeBookmarkService.php'; 23require_once 'tests/utils/FakeBookmarkService.php';
24require_once 'tests/container/ShaarliTestContainer.php';
25require_once 'tests/front/controller/FrontControllerMockHelper.php';
diff --git a/tests/container/ShaarliTestContainer.php b/tests/container/ShaarliTestContainer.php
new file mode 100644
index 00000000..53197ae6
--- /dev/null
+++ b/tests/container/ShaarliTestContainer.php
@@ -0,0 +1,38 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Container;
6
7use PHPUnit\Framework\MockObject\MockObject;
8use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager;
10use Shaarli\Feed\FeedBuilder;
11use Shaarli\Formatter\FormatterFactory;
12use Shaarli\History;
13use Shaarli\Plugin\PluginManager;
14use Shaarli\Render\PageBuilder;
15use Shaarli\Render\PageCacheManager;
16use Shaarli\Security\LoginManager;
17use Shaarli\Security\SessionManager;
18
19/**
20 * Test helper allowing auto-completion for MockObjects.
21 *
22 * @property mixed[] $environment $_SERVER automatically injected by Slim
23 * @property MockObject|ConfigManager $conf
24 * @property MockObject|SessionManager $sessionManager
25 * @property MockObject|LoginManager $loginManager
26 * @property MockObject|string $webPath
27 * @property MockObject|History $history
28 * @property MockObject|BookmarkServiceInterface $bookmarkService
29 * @property MockObject|PageBuilder $pageBuilder
30 * @property MockObject|PluginManager $pluginManager
31 * @property MockObject|FormatterFactory $formatterFactory
32 * @property MockObject|PageCacheManager $pageCacheManager
33 * @property MockObject|FeedBuilder $feedBuilder
34 */
35class ShaarliTestContainer extends ShaarliContainer
36{
37
38}
diff --git a/tests/front/controller/DailyControllerTest.php b/tests/front/controller/DailyControllerTest.php
index 88ec116b..7ec99030 100644
--- a/tests/front/controller/DailyControllerTest.php
+++ b/tests/front/controller/DailyControllerTest.php
@@ -6,31 +6,21 @@ namespace Shaarli\Front\Controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\Bookmark; 8use Shaarli\Bookmark\Bookmark;
9use Shaarli\Bookmark\BookmarkServiceInterface;
10use Shaarli\Config\ConfigManager;
11use Shaarli\Container\ShaarliContainer;
12use Shaarli\Feed\CachedPage; 9use Shaarli\Feed\CachedPage;
13use Shaarli\Formatter\BookmarkFormatter;
14use Shaarli\Formatter\BookmarkRawFormatter;
15use Shaarli\Formatter\FormatterFactory;
16use Shaarli\Plugin\PluginManager;
17use Shaarli\Render\PageBuilder;
18use Shaarli\Render\PageCacheManager;
19use Shaarli\Security\LoginManager;
20use Slim\Http\Request; 10use Slim\Http\Request;
21use Slim\Http\Response; 11use Slim\Http\Response;
22 12
23class DailyControllerTest extends TestCase 13class DailyControllerTest extends TestCase
24{ 14{
25 /** @var ShaarliContainer */ 15 use FrontControllerMockHelper;
26 protected $container;
27 16
28 /** @var DailyController */ 17 /** @var DailyController */
29 protected $controller; 18 protected $controller;
30 19
31 public function setUp(): void 20 public function setUp(): void
32 { 21 {
33 $this->container = $this->createMock(ShaarliContainer::class); 22 $this->createContainer();
23
34 $this->controller = new DailyController($this->container); 24 $this->controller = new DailyController($this->container);
35 DailyController::$DAILY_RSS_NB_DAYS = 2; 25 DailyController::$DAILY_RSS_NB_DAYS = 2;
36 } 26 }
@@ -105,7 +95,8 @@ class DailyControllerTest extends TestCase
105 static::assertArrayHasKey('loggedin', $param); 95 static::assertArrayHasKey('loggedin', $param);
106 96
107 return $data; 97 return $data;
108 }); 98 })
99 ;
109 100
110 $result = $this->controller->index($request, $response); 101 $result = $this->controller->index($request, $response);
111 102
@@ -497,71 +488,6 @@ class DailyControllerTest extends TestCase
497 static::assertCount(0, $assignedVariables['days']); 488 static::assertCount(0, $assignedVariables['days']);
498 } 489 }
499 490
500 protected function createValidContainerMockSet(): void
501 {
502 $loginManager = $this->createMock(LoginManager::class);
503 $this->container->loginManager = $loginManager;
504
505 // Config
506 $conf = $this->createMock(ConfigManager::class);
507 $this->container->conf = $conf;
508 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
509 return $default;
510 });
511
512 // PageBuilder
513 $pageBuilder = $this->createMock(PageBuilder::class);
514 $pageBuilder
515 ->method('render')
516 ->willReturnCallback(function (string $template): string {
517 return $template;
518 })
519 ;
520 $this->container->pageBuilder = $pageBuilder;
521
522 // Plugin Manager
523 $pluginManager = $this->createMock(PluginManager::class);
524 $this->container->pluginManager = $pluginManager;
525
526 // BookmarkService
527 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
528 $this->container->bookmarkService = $bookmarkService;
529
530 // Formatter
531 $formatterFactory = $this->createMock(FormatterFactory::class);
532 $formatterFactory
533 ->method('getFormatter')
534 ->willReturnCallback(function (): BookmarkFormatter {
535 return new BookmarkRawFormatter($this->container->conf, true);
536 })
537 ;
538 $this->container->formatterFactory = $formatterFactory;
539
540 // CacheManager
541 $pageCacheManager = $this->createMock(PageCacheManager::class);
542 $this->container->pageCacheManager = $pageCacheManager;
543
544 // $_SERVER
545 $this->container->environment = [
546 'SERVER_NAME' => 'shaarli',
547 'SERVER_PORT' => '80',
548 'REQUEST_URI' => '/daily-rss',
549 ];
550 }
551
552 protected function assignTemplateVars(array &$variables): void
553 {
554 $this->container->pageBuilder
555 ->expects(static::atLeastOnce())
556 ->method('assign')
557 ->willReturnCallback(function ($key, $value) use (&$variables) {
558 $variables[$key] = $value;
559
560 return $this;
561 })
562 ;
563 }
564
565 protected static function generateContent(int $length): string 491 protected static function generateContent(int $length): string
566 { 492 {
567 // bin2hex(random_bytes) generates string twice as long as given parameter 493 // bin2hex(random_bytes) generates string twice as long as given parameter
diff --git a/tests/front/controller/FeedControllerTest.php b/tests/front/controller/FeedControllerTest.php
index d4cc5536..7e8657e2 100644
--- a/tests/front/controller/FeedControllerTest.php
+++ b/tests/front/controller/FeedControllerTest.php
@@ -5,29 +5,23 @@ 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;
10use Shaarli\Container\ShaarliContainer;
11use Shaarli\Feed\FeedBuilder; 8use Shaarli\Feed\FeedBuilder;
12use Shaarli\Formatter\FormatterFactory;
13use Shaarli\Plugin\PluginManager;
14use Shaarli\Render\PageBuilder;
15use Shaarli\Render\PageCacheManager;
16use Shaarli\Security\LoginManager;
17use Slim\Http\Request; 9use Slim\Http\Request;
18use Slim\Http\Response; 10use Slim\Http\Response;
19 11
20class FeedControllerTest extends TestCase 12class FeedControllerTest extends TestCase
21{ 13{
22 /** @var ShaarliContainer */ 14 use FrontControllerMockHelper;
23 protected $container;
24 15
25 /** @var FeedController */ 16 /** @var FeedController */
26 protected $controller; 17 protected $controller;
27 18
28 public function setUp(): void 19 public function setUp(): void
29 { 20 {
30 $this->container = $this->createMock(ShaarliContainer::class); 21 $this->createContainer();
22
23 $this->container->feedBuilder = $this->createMock(FeedBuilder::class);
24
31 $this->controller = new FeedController($this->container); 25 $this->controller = new FeedController($this->container);
32 } 26 }
33 27
@@ -154,66 +148,4 @@ class FeedControllerTest extends TestCase
154 static::assertSame('feed.atom', (string) $result->getBody()); 148 static::assertSame('feed.atom', (string) $result->getBody());
155 static::assertSame('data', $assignedVariables['content']); 149 static::assertSame('data', $assignedVariables['content']);
156 } 150 }
157
158 protected function createValidContainerMockSet(): void
159 {
160 $loginManager = $this->createMock(LoginManager::class);
161 $this->container->loginManager = $loginManager;
162
163 // Config
164 $conf = $this->createMock(ConfigManager::class);
165 $this->container->conf = $conf;
166 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
167 return $default;
168 });
169
170 // PageBuilder
171 $pageBuilder = $this->createMock(PageBuilder::class);
172 $pageBuilder
173 ->method('render')
174 ->willReturnCallback(function (string $template): string {
175 return $template;
176 })
177 ;
178 $this->container->pageBuilder = $pageBuilder;
179
180 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
181 $this->container->bookmarkService = $bookmarkService;
182
183 // Plugin Manager
184 $pluginManager = $this->createMock(PluginManager::class);
185 $this->container->pluginManager = $pluginManager;
186
187 // Formatter
188 $formatterFactory = $this->createMock(FormatterFactory::class);
189 $this->container->formatterFactory = $formatterFactory;
190
191 // CacheManager
192 $pageCacheManager = $this->createMock(PageCacheManager::class);
193 $this->container->pageCacheManager = $pageCacheManager;
194
195 // FeedBuilder
196 $feedBuilder = $this->createMock(FeedBuilder::class);
197 $this->container->feedBuilder = $feedBuilder;
198
199 // $_SERVER
200 $this->container->environment = [
201 'SERVER_NAME' => 'shaarli',
202 'SERVER_PORT' => '80',
203 'REQUEST_URI' => '/daily-rss',
204 ];
205 }
206
207 protected function assignTemplateVars(array &$variables): void
208 {
209 $this->container->pageBuilder
210 ->expects(static::atLeastOnce())
211 ->method('assign')
212 ->willReturnCallback(function ($key, $value) use (&$variables) {
213 $variables[$key] = $value;
214
215 return $this;
216 })
217 ;
218 }
219} 151}
diff --git a/tests/front/controller/FrontControllerMockHelper.php b/tests/front/controller/FrontControllerMockHelper.php
new file mode 100644
index 00000000..b65607e7
--- /dev/null
+++ b/tests/front/controller/FrontControllerMockHelper.php
@@ -0,0 +1,114 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use PHPUnit\Framework\MockObject\MockObject;
8use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager;
10use Shaarli\Container\ShaarliTestContainer;
11use Shaarli\Formatter\BookmarkFormatter;
12use Shaarli\Formatter\BookmarkRawFormatter;
13use Shaarli\Formatter\FormatterFactory;
14use Shaarli\Plugin\PluginManager;
15use Shaarli\Render\PageBuilder;
16use Shaarli\Render\PageCacheManager;
17use Shaarli\Security\LoginManager;
18use Shaarli\Security\SessionManager;
19
20/**
21 * Trait FrontControllerMockHelper
22 *
23 * Helper trait used to initialize the ShaarliContainer and mock its services for controller tests.
24 *
25 * @property ShaarliTestContainer $container
26 * @package Shaarli\Front\Controller
27 */
28trait FrontControllerMockHelper
29{
30 /** @var ShaarliTestContainer */
31 protected $container;
32
33 /**
34 * Mock the container instance
35 */
36 protected function createContainer(): void
37 {
38 $this->container = $this->createMock(ShaarliTestContainer::class);
39 }
40
41 /**
42 * Initialize container's services used by tests
43 */
44 protected function createValidContainerMockSet(): void
45 {
46 $this->container->loginManager = $this->createMock(LoginManager::class);
47
48 // Config
49 $this->container->conf = $this->createMock(ConfigManager::class);
50 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
51 return $default;
52 });
53
54 // PageBuilder
55 $this->container->pageBuilder = $this->createMock(PageBuilder::class);
56 $this->container->pageBuilder
57 ->method('render')
58 ->willReturnCallback(function (string $template): string {
59 return $template;
60 })
61 ;
62
63 // Plugin Manager
64 $this->container->pluginManager = $this->createMock(PluginManager::class);
65
66 // BookmarkService
67 $this->container->bookmarkService = $this->createMock(BookmarkServiceInterface::class);
68
69 // Formatter
70 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
71 $this->container->formatterFactory
72 ->method('getFormatter')
73 ->willReturnCallback(function (): BookmarkFormatter {
74 return new BookmarkRawFormatter($this->container->conf, true);
75 })
76 ;
77
78 // CacheManager
79 $this->container->pageCacheManager = $this->createMock(PageCacheManager::class);
80
81 // SessionManager
82 $this->container->sessionManager = $this->createMock(SessionManager::class);
83
84 // $_SERVER
85 $this->container->environment = [
86 'SERVER_NAME' => 'shaarli',
87 'SERVER_PORT' => '80',
88 'REQUEST_URI' => '/daily-rss',
89 ];
90 }
91
92 /**
93 * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
94 *
95 * @param mixed $variables Array reference to populate.
96 */
97 protected function assignTemplateVars(array &$variables): void
98 {
99 $this->container->pageBuilder
100 ->expects(static::atLeastOnce())
101 ->method('assign')
102 ->willReturnCallback(function ($key, $value) use (&$variables) {
103 $variables[$key] = $value;
104
105 return $this;
106 })
107 ;
108 }
109
110 /**
111 * Force to be used in PHPUnit context.
112 */
113 protected abstract function createMock($originalClassName): MockObject;
114}
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}
diff --git a/tests/front/controller/LogoutControllerTest.php b/tests/front/controller/LogoutControllerTest.php
index d9ca1c25..8e01c367 100644
--- a/tests/front/controller/LogoutControllerTest.php
+++ b/tests/front/controller/LogoutControllerTest.php
@@ -12,8 +12,6 @@ if (!function_exists('Shaarli\Front\Controller\setcookie')) {
12} 12}
13 13
14use PHPUnit\Framework\TestCase; 14use PHPUnit\Framework\TestCase;
15use Shaarli\Container\ShaarliContainer;
16use Shaarli\Render\PageCacheManager;
17use Shaarli\Security\LoginManager; 15use Shaarli\Security\LoginManager;
18use Shaarli\Security\SessionManager; 16use Shaarli\Security\SessionManager;
19use Slim\Http\Request; 17use Slim\Http\Request;
@@ -21,15 +19,15 @@ use Slim\Http\Response;
21 19
22class LogoutControllerTest extends TestCase 20class LogoutControllerTest extends TestCase
23{ 21{
24 /** @var ShaarliContainer */ 22 use FrontControllerMockHelper;
25 protected $container;
26 23
27 /** @var LogoutController */ 24 /** @var LogoutController */
28 protected $controller; 25 protected $controller;
29 26
30 public function setUp(): void 27 public function setUp(): void
31 { 28 {
32 $this->container = $this->createMock(ShaarliContainer::class); 29 $this->createContainer();
30
33 $this->controller = new LogoutController($this->container); 31 $this->controller = new LogoutController($this->container);
34 32
35 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there'); 33 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
@@ -37,16 +35,15 @@ class LogoutControllerTest extends TestCase
37 35
38 public function testValidControllerInvoke(): void 36 public function testValidControllerInvoke(): void
39 { 37 {
38 $this->createValidContainerMockSet();
39
40 $request = $this->createMock(Request::class); 40 $request = $this->createMock(Request::class);
41 $response = new Response(); 41 $response = new Response();
42 42
43 $pageCacheManager = $this->createMock(PageCacheManager::class); 43 $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches');
44 $pageCacheManager->expects(static::once())->method('invalidateCaches');
45 $this->container->pageCacheManager = $pageCacheManager;
46 44
47 $sessionManager = $this->createMock(SessionManager::class); 45 $this->container->sessionManager = $this->createMock(SessionManager::class);
48 $sessionManager->expects(static::once())->method('logout'); 46 $this->container->sessionManager->expects(static::once())->method('logout');
49 $this->container->sessionManager = $sessionManager;
50 47
51 static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]); 48 static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
52 49
diff --git a/tests/front/controller/OpenSearchControllerTest.php b/tests/front/controller/OpenSearchControllerTest.php
index 7ba0f7df..f3b6f439 100644
--- a/tests/front/controller/OpenSearchControllerTest.php
+++ b/tests/front/controller/OpenSearchControllerTest.php
@@ -5,26 +5,22 @@ declare(strict_types=1);
5namespace front\controller; 5namespace front\controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkServiceInterface; 8use Shaarli\Front\Controller\FrontControllerMockHelper;
9use Shaarli\Container\ShaarliContainer;
10use Shaarli\Front\Controller\OpenSearchController; 9use Shaarli\Front\Controller\OpenSearchController;
11use Shaarli\Plugin\PluginManager;
12use Shaarli\Render\PageBuilder;
13use Shaarli\Security\LoginManager;
14use Slim\Http\Request; 10use Slim\Http\Request;
15use Slim\Http\Response; 11use Slim\Http\Response;
16 12
17class OpenSearchControllerTest extends TestCase 13class OpenSearchControllerTest extends TestCase
18{ 14{
19 /** @var ShaarliContainer */ 15 use FrontControllerMockHelper;
20 protected $container;
21 16
22 /** @var OpenSearchController */ 17 /** @var OpenSearchController */
23 protected $controller; 18 protected $controller;
24 19
25 public function setUp(): void 20 public function setUp(): void
26 { 21 {
27 $this->container = $this->createMock(ShaarliContainer::class); 22 $this->createContainer();
23
28 $this->controller = new OpenSearchController($this->container); 24 $this->controller = new OpenSearchController($this->container);
29 } 25 }
30 26
@@ -42,51 +38,11 @@ class OpenSearchControllerTest extends TestCase
42 $result = $this->controller->index($request, $response); 38 $result = $this->controller->index($request, $response);
43 39
44 static::assertSame(200, $result->getStatusCode()); 40 static::assertSame(200, $result->getStatusCode());
45 static::assertStringContainsString('application/xml', $result->getHeader('Content-Type')[0]); 41 static::assertStringContainsString(
42 'application/opensearchdescription+xml',
43 $result->getHeader('Content-Type')[0]
44 );
46 static::assertSame('opensearch', (string) $result->getBody()); 45 static::assertSame('opensearch', (string) $result->getBody());
47 static::assertSame('http://shaarli', $assignedVariables['serverurl']); 46 static::assertSame('http://shaarli', $assignedVariables['serverurl']);
48 } 47 }
49
50 protected function createValidContainerMockSet(): void
51 {
52 $loginManager = $this->createMock(LoginManager::class);
53 $this->container->loginManager = $loginManager;
54
55 // PageBuilder
56 $pageBuilder = $this->createMock(PageBuilder::class);
57 $pageBuilder
58 ->method('render')
59 ->willReturnCallback(function (string $template): string {
60 return $template;
61 })
62 ;
63 $this->container->pageBuilder = $pageBuilder;
64
65 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
66 $this->container->bookmarkService = $bookmarkService;
67
68 // Plugin Manager
69 $pluginManager = $this->createMock(PluginManager::class);
70 $this->container->pluginManager = $pluginManager;
71
72 // $_SERVER
73 $this->container->environment = [
74 'SERVER_NAME' => 'shaarli',
75 'SERVER_PORT' => '80',
76 'REQUEST_URI' => '/open-search',
77 ];
78 }
79
80 protected function assignTemplateVars(array &$variables): void
81 {
82 $this->container->pageBuilder
83 ->expects(static::atLeastOnce())
84 ->method('assign')
85 ->willReturnCallback(function ($key, $value) use (&$variables) {
86 $variables[$key] = $value;
87
88 return $this;
89 })
90 ;
91 }
92} 48}
diff --git a/tests/front/controller/PictureWallControllerTest.php b/tests/front/controller/PictureWallControllerTest.php
index 63802abd..8160bb38 100644
--- a/tests/front/controller/PictureWallControllerTest.php
+++ b/tests/front/controller/PictureWallControllerTest.php
@@ -6,31 +6,23 @@ namespace Shaarli\Front\Controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\Bookmark; 8use Shaarli\Bookmark\Bookmark;
9use Shaarli\Bookmark\BookmarkServiceInterface;
10use Shaarli\Config\ConfigManager; 9use Shaarli\Config\ConfigManager;
11use Shaarli\Container\ShaarliContainer;
12use Shaarli\Formatter\BookmarkFormatter;
13use Shaarli\Formatter\BookmarkRawFormatter;
14use Shaarli\Formatter\FormatterFactory;
15use Shaarli\Front\Exception\ThumbnailsDisabledException; 10use Shaarli\Front\Exception\ThumbnailsDisabledException;
16use Shaarli\Plugin\PluginManager;
17use Shaarli\Render\PageBuilder;
18use Shaarli\Security\LoginManager;
19use Shaarli\Thumbnailer; 11use Shaarli\Thumbnailer;
20use Slim\Http\Request; 12use Slim\Http\Request;
21use Slim\Http\Response; 13use Slim\Http\Response;
22 14
23class PictureWallControllerTest extends TestCase 15class PictureWallControllerTest extends TestCase
24{ 16{
25 /** @var ShaarliContainer */ 17 use FrontControllerMockHelper;
26 protected $container;
27 18
28 /** @var PictureWallController */ 19 /** @var PictureWallController */
29 protected $controller; 20 protected $controller;
30 21
31 public function setUp(): void 22 public function setUp(): void
32 { 23 {
33 $this->container = $this->createMock(ShaarliContainer::class); 24 $this->createContainer();
25
34 $this->controller = new PictureWallController($this->container); 26 $this->controller = new PictureWallController($this->container);
35 } 27 }
36 28
@@ -43,6 +35,7 @@ class PictureWallControllerTest extends TestCase
43 $response = new Response(); 35 $response = new Response();
44 36
45 // ConfigManager: thumbnails are enabled 37 // ConfigManager: thumbnails are enabled
38 $this->container->conf = $this->createMock(ConfigManager::class);
46 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { 39 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
47 if ($parameter === 'thumbnails.mode') { 40 if ($parameter === 'thumbnails.mode') {
48 return Thumbnailer::MODE_COMMON; 41 return Thumbnailer::MODE_COMMON;
@@ -53,15 +46,7 @@ class PictureWallControllerTest extends TestCase
53 46
54 // Save RainTPL assigned variables 47 // Save RainTPL assigned variables
55 $assignedVariables = []; 48 $assignedVariables = [];
56 $this->container->pageBuilder 49 $this->assignTemplateVars($assignedVariables);
57 ->expects(static::atLeastOnce())
58 ->method('assign')
59 ->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
60 $assignedVariables[$key] = $value;
61
62 return $this;
63 })
64 ;
65 50
66 // Links dataset: 2 links with thumbnails 51 // Links dataset: 2 links with thumbnails
67 $this->container->bookmarkService 52 $this->container->bookmarkService
@@ -137,44 +122,4 @@ class PictureWallControllerTest extends TestCase
137 122
138 $this->controller->index($request, $response); 123 $this->controller->index($request, $response);
139 } 124 }
140
141 protected function createValidContainerMockSet(): void
142 {
143 $loginManager = $this->createMock(LoginManager::class);
144 $this->container->loginManager = $loginManager;
145
146 // Config
147 $conf = $this->createMock(ConfigManager::class);
148 $this->container->conf = $conf;
149
150 // PageBuilder
151 $pageBuilder = $this->createMock(PageBuilder::class);
152 $pageBuilder
153 ->method('render')
154 ->willReturnCallback(function (string $template): string {
155 return $template;
156 })
157 ;
158 $this->container->pageBuilder = $pageBuilder;
159
160 // Plugin Manager
161 $pluginManager = $this->createMock(PluginManager::class);
162 $this->container->pluginManager = $pluginManager;
163
164 // BookmarkService
165 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
166 $this->container->bookmarkService = $bookmarkService;
167
168 // Formatter
169 $formatterFactory = $this->createMock(FormatterFactory::class);
170 $formatterFactory
171 ->method('getFormatter')
172 ->willReturnCallback(function (string $type): BookmarkFormatter {
173 if ($type === 'raw') {
174 return new BookmarkRawFormatter($this->container->conf, true);
175 }
176 })
177 ;
178 $this->container->formatterFactory = $formatterFactory;
179 }
180} 125}
diff --git a/tests/front/controller/ShaarliControllerTest.php b/tests/front/controller/ShaarliControllerTest.php
index 6fa3feb9..3efe4d95 100644
--- a/tests/front/controller/ShaarliControllerTest.php
+++ b/tests/front/controller/ShaarliControllerTest.php
@@ -6,11 +6,6 @@ namespace Shaarli\Front\Controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter; 8use Shaarli\Bookmark\BookmarkFilter;
9use Shaarli\Bookmark\BookmarkServiceInterface;
10use Shaarli\Container\ShaarliContainer;
11use Shaarli\Plugin\PluginManager;
12use Shaarli\Render\PageBuilder;
13use Shaarli\Security\LoginManager;
14 9
15/** 10/**
16 * Class ShaarliControllerTest 11 * Class ShaarliControllerTest
@@ -20,8 +15,7 @@ use Shaarli\Security\LoginManager;
20 */ 15 */
21class ShaarliControllerTest extends TestCase 16class ShaarliControllerTest extends TestCase
22{ 17{
23 /** @var ShaarliContainer */ 18 use FrontControllerMockHelper;
24 protected $container;
25 19
26 /** @var LoginController */ 20 /** @var LoginController */
27 protected $controller; 21 protected $controller;
@@ -31,7 +25,8 @@ class ShaarliControllerTest extends TestCase
31 25
32 public function setUp(): void 26 public function setUp(): void
33 { 27 {
34 $this->container = $this->createMock(ShaarliContainer::class); 28 $this->createContainer();
29
35 $this->controller = new class($this->container) extends ShaarliController 30 $this->controller = new class($this->container) extends ShaarliController
36 { 31 {
37 public function assignView(string $key, $value): ShaarliController 32 public function assignView(string $key, $value): ShaarliController
@@ -51,6 +46,8 @@ class ShaarliControllerTest extends TestCase
51 { 46 {
52 $this->createValidContainerMockSet(); 47 $this->createValidContainerMockSet();
53 48
49 $this->assignTemplateVars($this->assignedValues);
50
54 $self = $this->controller->assignView('variableName', 'variableValue'); 51 $self = $this->controller->assignView('variableName', 'variableValue');
55 52
56 static::assertInstanceOf(ShaarliController::class, $self); 53 static::assertInstanceOf(ShaarliController::class, $self);
@@ -61,6 +58,24 @@ class ShaarliControllerTest extends TestCase
61 { 58 {
62 $this->createValidContainerMockSet(); 59 $this->createValidContainerMockSet();
63 60
61 $this->assignTemplateVars($this->assignedValues);
62
63 $this->container->bookmarkService
64 ->method('count')
65 ->willReturnCallback(function (string $visibility): int {
66 return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
67 })
68 ;
69
70 $this->container->pluginManager
71 ->method('executeHooks')
72 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
73 return $data[$hook] = $params;
74 });
75 $this->container->pluginManager->method('getErrors')->willReturn(['error']);
76
77 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
78
64 $render = $this->controller->render('templateName'); 79 $render = $this->controller->render('templateName');
65 80
66 static::assertSame('templateName', $render); 81 static::assertSame('templateName', $render);
@@ -76,41 +91,4 @@ class ShaarliControllerTest extends TestCase
76 static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']); 91 static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
77 static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']); 92 static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
78 } 93 }
79
80 protected function createValidContainerMockSet(): void
81 {
82 $pageBuilder = $this->createMock(PageBuilder::class);
83 $pageBuilder
84 ->method('assign')
85 ->willReturnCallback(function (string $key, $value): void {
86 $this->assignedValues[$key] = $value;
87 });
88 $pageBuilder
89 ->method('render')
90 ->willReturnCallback(function (string $template): string {
91 return $template;
92 });
93 $this->container->pageBuilder = $pageBuilder;
94
95 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
96 $bookmarkService
97 ->method('count')
98 ->willReturnCallback(function (string $visibility): int {
99 return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
100 });
101 $this->container->bookmarkService = $bookmarkService;
102
103 $pluginManager = $this->createMock(PluginManager::class);
104 $pluginManager
105 ->method('executeHooks')
106 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
107 return $data[$hook] = $params;
108 });
109 $pluginManager->method('getErrors')->willReturn(['error']);
110 $this->container->pluginManager = $pluginManager;
111
112 $loginManager = $this->createMock(LoginManager::class);
113 $loginManager->method('isLoggedIn')->willReturn(true);
114 $this->container->loginManager = $loginManager;
115 }
116} 94}
diff --git a/tests/front/controller/TagCloudControllerTest.php b/tests/front/controller/TagCloudControllerTest.php
index 719610d7..8c27900d 100644
--- a/tests/front/controller/TagCloudControllerTest.php
+++ b/tests/front/controller/TagCloudControllerTest.php
@@ -6,27 +6,20 @@ namespace Shaarli\Front\Controller;
6 6
7use PHPUnit\Framework\TestCase; 7use PHPUnit\Framework\TestCase;
8use Shaarli\Bookmark\BookmarkFilter; 8use Shaarli\Bookmark\BookmarkFilter;
9use Shaarli\Bookmark\BookmarkServiceInterface;
10use Shaarli\Config\ConfigManager;
11use Shaarli\Container\ShaarliContainer;
12use Shaarli\Plugin\PluginManager;
13use Shaarli\Render\PageBuilder;
14use Shaarli\Security\LoginManager;
15use Shaarli\Security\SessionManager;
16use Slim\Http\Request; 9use Slim\Http\Request;
17use Slim\Http\Response; 10use Slim\Http\Response;
18 11
19class TagCloudControllerTest extends TestCase 12class TagCloudControllerTest extends TestCase
20{ 13{
21 /** @var ShaarliContainer */ 14 use FrontControllerMockHelper;
22 protected $container;
23 15
24 /** @var TagCloudController */ 16 /** @var TagCloudController */
25 protected $controller; 17 protected $controller;
26 18
27 public function setUp(): void 19 public function setUp(): void
28 { 20 {
29 $this->container = $this->createMock(ShaarliContainer::class); 21 $this->createContainer();
22
30 $this->controller = new TagCloudController($this->container); 23 $this->controller = new TagCloudController($this->container);
31 } 24 }
32 25
@@ -385,53 +378,4 @@ class TagCloudControllerTest extends TestCase
385 static::assertSame('', $assignedVariables['search_tags']); 378 static::assertSame('', $assignedVariables['search_tags']);
386 static::assertCount(0, $assignedVariables['tags']); 379 static::assertCount(0, $assignedVariables['tags']);
387 } 380 }
388
389
390 protected function createValidContainerMockSet(): void
391 {
392 $loginManager = $this->createMock(LoginManager::class);
393 $this->container->loginManager = $loginManager;
394
395 $sessionManager = $this->createMock(SessionManager::class);
396 $this->container->sessionManager = $sessionManager;
397
398 // Config
399 $conf = $this->createMock(ConfigManager::class);
400 $this->container->conf = $conf;
401
402 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
403 return $default;
404 });
405
406 // PageBuilder
407 $pageBuilder = $this->createMock(PageBuilder::class);
408 $pageBuilder
409 ->method('render')
410 ->willReturnCallback(function (string $template): string {
411 return $template;
412 })
413 ;
414 $this->container->pageBuilder = $pageBuilder;
415
416 // Plugin Manager
417 $pluginManager = $this->createMock(PluginManager::class);
418 $this->container->pluginManager = $pluginManager;
419
420 // BookmarkService
421 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
422 $this->container->bookmarkService = $bookmarkService;
423 }
424
425 protected function assignTemplateVars(array &$variables): void
426 {
427 $this->container->pageBuilder
428 ->expects(static::atLeastOnce())
429 ->method('assign')
430 ->willReturnCallback(function ($key, $value) use (&$variables) {
431 $variables[$key] = $value;
432
433 return $this;
434 })
435 ;
436 }
437} 381}
diff --git a/tests/front/controller/TagControllerTest.php b/tests/front/controller/TagControllerTest.php
index bbac5652..5eea537b 100644
--- a/tests/front/controller/TagControllerTest.php
+++ b/tests/front/controller/TagControllerTest.php
@@ -5,32 +5,27 @@ 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;
10use Shaarli\Container\ShaarliContainer;
11use Shaarli\Plugin\PluginManager;
12use Shaarli\Render\PageBuilder;
13use Shaarli\Security\LoginManager;
14use Slim\Http\Request; 8use Slim\Http\Request;
15use Slim\Http\Response; 9use Slim\Http\Response;
16 10
17class TagControllerTest extends TestCase 11class TagControllerTest extends TestCase
18{ 12{
19 /** @var ShaarliContainer */ 13 use FrontControllerMockHelper;
20 protected $container;
21 14
22 /** @var TagController */ 15 /** @var TagController */
23 protected $controller; 16 protected $controller;
24 17
25 public function setUp(): void 18 public function setUp(): void
26 { 19 {
27 $this->container = $this->createMock(ShaarliContainer::class); 20 $this->createContainer();
21
28 $this->controller = new TagController($this->container); 22 $this->controller = new TagController($this->container);
29 } 23 }
30 24
31 public function testAddTagWithReferer(): void 25 public function testAddTagWithReferer(): void
32 { 26 {
33 $this->createValidContainerMockSet(); 27 $this->createValidContainerMockSet();
28
34 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/']; 29 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/'];
35 30
36 $request = $this->createMock(Request::class); 31 $request = $this->createMock(Request::class);
@@ -48,6 +43,7 @@ class TagControllerTest extends TestCase
48 public function testAddTagWithRefererAndExistingSearch(): void 43 public function testAddTagWithRefererAndExistingSearch(): void
49 { 44 {
50 $this->createValidContainerMockSet(); 45 $this->createValidContainerMockSet();
46
51 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; 47 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
52 48
53 $request = $this->createMock(Request::class); 49 $request = $this->createMock(Request::class);
@@ -81,6 +77,7 @@ class TagControllerTest extends TestCase
81 public function testAddTagRemoveLegacyQueryParam(): void 77 public function testAddTagRemoveLegacyQueryParam(): void
82 { 78 {
83 $this->createValidContainerMockSet(); 79 $this->createValidContainerMockSet();
80
84 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc']; 81 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
85 82
86 $request = $this->createMock(Request::class); 83 $request = $this->createMock(Request::class);
@@ -98,6 +95,7 @@ class TagControllerTest extends TestCase
98 public function testAddTagResetPagination(): void 95 public function testAddTagResetPagination(): void
99 { 96 {
100 $this->createValidContainerMockSet(); 97 $this->createValidContainerMockSet();
98
101 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12']; 99 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
102 100
103 $request = $this->createMock(Request::class); 101 $request = $this->createMock(Request::class);
@@ -115,6 +113,7 @@ class TagControllerTest extends TestCase
115 public function testAddTagWithRefererAndEmptySearch(): void 113 public function testAddTagWithRefererAndEmptySearch(): void
116 { 114 {
117 $this->createValidContainerMockSet(); 115 $this->createValidContainerMockSet();
116
118 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=']; 117 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
119 118
120 $request = $this->createMock(Request::class); 119 $request = $this->createMock(Request::class);
@@ -132,6 +131,7 @@ class TagControllerTest extends TestCase
132 public function testAddTagWithoutNewTagWithReferer(): void 131 public function testAddTagWithoutNewTagWithReferer(): void
133 { 132 {
134 $this->createValidContainerMockSet(); 133 $this->createValidContainerMockSet();
134
135 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; 135 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
136 136
137 $request = $this->createMock(Request::class); 137 $request = $this->createMock(Request::class);
@@ -157,35 +157,4 @@ class TagControllerTest extends TestCase
157 static::assertSame(302, $result->getStatusCode()); 157 static::assertSame(302, $result->getStatusCode());
158 static::assertSame(['./'], $result->getHeader('location')); 158 static::assertSame(['./'], $result->getHeader('location'));
159 } 159 }
160
161 protected function createValidContainerMockSet(): void
162 {
163 // User logged out
164 $loginManager = $this->createMock(LoginManager::class);
165 $loginManager->method('isLoggedIn')->willReturn(false);
166 $loginManager->method('canLogin')->willReturn(true);
167 $this->container->loginManager = $loginManager;
168
169 // Config
170 $conf = $this->createMock(ConfigManager::class);
171 $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
172 return $default;
173 });
174 $this->container->conf = $conf;
175
176 // PageBuilder
177 $pageBuilder = $this->createMock(PageBuilder::class);
178 $pageBuilder
179 ->method('render')
180 ->willReturnCallback(function (string $template): string {
181 return $template;
182 })
183 ;
184 $this->container->pageBuilder = $pageBuilder;
185
186 $pluginManager = $this->createMock(PluginManager::class);
187 $this->container->pluginManager = $pluginManager;
188 $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
189 $this->container->bookmarkService = $bookmarkService;
190 }
191} 160}