aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/FrontControllerMockHelper.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/front/controller/FrontControllerMockHelper.php')
-rw-r--r--tests/front/controller/FrontControllerMockHelper.php114
1 files changed, 114 insertions, 0 deletions
diff --git a/tests/front/controller/FrontControllerMockHelper.php b/tests/front/controller/FrontControllerMockHelper.php
new file mode 100644
index 00000000..b65607e7
--- /dev/null
+++ b/tests/front/controller/FrontControllerMockHelper.php
@@ -0,0 +1,114 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
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 /**
34 * Mock the container instance
35 */
36 protected function createContainer(): void
37 {
38 $this->container = $this->createMock(ShaarliTestContainer::class);
39 }
40
41 /**
42 * Initialize container's services used by tests
43 */
44 protected function createValidContainerMockSet(): void
45 {
46 $this->container->loginManager = $this->createMock(LoginManager::class);
47
48 // Config
49 $this->container->conf = $this->createMock(ConfigManager::class);
50 $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
51 return $default;
52 });
53
54 // PageBuilder
55 $this->container->pageBuilder = $this->createMock(PageBuilder::class);
56 $this->container->pageBuilder
57 ->method('render')
58 ->willReturnCallback(function (string $template): string {
59 return $template;
60 })
61 ;
62
63 // Plugin Manager
64 $this->container->pluginManager = $this->createMock(PluginManager::class);
65
66 // BookmarkService
67 $this->container->bookmarkService = $this->createMock(BookmarkServiceInterface::class);
68
69 // Formatter
70 $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
71 $this->container->formatterFactory
72 ->method('getFormatter')
73 ->willReturnCallback(function (): BookmarkFormatter {
74 return new BookmarkRawFormatter($this->container->conf, true);
75 })
76 ;
77
78 // CacheManager
79 $this->container->pageCacheManager = $this->createMock(PageCacheManager::class);
80
81 // SessionManager
82 $this->container->sessionManager = $this->createMock(SessionManager::class);
83
84 // $_SERVER
85 $this->container->environment = [
86 'SERVER_NAME' => 'shaarli',
87 'SERVER_PORT' => '80',
88 'REQUEST_URI' => '/daily-rss',
89 ];
90 }
91
92 /**
93 * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
94 *
95 * @param mixed $variables Array reference to populate.
96 */
97 protected function assignTemplateVars(array &$variables): void
98 {
99 $this->container->pageBuilder
100 ->expects(static::atLeastOnce())
101 ->method('assign')
102 ->willReturnCallback(function ($key, $value) use (&$variables) {
103 $variables[$key] = $value;
104
105 return $this;
106 })
107 ;
108 }
109
110 /**
111 * Force to be used in PHPUnit context.
112 */
113 protected abstract function createMock($originalClassName): MockObject;
114}