aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/FrontAdminControllerMockHelper.php
blob: 2b9f2ef197c963edfd6b15d19a4313d941b2eec5 (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
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Admin;

use Shaarli\Container\ShaarliTestContainer;
use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper;
use Shaarli\History;

/**
 * Trait FrontControllerMockHelper
 *
 * Helper trait used to initialize the ShaarliContainer and mock its services for admin controller tests.
 *
 * @property ShaarliTestContainer $container
 */
trait FrontAdminControllerMockHelper
{
    use FrontControllerMockHelper {
        FrontControllerMockHelper::createContainer as parentCreateContainer;
    }

    /**
     * Mock the container instance
     */
    protected function createContainer(): void
    {
        $this->parentCreateContainer();

        $this->container->history = $this->createMock(History::class);

        $this->container->loginManager->method('isLoggedIn')->willReturn(true);
        $this->container->sessionManager->method('checkToken')->willReturn(true);
    }


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

                return $this->container->sessionManager;
            })
        ;
    }
}