3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
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 and initialize container's services used by tests
36 protected function createContainer(): void
38 $this->container
= $this->createMock(ShaarliTestContainer
::class);
40 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
43 $this->container
->conf
= $this->createMock(ConfigManager
::class);
44 $this->container
->conf
->method('get')->willReturnCallback(function (string $parameter, $default) {
45 return $default === null ? $parameter : $default;
49 $this->container
->pageBuilder
= $this->createMock(PageBuilder
::class);
50 $this->container
->pageBuilder
52 ->willReturnCallback(function (string $template): string {
58 $this->container
->pluginManager
= $this->createMock(PluginManager
::class);
61 $this->container
->bookmarkService
= $this->createMock(BookmarkServiceInterface
::class);
64 $this->container
->formatterFactory
= $this->createMock(FormatterFactory
::class);
65 $this->container
->formatterFactory
66 ->method('getFormatter')
67 ->willReturnCallback(function (): BookmarkFormatter
{
68 return new BookmarkRawFormatter($this->container
->conf
, true);
73 $this->container
->pageCacheManager
= $this->createMock(PageCacheManager
::class);
76 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
79 $this->container
->environment
= [
80 'SERVER_NAME' => 'shaarli',
81 'SERVER_PORT' => '80',
82 'REQUEST_URI' => '/daily-rss',
87 * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
89 * @param mixed $variables Array reference to populate.
91 protected function assignTemplateVars(array &$variables): void
93 $this->container
->pageBuilder
94 ->expects(static::atLeastOnce())
96 ->willReturnCallback(function ($key, $value) use (&$variables) {
97 $variables[$key] = $value;
104 protected static function generateString(int $length): string
106 // bin2hex(random_bytes) generates string twice as long as given parameter
107 $length = (int) ceil($length / 2);
109 return bin2hex(random_bytes($length));
113 * Force to be used in PHPUnit context.
115 protected abstract function createMock($originalClassName): MockObject
;