3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 use PHPUnit\Framework\MockObject\MockObject
;
8 use Shaarli\Bookmark\BookmarkServiceInterface
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Container\ShaarliTestContainer
;
11 use Shaarli\Formatter\BookmarkFormatter
;
12 use Shaarli\Formatter\BookmarkRawFormatter
;
13 use Shaarli\Formatter\FormatterFactory
;
14 use Shaarli\Plugin\PluginManager
;
15 use Shaarli\Render\PageBuilder
;
16 use Shaarli\Render\PageCacheManager
;
17 use Shaarli\Security\LoginManager
;
18 use Shaarli\Security\SessionManager
;
21 * Trait FrontControllerMockHelper
23 * Helper trait used to initialize the ShaarliContainer and mock its services for controller tests.
25 * @property ShaarliTestContainer $container
26 * @package Shaarli\Front\Controller
28 trait FrontControllerMockHelper
30 /** @var ShaarliTestContainer */
34 * Mock the container instance
36 protected function createContainer(): void
38 $this->container
= $this->createMock(ShaarliTestContainer
::class);
42 * Initialize container's services used by tests
44 protected function createValidContainerMockSet(): void
46 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
49 $this->container
->conf
= $this->createMock(ConfigManager
::class);
50 $this->container
->conf
->method('get')->willReturnCallback(function (string $parameter, $default) {
55 $this->container
->pageBuilder
= $this->createMock(PageBuilder
::class);
56 $this->container
->pageBuilder
58 ->willReturnCallback(function (string $template): string {
64 $this->container
->pluginManager
= $this->createMock(PluginManager
::class);
67 $this->container
->bookmarkService
= $this->createMock(BookmarkServiceInterface
::class);
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);
79 $this->container
->pageCacheManager
= $this->createMock(PageCacheManager
::class);
82 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
85 $this->container
->environment
= [
86 'SERVER_NAME' => 'shaarli',
87 'SERVER_PORT' => '80',
88 'REQUEST_URI' => '/daily-rss',
93 * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
95 * @param mixed $variables Array reference to populate.
97 protected function assignTemplateVars(array &$variables): void
99 $this->container
->pageBuilder
100 ->expects(static::atLeastOnce())
102 ->willReturnCallback(function ($key, $value) use (&$variables) {
103 $variables[$key] = $value;
111 * Force to be used in PHPUnit context.
113 protected abstract function createMock($originalClassName): MockObject
;