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 return $default === null ? $parameter : $default;
48 $this->container
->pageBuilder
= $this->createMock(PageBuilder
::class);
49 $this->container
->pageBuilder
51 ->willReturnCallback(function (string $template): string {
57 $this->container
->pluginManager
= $this->createMock(PluginManager
::class);
60 $this->container
->bookmarkService
= $this->createMock(BookmarkServiceInterface
::class);
63 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
64 $this->container
->formatterFactory
65 ->method('getFormatter')
66 ->willReturnCallback(function (): BookmarkFormatter
{
67 return new BookmarkRawFormatter($this->container
->conf
, true);
72 $this->container
->pageCacheManager
= $this->createMock(PageCacheManager
::class);
75 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
78 $this->container
->environment
= [
79 'SERVER_NAME' => 'shaarli',
80 'SERVER_PORT' => '80',
81 'REQUEST_URI' => '/subfolder/daily-rss',
82 'REMOTE_ADDR' => '1.2.3.4',
83 'SCRIPT_NAME' => '/subfolder/index.php',
86 $this->container
->basePath
= '/subfolder';
90 * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
92 * @param mixed $variables Array reference to populate.
94 protected function assignTemplateVars(array &$variables): void
96 $this->container
->pageBuilder
98 ->willReturnCallback(function ($key, $value) use (&$variables) {
99 $variables[$key] = $value;
106 protected static function generateString(int $length): string
108 // bin2hex(random_bytes) generates string twice as long as given parameter
109 $length = (int) ceil($length / 2);
111 return bin2hex(random_bytes($length));
115 * Force to be used in PHPUnit context.
117 protected abstract function isInTestsContext(): bool;