diff options
Diffstat (limited to 'tests/front/controller/visitor/FrontControllerMockHelper.php')
-rw-r--r-- | tests/front/controller/visitor/FrontControllerMockHelper.php | 119 |
1 files changed, 119 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..e0bd4ecf --- /dev/null +++ b/tests/front/controller/visitor/FrontControllerMockHelper.php | |||
@@ -0,0 +1,119 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
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; | ||
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 | */ | ||
28 | trait FrontControllerMockHelper | ||
29 | { | ||
30 | /** @var ShaarliTestContainer */ | ||
31 | protected $container; | ||
32 | |||
33 | /** | ||
34 | * Mock the container instance and initialize container's services used by tests | ||
35 | */ | ||
36 | protected function createContainer(): void | ||
37 | { | ||
38 | $this->container = $this->createMock(ShaarliTestContainer::class); | ||
39 | |||
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) { | ||
45 | return $default === null ? $parameter : $default; | ||
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', | ||
82 | 'REQUEST_URI' => '/daily-rss', | ||
83 | 'REMOTE_ADDR' => '1.2.3.4', | ||
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 | ->expects(static::atLeastOnce()) | ||
98 | ->method('assign') | ||
99 | ->willReturnCallback(function ($key, $value) use (&$variables) { | ||
100 | $variables[$key] = $value; | ||
101 | |||
102 | return $this; | ||
103 | }) | ||
104 | ; | ||
105 | } | ||
106 | |||
107 | protected static function generateString(int $length): string | ||
108 | { | ||
109 | // bin2hex(random_bytes) generates string twice as long as given parameter | ||
110 | $length = (int) ceil($length / 2); | ||
111 | |||
112 | return bin2hex(random_bytes($length)); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Force to be used in PHPUnit context. | ||
117 | */ | ||
118 | protected abstract function createMock($originalClassName): MockObject; | ||
119 | } | ||