diff options
Diffstat (limited to 'tests/front/controller/visitor/FrontControllerMockHelper.php')
-rw-r--r-- | tests/front/controller/visitor/FrontControllerMockHelper.php | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/front/controller/visitor/FrontControllerMockHelper.php b/tests/front/controller/visitor/FrontControllerMockHelper.php new file mode 100644 index 00000000..fc0bb7d1 --- /dev/null +++ b/tests/front/controller/visitor/FrontControllerMockHelper.php | |||
@@ -0,0 +1,118 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
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; | ||
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 | */ | ||
27 | trait FrontControllerMockHelper | ||
28 | { | ||
29 | /** @var ShaarliTestContainer */ | ||
30 | protected $container; | ||
31 | |||
32 | /** | ||
33 | * Mock the container instance and initialize container's services used by tests | ||
34 | */ | ||
35 | protected function createContainer(): void | ||
36 | { | ||
37 | $this->container = $this->createMock(ShaarliTestContainer::class); | ||
38 | |||
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) { | ||
44 | return $default === null ? $parameter : $default; | ||
45 | }); | ||
46 | |||
47 | // PageBuilder | ||
48 | $this->container->pageBuilder = $this->createMock(PageBuilder::class); | ||
49 | $this->container->pageBuilder | ||
50 | ->method('render') | ||
51 | ->willReturnCallback(function (string $template): string { | ||
52 | return $template; | ||
53 | }) | ||
54 | ; | ||
55 | |||
56 | // Plugin Manager | ||
57 | $this->container->pluginManager = $this->createMock(PluginManager::class); | ||
58 | |||
59 | // BookmarkService | ||
60 | $this->container->bookmarkService = $this->createMock(BookmarkServiceInterface::class); | ||
61 | |||
62 | // Formatter | ||
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); | ||
68 | }) | ||
69 | ; | ||
70 | |||
71 | // CacheManager | ||
72 | $this->container->pageCacheManager = $this->createMock(PageCacheManager::class); | ||
73 | |||
74 | // SessionManager | ||
75 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
76 | |||
77 | // $_SERVER | ||
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', | ||
84 | ]; | ||
85 | |||
86 | $this->container->basePath = '/subfolder'; | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution. | ||
91 | * | ||
92 | * @param mixed $variables Array reference to populate. | ||
93 | */ | ||
94 | protected function assignTemplateVars(array &$variables): void | ||
95 | { | ||
96 | $this->container->pageBuilder | ||
97 | ->method('assign') | ||
98 | ->willReturnCallback(function ($key, $value) use (&$variables) { | ||
99 | $variables[$key] = $value; | ||
100 | |||
101 | return $this; | ||
102 | }) | ||
103 | ; | ||
104 | } | ||
105 | |||
106 | protected static function generateString(int $length): string | ||
107 | { | ||
108 | // bin2hex(random_bytes) generates string twice as long as given parameter | ||
109 | $length = (int) ceil($length / 2); | ||
110 | |||
111 | return bin2hex(random_bytes($length)); | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * Force to be used in PHPUnit context. | ||
116 | */ | ||
117 | protected abstract function isInTestsContext(): bool; | ||
118 | } | ||