aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/FrontControllerMockHelper.php
blob: b65607e74a34862b06d8dcd08f95173d69b5d0f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller;

use PHPUnit\Framework\MockObject\MockObject;
use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Container\ShaarliTestContainer;
use Shaarli\Formatter\BookmarkFormatter;
use Shaarli\Formatter\BookmarkRawFormatter;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\PageBuilder;
use Shaarli\Render\PageCacheManager;
use Shaarli\Security\LoginManager;
use Shaarli\Security\SessionManager;

/**
 * Trait FrontControllerMockHelper
 *
 * Helper trait used to initialize the ShaarliContainer and mock its services for controller tests.
 *
 * @property ShaarliTestContainer $container
 * @package Shaarli\Front\Controller
 */
trait FrontControllerMockHelper
{
    /** @var ShaarliTestContainer */
    protected $container;

    /**
     * Mock the container instance
     */
    protected function createContainer(): void
    {
        $this->container = $this->createMock(ShaarliTestContainer::class);
    }

    /**
     * Initialize container's services used by tests
     */
    protected function createValidContainerMockSet(): void
    {
        $this->container->loginManager = $this->createMock(LoginManager::class);

        // Config
        $this->container->conf = $this->createMock(ConfigManager::class);
        $this->container->conf->method('get')->willReturnCallback(function (string $parameter, $default) {
            return $default;
        });

        // PageBuilder
        $this->container->pageBuilder = $this->createMock(PageBuilder::class);
        $this->container->pageBuilder
            ->method('render')
            ->willReturnCallback(function (string $template): string {
                return $template;
            })
        ;

        // Plugin Manager
        $this->container->pluginManager = $this->createMock(PluginManager::class);

        // BookmarkService
        $this->container->bookmarkService = $this->createMock(BookmarkServiceInterface::class);

        // Formatter
        $this->container->formatterFactory = $this->createMock(FormatterFactory::class);
        $this->container->formatterFactory
            ->method('getFormatter')
            ->willReturnCallback(function (): BookmarkFormatter {
                return new BookmarkRawFormatter($this->container->conf, true);
            })
        ;

        // CacheManager
        $this->container->pageCacheManager = $this->createMock(PageCacheManager::class);

        // SessionManager
        $this->container->sessionManager = $this->createMock(SessionManager::class);

        // $_SERVER
        $this->container->environment = [
            'SERVER_NAME' => 'shaarli',
            'SERVER_PORT' => '80',
            'REQUEST_URI' => '/daily-rss',
        ];
    }

    /**
     * Pass a reference of an array which will be populated by `pageBuilder->assign` calls during execution.
     *
     * @param mixed $variables Array reference to populate.
     */
    protected function assignTemplateVars(array &$variables): void
    {
        $this->container->pageBuilder
            ->expects(static::atLeastOnce())
            ->method('assign')
            ->willReturnCallback(function ($key, $value) use (&$variables) {
                $variables[$key] = $value;

                return $this;
            })
        ;
    }

    /**
     * Force to be used in PHPUnit context.
     */
    protected abstract function createMock($originalClassName): MockObject;
}