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