]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/front/controller/TagControllerTest.php
Refactor front controller tests to create container mock using a trait
[github/shaarli/Shaarli.git] / tests / front / controller / TagControllerTest.php
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;
-    }
 }