From dd09ec52b20b4a548ecf5c847627575e506e3a50 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 20 May 2020 12:43:40 +0200 Subject: [PATCH] Refactor front controller tests to create container mock using a trait --- tests/bootstrap.php | 2 + tests/container/ShaarliTestContainer.php | 38 ++++++ .../front/controller/DailyControllerTest.php | 84 +------------ tests/front/controller/FeedControllerTest.php | 78 +----------- .../controller/FrontControllerMockHelper.php | 114 ++++++++++++++++++ .../front/controller/LoginControllerTest.php | 58 ++------- .../front/controller/LogoutControllerTest.php | 19 ++- .../controller/OpenSearchControllerTest.php | 60 ++------- .../controller/PictureWallControllerTest.php | 65 +--------- .../controller/ShaarliControllerTest.php | 68 ++++------- .../controller/TagCloudControllerTest.php | 62 +--------- tests/front/controller/TagControllerTest.php | 49 ++------ 12 files changed, 232 insertions(+), 465 deletions(-) create mode 100644 tests/container/ShaarliTestContainer.php create mode 100644 tests/front/controller/FrontControllerMockHelper.php 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 @@ function is_iterable($var) require_once 'tests/utils/ReferenceLinkDB.php'; require_once 'tests/utils/ReferenceHistory.php'; require_once 'tests/utils/FakeBookmarkService.php'; +require_once 'tests/container/ShaarliTestContainer.php'; +require_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 @@ +container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + $this->controller = new DailyController($this->container); DailyController::$DAILY_RSS_NB_DAYS = 2; } @@ -105,7 +95,8 @@ public function testValidIndexControllerInvokeDefault(): void static::assertArrayHasKey('loggedin', $param); return $data; - }); + }) + ; $result = $this->controller->index($request, $response); @@ -497,71 +488,6 @@ public function testValidRssControllerInvokeNoBookmark(): void static::assertCount(0, $assignedVariables['days']); } - protected function createValidContainerMockSet(): void - { - $loginManager = $this->createMock(LoginManager::class); - $this->container->loginManager = $loginManager; - - // Config - $conf = $this->createMock(ConfigManager::class); - $this->container->conf = $conf; - $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { - return $default; - }); - - // PageBuilder - $pageBuilder = $this->createMock(PageBuilder::class); - $pageBuilder - ->method('render') - ->willReturnCallback(function (string $template): string { - return $template; - }) - ; - $this->container->pageBuilder = $pageBuilder; - - // Plugin Manager - $pluginManager = $this->createMock(PluginManager::class); - $this->container->pluginManager = $pluginManager; - - // BookmarkService - $bookmarkService = $this->createMock(BookmarkServiceInterface::class); - $this->container->bookmarkService = $bookmarkService; - - // Formatter - $formatterFactory = $this->createMock(FormatterFactory::class); - $formatterFactory - ->method('getFormatter') - ->willReturnCallback(function (): BookmarkFormatter { - return new BookmarkRawFormatter($this->container->conf, true); - }) - ; - $this->container->formatterFactory = $formatterFactory; - - // CacheManager - $pageCacheManager = $this->createMock(PageCacheManager::class); - $this->container->pageCacheManager = $pageCacheManager; - - // $_SERVER - $this->container->environment = [ - 'SERVER_NAME' => 'shaarli', - 'SERVER_PORT' => '80', - 'REQUEST_URI' => '/daily-rss', - ]; - } - - protected function assignTemplateVars(array &$variables): void - { - $this->container->pageBuilder - ->expects(static::atLeastOnce()) - ->method('assign') - ->willReturnCallback(function ($key, $value) use (&$variables) { - $variables[$key] = $value; - - return $this; - }) - ; - } - protected static function generateContent(int $length): string { // 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 @@ namespace Shaarli\Front\Controller; use PHPUnit\Framework\TestCase; -use Shaarli\Bookmark\BookmarkServiceInterface; -use Shaarli\Config\ConfigManager; -use Shaarli\Container\ShaarliContainer; use Shaarli\Feed\FeedBuilder; -use Shaarli\Formatter\FormatterFactory; -use Shaarli\Plugin\PluginManager; -use Shaarli\Render\PageBuilder; -use Shaarli\Render\PageCacheManager; -use Shaarli\Security\LoginManager; use Slim\Http\Request; use Slim\Http\Response; class FeedControllerTest extends TestCase { - /** @var ShaarliContainer */ - protected $container; + use FrontControllerMockHelper; /** @var FeedController */ protected $controller; public function setUp(): void { - $this->container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + + $this->container->feedBuilder = $this->createMock(FeedBuilder::class); + $this->controller = new FeedController($this->container); } @@ -154,66 +148,4 @@ public function testAtomControllerWithParameters(): void static::assertSame('feed.atom', (string) $result->getBody()); static::assertSame('data', $assignedVariables['content']); } - - protected function createValidContainerMockSet(): void - { - $loginManager = $this->createMock(LoginManager::class); - $this->container->loginManager = $loginManager; - - // Config - $conf = $this->createMock(ConfigManager::class); - $this->container->conf = $conf; - $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { - return $default; - }); - - // PageBuilder - $pageBuilder = $this->createMock(PageBuilder::class); - $pageBuilder - ->method('render') - ->willReturnCallback(function (string $template): string { - return $template; - }) - ; - $this->container->pageBuilder = $pageBuilder; - - $bookmarkService = $this->createMock(BookmarkServiceInterface::class); - $this->container->bookmarkService = $bookmarkService; - - // Plugin Manager - $pluginManager = $this->createMock(PluginManager::class); - $this->container->pluginManager = $pluginManager; - - // Formatter - $formatterFactory = $this->createMock(FormatterFactory::class); - $this->container->formatterFactory = $formatterFactory; - - // CacheManager - $pageCacheManager = $this->createMock(PageCacheManager::class); - $this->container->pageCacheManager = $pageCacheManager; - - // FeedBuilder - $feedBuilder = $this->createMock(FeedBuilder::class); - $this->container->feedBuilder = $feedBuilder; - - // $_SERVER - $this->container->environment = [ - 'SERVER_NAME' => 'shaarli', - 'SERVER_PORT' => '80', - 'REQUEST_URI' => '/daily-rss', - ]; - } - - protected function assignTemplateVars(array &$variables): void - { - $this->container->pageBuilder - ->expects(static::atLeastOnce()) - ->method('assign') - ->willReturnCallback(function ($key, $value) use (&$variables) { - $variables[$key] = $value; - - return $this; - }) - ; - } } 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 @@ +container = $this->createMock(ShaarliTestContainer::class); + } + + /** + * Initialize container's services used by tests + */ + protected function createValidContainerMockSet(): void + { + $this->container->loginManager = $this->createMock(LoginManager::class); + + // Config + $this->container->conf = $this->createMock(ConfigManager::class); + $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { + return $default; + }); + + // PageBuilder + $this->container->pageBuilder = $this->createMock(PageBuilder::class); + $this->container->pageBuilder + ->method('render') + ->willReturnCallback(function (string $template): string { + return $template; + }) + ; + + // Plugin Manager + $this->container->pluginManager = $this->createMock(PluginManager::class); + + // BookmarkService + $this->container->bookmarkService = $this->createMock(BookmarkServiceInterface::class); + + // Formatter + $this->container->formatterFactory = $this->createMock(FormatterFactory::class); + $this->container->formatterFactory + ->method('getFormatter') + ->willReturnCallback(function (): BookmarkFormatter { + return new BookmarkRawFormatter($this->container->conf, true); + }) + ; + + // CacheManager + $this->container->pageCacheManager = $this->createMock(PageCacheManager::class); + + // SessionManager + $this->container->sessionManager = $this->createMock(SessionManager::class); + + // $_SERVER + $this->container->environment = [ + 'SERVER_NAME' => 'shaarli', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => '/daily-rss', + ]; + } + + /** + * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution. + * + * @param mixed $variables Array reference to populate. + */ + protected function assignTemplateVars(array &$variables): void + { + $this->container->pageBuilder + ->expects(static::atLeastOnce()) + ->method('assign') + ->willReturnCallback(function ($key, $value) use (&$variables) { + $variables[$key] = $value; + + return $this; + }) + ; + } + + /** + * Force to be used in PHPUnit context. + */ + protected abstract function createMock($originalClassName): MockObject; +} 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 @@ 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 @@ public function testValidControllerInvoke(): void }) ; + $this->container->loginManager->method('canLogin')->willReturn(true); + $result = $this->controller->index($request, $response); static::assertInstanceOf(Response::class, $result); @@ -77,6 +74,8 @@ public function testValidControllerInvokeWithUserName(): void }) ; + $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 @@ public function testValidControllerInvokeWithUserName(): void 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 @@ public function testLoginControllerWhileBanned(): void $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; - } } 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 @@ function setcookie(string $name, string $value): void { } use PHPUnit\Framework\TestCase; -use Shaarli\Container\ShaarliContainer; -use Shaarli\Render\PageCacheManager; use Shaarli\Security\LoginManager; use Shaarli\Security\SessionManager; use Slim\Http\Request; @@ -21,15 +19,15 @@ function setcookie(string $name, string $value): void { class LogoutControllerTest extends TestCase { - /** @var ShaarliContainer */ - protected $container; + use FrontControllerMockHelper; /** @var LogoutController */ protected $controller; public function setUp(): void { - $this->container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + $this->controller = new LogoutController($this->container); setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there'); @@ -37,16 +35,15 @@ public function setUp(): void public function testValidControllerInvoke(): void { + $this->createValidContainerMockSet(); + $request = $this->createMock(Request::class); $response = new Response(); - $pageCacheManager = $this->createMock(PageCacheManager::class); - $pageCacheManager->expects(static::once())->method('invalidateCaches'); - $this->container->pageCacheManager = $pageCacheManager; + $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches'); - $sessionManager = $this->createMock(SessionManager::class); - $sessionManager->expects(static::once())->method('logout'); - $this->container->sessionManager = $sessionManager; + $this->container->sessionManager = $this->createMock(SessionManager::class); + $this->container->sessionManager->expects(static::once())->method('logout'); static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]); 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 @@ namespace front\controller; use PHPUnit\Framework\TestCase; -use Shaarli\Bookmark\BookmarkServiceInterface; -use Shaarli\Container\ShaarliContainer; +use Shaarli\Front\Controller\FrontControllerMockHelper; use Shaarli\Front\Controller\OpenSearchController; -use Shaarli\Plugin\PluginManager; -use Shaarli\Render\PageBuilder; -use Shaarli\Security\LoginManager; use Slim\Http\Request; use Slim\Http\Response; class OpenSearchControllerTest extends TestCase { - /** @var ShaarliContainer */ - protected $container; + use FrontControllerMockHelper; /** @var OpenSearchController */ protected $controller; public function setUp(): void { - $this->container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + $this->controller = new OpenSearchController($this->container); } @@ -42,51 +38,11 @@ public function testOpenSearchController(): void $result = $this->controller->index($request, $response); static::assertSame(200, $result->getStatusCode()); - static::assertStringContainsString('application/xml', $result->getHeader('Content-Type')[0]); + static::assertStringContainsString( + 'application/opensearchdescription+xml', + $result->getHeader('Content-Type')[0] + ); static::assertSame('opensearch', (string) $result->getBody()); static::assertSame('http://shaarli', $assignedVariables['serverurl']); } - - protected function createValidContainerMockSet(): void - { - $loginManager = $this->createMock(LoginManager::class); - $this->container->loginManager = $loginManager; - - // PageBuilder - $pageBuilder = $this->createMock(PageBuilder::class); - $pageBuilder - ->method('render') - ->willReturnCallback(function (string $template): string { - return $template; - }) - ; - $this->container->pageBuilder = $pageBuilder; - - $bookmarkService = $this->createMock(BookmarkServiceInterface::class); - $this->container->bookmarkService = $bookmarkService; - - // Plugin Manager - $pluginManager = $this->createMock(PluginManager::class); - $this->container->pluginManager = $pluginManager; - - // $_SERVER - $this->container->environment = [ - 'SERVER_NAME' => 'shaarli', - 'SERVER_PORT' => '80', - 'REQUEST_URI' => '/open-search', - ]; - } - - protected function assignTemplateVars(array &$variables): void - { - $this->container->pageBuilder - ->expects(static::atLeastOnce()) - ->method('assign') - ->willReturnCallback(function ($key, $value) use (&$variables) { - $variables[$key] = $value; - - return $this; - }) - ; - } } 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 @@ use PHPUnit\Framework\TestCase; use Shaarli\Bookmark\Bookmark; -use Shaarli\Bookmark\BookmarkServiceInterface; use Shaarli\Config\ConfigManager; -use Shaarli\Container\ShaarliContainer; -use Shaarli\Formatter\BookmarkFormatter; -use Shaarli\Formatter\BookmarkRawFormatter; -use Shaarli\Formatter\FormatterFactory; use Shaarli\Front\Exception\ThumbnailsDisabledException; -use Shaarli\Plugin\PluginManager; -use Shaarli\Render\PageBuilder; -use Shaarli\Security\LoginManager; use Shaarli\Thumbnailer; use Slim\Http\Request; use Slim\Http\Response; class PictureWallControllerTest extends TestCase { - /** @var ShaarliContainer */ - protected $container; + use FrontControllerMockHelper; /** @var PictureWallController */ protected $controller; public function setUp(): void { - $this->container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + $this->controller = new PictureWallController($this->container); } @@ -43,6 +35,7 @@ public function testValidControllerInvokeDefault(): void $response = new Response(); // ConfigManager: thumbnails are enabled + $this->container->conf = $this->createMock(ConfigManager::class); $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { if ($parameter === 'thumbnails.mode') { return Thumbnailer::MODE_COMMON; @@ -53,15 +46,7 @@ public function testValidControllerInvokeDefault(): void // Save RainTPL assigned variables $assignedVariables = []; - $this->container->pageBuilder - ->expects(static::atLeastOnce()) - ->method('assign') - ->willReturnCallback(function ($key, $value) use (&$assignedVariables) { - $assignedVariables[$key] = $value; - - return $this; - }) - ; + $this->assignTemplateVars($assignedVariables); // Links dataset: 2 links with thumbnails $this->container->bookmarkService @@ -137,44 +122,4 @@ public function testControllerWithThumbnailsDisabled(): void $this->controller->index($request, $response); } - - protected function createValidContainerMockSet(): void - { - $loginManager = $this->createMock(LoginManager::class); - $this->container->loginManager = $loginManager; - - // Config - $conf = $this->createMock(ConfigManager::class); - $this->container->conf = $conf; - - // PageBuilder - $pageBuilder = $this->createMock(PageBuilder::class); - $pageBuilder - ->method('render') - ->willReturnCallback(function (string $template): string { - return $template; - }) - ; - $this->container->pageBuilder = $pageBuilder; - - // Plugin Manager - $pluginManager = $this->createMock(PluginManager::class); - $this->container->pluginManager = $pluginManager; - - // BookmarkService - $bookmarkService = $this->createMock(BookmarkServiceInterface::class); - $this->container->bookmarkService = $bookmarkService; - - // Formatter - $formatterFactory = $this->createMock(FormatterFactory::class); - $formatterFactory - ->method('getFormatter') - ->willReturnCallback(function (string $type): BookmarkFormatter { - if ($type === 'raw') { - return new BookmarkRawFormatter($this->container->conf, true); - } - }) - ; - $this->container->formatterFactory = $formatterFactory; - } } 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 @@ use PHPUnit\Framework\TestCase; use Shaarli\Bookmark\BookmarkFilter; -use Shaarli\Bookmark\BookmarkServiceInterface; -use Shaarli\Container\ShaarliContainer; -use Shaarli\Plugin\PluginManager; -use Shaarli\Render\PageBuilder; -use Shaarli\Security\LoginManager; /** * Class ShaarliControllerTest @@ -20,8 +15,7 @@ */ class ShaarliControllerTest extends TestCase { - /** @var ShaarliContainer */ - protected $container; + use FrontControllerMockHelper; /** @var LoginController */ protected $controller; @@ -31,7 +25,8 @@ class ShaarliControllerTest extends TestCase public function setUp(): void { - $this->container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + $this->controller = new class($this->container) extends ShaarliController { public function assignView(string $key, $value): ShaarliController @@ -51,6 +46,8 @@ public function testAssignView(): void { $this->createValidContainerMockSet(); + $this->assignTemplateVars($this->assignedValues); + $self = $this->controller->assignView('variableName', 'variableValue'); static::assertInstanceOf(ShaarliController::class, $self); @@ -61,6 +58,24 @@ public function testRender(): void { $this->createValidContainerMockSet(); + $this->assignTemplateVars($this->assignedValues); + + $this->container->bookmarkService + ->method('count') + ->willReturnCallback(function (string $visibility): int { + return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10; + }) + ; + + $this->container->pluginManager + ->method('executeHooks') + ->willReturnCallback(function (string $hook, array &$data, array $params): array { + return $data[$hook] = $params; + }); + $this->container->pluginManager->method('getErrors')->willReturn(['error']); + + $this->container->loginManager->method('isLoggedIn')->willReturn(true); + $render = $this->controller->render('templateName'); static::assertSame('templateName', $render); @@ -76,41 +91,4 @@ public function testRender(): void static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']); static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']); } - - protected function createValidContainerMockSet(): void - { - $pageBuilder = $this->createMock(PageBuilder::class); - $pageBuilder - ->method('assign') - ->willReturnCallback(function (string $key, $value): void { - $this->assignedValues[$key] = $value; - }); - $pageBuilder - ->method('render') - ->willReturnCallback(function (string $template): string { - return $template; - }); - $this->container->pageBuilder = $pageBuilder; - - $bookmarkService = $this->createMock(BookmarkServiceInterface::class); - $bookmarkService - ->method('count') - ->willReturnCallback(function (string $visibility): int { - return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10; - }); - $this->container->bookmarkService = $bookmarkService; - - $pluginManager = $this->createMock(PluginManager::class); - $pluginManager - ->method('executeHooks') - ->willReturnCallback(function (string $hook, array &$data, array $params): array { - return $data[$hook] = $params; - }); - $pluginManager->method('getErrors')->willReturn(['error']); - $this->container->pluginManager = $pluginManager; - - $loginManager = $this->createMock(LoginManager::class); - $loginManager->method('isLoggedIn')->willReturn(true); - $this->container->loginManager = $loginManager; - } } 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 @@ use PHPUnit\Framework\TestCase; use Shaarli\Bookmark\BookmarkFilter; -use Shaarli\Bookmark\BookmarkServiceInterface; -use Shaarli\Config\ConfigManager; -use Shaarli\Container\ShaarliContainer; -use Shaarli\Plugin\PluginManager; -use Shaarli\Render\PageBuilder; -use Shaarli\Security\LoginManager; -use Shaarli\Security\SessionManager; use Slim\Http\Request; use Slim\Http\Response; class TagCloudControllerTest extends TestCase { - /** @var ShaarliContainer */ - protected $container; + use FrontControllerMockHelper; /** @var TagCloudController */ protected $controller; public function setUp(): void { - $this->container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + $this->controller = new TagCloudController($this->container); } @@ -385,53 +378,4 @@ public function testEmptyList(): void static::assertSame('', $assignedVariables['search_tags']); static::assertCount(0, $assignedVariables['tags']); } - - - protected function createValidContainerMockSet(): void - { - $loginManager = $this->createMock(LoginManager::class); - $this->container->loginManager = $loginManager; - - $sessionManager = $this->createMock(SessionManager::class); - $this->container->sessionManager = $sessionManager; - - // Config - $conf = $this->createMock(ConfigManager::class); - $this->container->conf = $conf; - - $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) { - return $default; - }); - - // PageBuilder - $pageBuilder = $this->createMock(PageBuilder::class); - $pageBuilder - ->method('render') - ->willReturnCallback(function (string $template): string { - return $template; - }) - ; - $this->container->pageBuilder = $pageBuilder; - - // Plugin Manager - $pluginManager = $this->createMock(PluginManager::class); - $this->container->pluginManager = $pluginManager; - - // BookmarkService - $bookmarkService = $this->createMock(BookmarkServiceInterface::class); - $this->container->bookmarkService = $bookmarkService; - } - - protected function assignTemplateVars(array &$variables): void - { - $this->container->pageBuilder - ->expects(static::atLeastOnce()) - ->method('assign') - ->willReturnCallback(function ($key, $value) use (&$variables) { - $variables[$key] = $value; - - return $this; - }) - ; - } } 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 @@ namespace Shaarli\Front\Controller; use PHPUnit\Framework\TestCase; -use Shaarli\Bookmark\BookmarkServiceInterface; -use Shaarli\Config\ConfigManager; -use Shaarli\Container\ShaarliContainer; -use Shaarli\Plugin\PluginManager; -use Shaarli\Render\PageBuilder; -use Shaarli\Security\LoginManager; use Slim\Http\Request; use Slim\Http\Response; class TagControllerTest extends TestCase { - /** @var ShaarliContainer */ - protected $container; + use FrontControllerMockHelper; /** @var TagController */ protected $controller; public function setUp(): void { - $this->container = $this->createMock(ShaarliContainer::class); + $this->createContainer(); + $this->controller = new TagController($this->container); } public function testAddTagWithReferer(): void { $this->createValidContainerMockSet(); + $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/']; $request = $this->createMock(Request::class); @@ -48,6 +43,7 @@ public function testAddTagWithReferer(): void public function testAddTagWithRefererAndExistingSearch(): void { $this->createValidContainerMockSet(); + $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; $request = $this->createMock(Request::class); @@ -81,6 +77,7 @@ public function testAddTagWithoutRefererAndExistingSearch(): void public function testAddTagRemoveLegacyQueryParam(): void { $this->createValidContainerMockSet(); + $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc']; $request = $this->createMock(Request::class); @@ -98,6 +95,7 @@ public function testAddTagRemoveLegacyQueryParam(): void public function testAddTagResetPagination(): void { $this->createValidContainerMockSet(); + $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12']; $request = $this->createMock(Request::class); @@ -115,6 +113,7 @@ public function testAddTagResetPagination(): void public function testAddTagWithRefererAndEmptySearch(): void { $this->createValidContainerMockSet(); + $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=']; $request = $this->createMock(Request::class); @@ -132,6 +131,7 @@ public function testAddTagWithRefererAndEmptySearch(): void public function testAddTagWithoutNewTagWithReferer(): void { $this->createValidContainerMockSet(); + $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; $request = $this->createMock(Request::class); @@ -157,35 +157,4 @@ public function testAddTagWithoutNewTagWithoutReferer(): void static::assertSame(302, $result->getStatusCode()); static::assertSame(['./'], $result->getHeader('location')); } - - 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; - } }