]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/visitor/FrontControllerMockHelper.php
Feature: support any tag separator
[github/shaarli/Shaarli.git] / tests / front / controller / visitor / FrontControllerMockHelper.php
CommitLineData
dd09ec52
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
dd09ec52 6
dd09ec52
A
7use Shaarli\Bookmark\BookmarkServiceInterface;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Container\ShaarliTestContainer;
10use Shaarli\Formatter\BookmarkFormatter;
11use Shaarli\Formatter\BookmarkRawFormatter;
12use Shaarli\Formatter\FormatterFactory;
13use Shaarli\Plugin\PluginManager;
14use Shaarli\Render\PageBuilder;
15use Shaarli\Render\PageCacheManager;
16use Shaarli\Security\LoginManager;
17use Shaarli\Security\SessionManager;
18
19/**
20 * Trait FrontControllerMockHelper
21 *
22 * Helper trait used to initialize the ShaarliContainer and mock its services for controller tests.
23 *
24 * @property ShaarliTestContainer $container
25 * @package Shaarli\Front\Controller
26 */
27trait FrontControllerMockHelper
28{
29 /** @var ShaarliTestContainer */
30 protected $container;
31
32 /**
ef00f9d2 33 * Mock the container instance and initialize container's services used by tests
dd09ec52
A
34 */
35 protected function createContainer(): void
36 {
37 $this->container = $this->createMock(ShaarliTestContainer::class);
dd09ec52 38
dd09ec52
A
39 $this->container->loginManager = $this->createMock(LoginManager::class);
40
41 // Config
42 $this->container->conf = $this->createMock(ConfigManager::class);
43 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
b3bd8c3e
A
44 if ($parameter === 'general.tags_separator') {
45 return '@';
46 }
47
66063ed1 48 return $default === null ? $parameter : $default;
dd09ec52
A
49 });
50
51 // PageBuilder
52 $this->container->pageBuilder = $this->createMock(PageBuilder::class);
53 $this->container->pageBuilder
54 ->method('render')
55 ->willReturnCallback(function (string $template): string {
56 return $template;
57 })
58 ;
59
60 // Plugin Manager
61 $this->container->pluginManager = $this->createMock(PluginManager::class);
62
63 // BookmarkService
64 $this->container->bookmarkService = $this->createMock(BookmarkServiceInterface::class);
65
66 // Formatter
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);
72 })
73 ;
74
75 // CacheManager
76 $this->container->pageCacheManager = $this->createMock(PageCacheManager::class);
77
78 // SessionManager
79 $this->container->sessionManager = $this->createMock(SessionManager::class);
80
81 // $_SERVER
82 $this->container->environment = [
83 'SERVER_NAME' => 'shaarli',
84 'SERVER_PORT' => '80',
b93cfeba 85 'REQUEST_URI' => '/subfolder/daily-rss',
a8c11451 86 'REMOTE_ADDR' => '1.2.3.4',
b93cfeba 87 'SCRIPT_NAME' => '/subfolder/index.php',
dd09ec52 88 ];
818b3193
A
89
90 $this->container->basePath = '/subfolder';
dd09ec52
A
91 }
92
93 /**
94 * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
95 *
96 * @param mixed $variables Array reference to populate.
97 */
98 protected function assignTemplateVars(array &$variables): void
99 {
100 $this->container->pageBuilder
dd09ec52
A
101 ->method('assign')
102 ->willReturnCallback(function ($key, $value) use (&$variables) {
103 $variables[$key] = $value;
104
105 return $this;
106 })
107 ;
108 }
109
66063ed1
A
110 protected static function generateString(int $length): string
111 {
112 // bin2hex(random_bytes) generates string twice as long as given parameter
113 $length = (int) ceil($length / 2);
114
115 return bin2hex(random_bytes($length));
116 }
117
dd09ec52
A
118 /**
119 * Force to be used in PHPUnit context.
120 */
a5a9cf23 121 protected abstract function isInTestsContext(): bool;
dd09ec52 122}