3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Bookmark\BookmarkServiceInterface
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Container\ShaarliTestContainer
;
10 use Shaarli\Formatter\BookmarkFormatter
;
11 use Shaarli\Formatter\BookmarkRawFormatter
;
12 use Shaarli\Formatter\FormatterFactory
;
13 use Shaarli\Plugin\PluginManager
;
14 use Shaarli\Render\PageBuilder
;
15 use Shaarli\Render\PageCacheManager
;
16 use Shaarli\Security\LoginManager
;
17 use Shaarli\Security\SessionManager
;
20 * Trait FrontControllerMockHelper
22 * Helper trait used to initialize the ShaarliContainer and mock its services for controller tests.
24 * @property ShaarliTestContainer $container
25 * @package Shaarli\Front\Controller
27 trait FrontControllerMockHelper
29 /** @var ShaarliTestContainer */
33 * Mock the container instance and initialize container's services used by tests
35 protected function createContainer(): void
37 $this->container
= $this->createMock(ShaarliTestContainer
::class);
39 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
42 $this->container
->conf
= $this->createMock(ConfigManager
::class);
43 $this->container
->conf
->method('get')->willReturnCallback(function (string $parameter, $default) {
44 if ($parameter === 'general.tags_separator') {
48 return $default === null ? $parameter : $default;
52 $this->container
->pageBuilder
= $this->createMock(PageBuilder
::class);
53 $this->container
->pageBuilder
55 ->willReturnCallback(function (string $template): string {
61 $this->container
->pluginManager
= $this->createMock(PluginManager
::class);
64 $this->container
->bookmarkService
= $this->createMock(BookmarkServiceInterface
::class);
67 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
68 $this->container
->formatterFactory
69 ->method('getFormatter')
70 ->willReturnCallback(function (): BookmarkFormatter
{
71 return new BookmarkRawFormatter($this->container
->conf
, true);
76 $this->container
->pageCacheManager
= $this->createMock(PageCacheManager
::class);
79 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
82 $this->container
->environment
= [
83 'SERVER_NAME' => 'shaarli',
84 'SERVER_PORT' => '80',
85 'REQUEST_URI' => '/subfolder/daily-rss',
86 'REMOTE_ADDR' => '1.2.3.4',
87 'SCRIPT_NAME' => '/subfolder/index.php',
90 $this->container
->basePath
= '/subfolder';
94 * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
96 * @param mixed $variables Array reference to populate.
98 protected function assignTemplateVars(array &$variables): void
100 $this->container
->pageBuilder
102 ->willReturnCallback(function ($key, $value) use (&$variables) {
103 $variables[$key] = $value;
110 protected static function generateString(int $length): string
112 // bin2hex(random_bytes) generates string twice as long as given parameter
113 $length = (int) ceil($length / 2);
115 return bin2hex(random_bytes($length));
119 * Force to be used in PHPUnit context.
121 protected abstract function isInTestsContext(): bool;