]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Refactor front controller tests to create container mock using a trait
authorArthurHoaro <arthur@hoa.ro>
Wed, 20 May 2020 10:43:40 +0000 (12:43 +0200)
committerArthurHoaro <arthur@hoa.ro>
Thu, 23 Jul 2020 19:19:21 +0000 (21:19 +0200)
12 files changed:
tests/bootstrap.php
tests/container/ShaarliTestContainer.php [new file with mode: 0644]
tests/front/controller/DailyControllerTest.php
tests/front/controller/FeedControllerTest.php
tests/front/controller/FrontControllerMockHelper.php [new file with mode: 0644]
tests/front/controller/LoginControllerTest.php
tests/front/controller/LogoutControllerTest.php
tests/front/controller/OpenSearchControllerTest.php
tests/front/controller/PictureWallControllerTest.php
tests/front/controller/ShaarliControllerTest.php
tests/front/controller/TagCloudControllerTest.php
tests/front/controller/TagControllerTest.php

index c80bcb33a3decdce2080479c65fa50fa6a3decaf..6bb345c20847b59f56f79aaf457a7e455c22d784 100644 (file)
@@ -21,3 +21,5 @@ require_once 'application/http/HttpUtils.php';
 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 (file)
index 0000000..53197ae
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Container;
+
+use PHPUnit\Framework\MockObject\MockObject;
+use Shaarli\Bookmark\BookmarkServiceInterface;
+use Shaarli\Config\ConfigManager;
+use Shaarli\Feed\FeedBuilder;
+use Shaarli\Formatter\FormatterFactory;
+use Shaarli\History;
+use Shaarli\Plugin\PluginManager;
+use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
+use Shaarli\Security\LoginManager;
+use Shaarli\Security\SessionManager;
+
+/**
+ * Test helper allowing auto-completion for MockObjects.
+ *
+ * @property mixed[]                             $environment     $_SERVER automatically injected by Slim
+ * @property MockObject|ConfigManager            $conf
+ * @property MockObject|SessionManager           $sessionManager
+ * @property MockObject|LoginManager             $loginManager
+ * @property MockObject|string                   $webPath
+ * @property MockObject|History                  $history
+ * @property MockObject|BookmarkServiceInterface $bookmarkService
+ * @property MockObject|PageBuilder              $pageBuilder
+ * @property MockObject|PluginManager            $pluginManager
+ * @property MockObject|FormatterFactory         $formatterFactory
+ * @property MockObject|PageCacheManager         $pageCacheManager
+ * @property MockObject|FeedBuilder              $feedBuilder
+ */
+class ShaarliTestContainer extends ShaarliContainer
+{
+
+}
index 88ec116bac509ec89ba29ff0915da5e3200051d1..7ec99030c8e838e15c7943f651b4572ae320bfb6 100644 (file)
@@ -6,31 +6,21 @@ namespace Shaarli\Front\Controller;
 
 use PHPUnit\Framework\TestCase;
 use Shaarli\Bookmark\Bookmark;
-use Shaarli\Bookmark\BookmarkServiceInterface;
-use Shaarli\Config\ConfigManager;
-use Shaarli\Container\ShaarliContainer;
 use Shaarli\Feed\CachedPage;
-use Shaarli\Formatter\BookmarkFormatter;
-use Shaarli\Formatter\BookmarkRawFormatter;
-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 DailyControllerTest extends TestCase
 {
-    /** @var ShaarliContainer */
-    protected $container;
+    use FrontControllerMockHelper;
 
     /** @var DailyController */
     protected $controller;
 
     public function setUp(): void
     {
-        $this->container = $this->createMock(ShaarliContainer::class);
+        $this->createContainer();
+
         $this->controller = new DailyController($this->container);
         DailyController::$DAILY_RSS_NB_DAYS = 2;
     }
@@ -105,7 +95,8 @@ class DailyControllerTest extends TestCase
                 static::assertArrayHasKey('loggedin', $param);
 
                 return $data;
-            });
+            })
+        ;
 
         $result = $this->controller->index($request, $response);
 
@@ -497,71 +488,6 @@ class DailyControllerTest extends TestCase
         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
index d4cc5536c19b94c064aa26977f115d458a100052..7e8657e2cce69a3e772fff4505ed8095f2128416 100644 (file)
@@ -5,29 +5,23 @@ 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\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 @@ class FeedControllerTest extends TestCase
         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 (file)
