3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\BookmarkFilter
;
9 use Shaarli\Bookmark\BookmarkServiceInterface
;
10 use Shaarli\Container\ShaarliContainer
;
11 use Shaarli\Plugin\PluginManager
;
12 use Shaarli\Render\PageBuilder
;
13 use Shaarli\Security\LoginManager
;
16 * Class ShaarliControllerTest
18 * This class is used to test default behavior of ShaarliController abstract class.
19 * It uses a dummy non abstract controller.
21 class ShaarliControllerTest
extends TestCase
23 /** @var ShaarliContainer */
26 /** @var LoginController */
27 protected $controller;
29 /** @var mixed[] List of variable assigned to the template */
30 protected $assignedValues;
32 public function setUp(): void
34 $this->container
= $this->createMock(ShaarliContainer
::class);
35 $this->controller
= new class($this->container
) extends ShaarliController
37 public function assignView(string $key, $value): ShaarliController
39 return parent
::assignView($key, $value);
42 public function render(string $template): string
44 return parent
::render($template);
47 $this->assignedValues
= [];
50 public function testAssignView(): void
52 $this->createValidContainerMockSet();
54 $self = $this->controller
->assignView('variableName', 'variableValue');
56 static::assertInstanceOf(ShaarliController
::class, $self);
57 static::assertSame('variableValue', $this->assignedValues
['variableName']);
60 public function testRender(): void
62 $this->createValidContainerMockSet();
64 $render = $this->controller
->render('templateName');
66 static::assertSame('templateName', $render);
68 static::assertSame(10, $this->assignedValues
['linkcount']);
69 static::assertSame(5, $this->assignedValues
['privateLinkcount']);
70 static::assertSame(['error'], $this->assignedValues
['plugin_errors']);
72 static::assertSame('templateName', $this->assignedValues
['plugins_includes']['render_includes']['target']);
73 static::assertTrue($this->assignedValues
['plugins_includes']['render_includes']['loggedin']);
74 static::assertSame('templateName', $this->assignedValues
['plugins_header']['render_header']['target']);
75 static::assertTrue($this->assignedValues
['plugins_header']['render_header']['loggedin']);
76 static::assertSame('templateName', $this->assignedValues
['plugins_footer']['render_footer']['target']);
77 static::assertTrue($this->assignedValues
['plugins_footer']['render_footer']['loggedin']);
80 protected function createValidContainerMockSet(): void
82 $pageBuilder = $this->createMock(PageBuilder
::class);
85 ->willReturnCallback(function (string $key, $value): void {
86 $this->assignedValues
[$key] = $value;
90 ->willReturnCallback(function (string $template): string {
93 $this->container
->pageBuilder
= $pageBuilder;
95 $bookmarkService = $this->createMock(BookmarkServiceInterface
::class);
98 ->willReturnCallback(function (string $visibility): int {
99 return $visibility === BookmarkFilter
::$PRIVATE ? 5 : 10;
101 $this->container
->bookmarkService
= $bookmarkService;
103 $pluginManager = $this->createMock(PluginManager
::class);
105 ->method('executeHooks')
106 ->willReturnCallback(function (string $hook, array &$data, array $params): array {
107 return $data[$hook] = $params;
109 $pluginManager->method('getErrors')->willReturn(['error']);
110 $this->container
->pluginManager
= $pluginManager;
112 $loginManager = $this->createMock(LoginManager
::class);
113 $loginManager->method('isLoggedIn')->willReturn(true);
114 $this->container
->loginManager
= $loginManager;