index 0000000..b65607e
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller;
+
+use PHPUnit\Framework\MockObject\MockObject;
+use Shaarli\Bookmark\BookmarkServiceInterface;
+use Shaarli\Config\ConfigManager;
+use Shaarli\Container\ShaarliTestContainer;
+use Shaarli\Formatter\BookmarkFormatter;
+use Shaarli\Formatter\BookmarkRawFormatter;
+use Shaarli\Formatter\FormatterFactory;
+use Shaarli\Plugin\PluginManager;
+use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
+use Shaarli\Security\LoginManager;
+use Shaarli\Security\SessionManager;
+
+/**
+ * Trait FrontControllerMockHelper
+ *
+ * Helper trait used to initialize the ShaarliContainer and mock its services for controller tests.
+ *
+ * @property ShaarliTestContainer $container
+ * @package Shaarli\Front\Controller
+ */
+trait FrontControllerMockHelper
+{
+    /** @var ShaarliTestContainer */
+    protected $container;
+
+    /**
+     * Mock the container instance
+     */
+    protected function createContainer(): void
+    {
+        $this->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;
+}
index 8cf8ece7982397d0e79f0dbeee6d0d3d8b3e82d1..21937f3c6605254eeec9c813547fe87b74b0601f 100644 (file)
@@ -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;
-    }
 }
index d9ca1c252f9beee11c81e0f80fcf02ae1ba643bc..8e01c367aff8e744df0ec757a8c99e46bb501c60 100644 (file)
@@ -12,8 +12,6 @@ if (!function_exists('Shaarli\Front\Controller\setcookie')) {
 }
 
 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 @@ use Slim\Http\Response;
 
 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 @@ class LogoutControllerTest extends TestCase
 
     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]);
 
index 7ba0f7dfa69679700d42addd2be1affca7c15908..f3b6f4396b59154faa635ec63079af8d6bb37154 100644 (file)
@@ -5,26 +5,22 @@ declare(strict_types=1);
 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 @@ class OpenSearchControllerTest extends TestCase
         $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;
-            })
-        ;
-    }
 }
index 63802abdde978253e184ddfe56e082d15fe409c2..8160bb38e12a32cbb7cb7f4410240ba86a1be842 100644 (file)
@@ -6,31 +6,23 @@ namespace Shaarli\Front\Controller;
 
 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 @@ class PictureWallControllerTest extends TestCase
         $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 @@ class PictureWallControllerTest extends TestCase
 
         // 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 @@ class PictureWallControllerTest extends TestCase
 
         $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;
-    }
 }
index 6fa3feb93568f0112d046992506c4ae6ea5df94b..3efe4d95daa19d0930a9c62b9b2def88a9ae3ec8 100644 (file)
@@ -6,11 +6,6 @@ namespace Shaarli\Front\Controller;
 
 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 @@ use Shaarli\Security\LoginManager;
  */
 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 @@ class ShaarliControllerTest extends TestCase
     {
         $this->createValidContainerMockSet();
 
+        $this->assignTemplateVars($this->assignedValues);
+
         $self = $this->controller->assignView('variableName', 'variableValue');
 
         static::assertInstanceOf(ShaarliController::class, $self);
@@ -61,6 +58,24 @@ class ShaarliControllerTest extends TestCase
     {
         $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 @@ class ShaarliControllerTest extends TestCase
         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;
-    }
 }
index 719610d7e83027643a34a2ba7dcb9e4430ff5af0..8c27900d5d0d4384acbcb2e096c6ce976f17f58d 100644 (file)
@@ -6,27 +6,20 @@ namespace Shaarli\Front\Controller;
 
 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 @@ class TagCloudControllerTest extends TestCase
         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;
-            })
-        ;
-    }
 }
index bbac56520aec6c418c46c4d69de5562654d6bb38..5eea537be0d50f8ebbc6606e4e54aed724b8e62c 100644 (file)
@@ -5,32 +5,27 @@ 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\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 @@ class TagControllerTest extends TestCase
     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 @@ class TagControllerTest extends TestCase
     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 @@ class TagControllerTest extends TestCase
     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 @@ class TagControllerTest extends TestCase
     public function testAddTagWithRefererAndEmptySearch(): void
     {
         $this->createValidContainerMockSet();
+
         $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
 
         $request = $this->createMock(Request::class);
@@ -132,6 +131,7 @@ class TagControllerTest extends TestCase
     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 @@ class TagControllerTest extends TestCase
         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;
-    }
 }