summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Application/Test.php
blob: 74321ea1bba63826bf290beb350d01f933761218 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

namespace Icinga\Application;

use Icinga\Web\Request;
use Icinga\Web\Response;

require_once __DIR__ . '/Cli.php';

class Test extends Cli
{
    protected $isCli = false;

    /** @var Request */
    private $request;

    /** @var Response */
    private $response;

    public function setRequest(Request $request): void
    {
        $this->request = $request;
    }

    public function getRequest(): Request
    {
        assert(isset($this->request), 'BaseTestCase should have set the request');

        return $this->request;
    }

    public function setResponse(Response $response): void
    {
        $this->response = $response;
    }

    public function getResponse(): Response
    {
        assert(isset($this->request), 'BaseTestCase should have set the response');

        return $this->response;
    }

    public function getFrontController()
    {
        return $this; // Callers are expected to only call getRequest or getResponse, hence the app should suffice
    }

    protected function bootstrap()
    {
        $this->assertRunningOnCli();
        $this->setupLogging()
            ->setupErrorHandling()
            ->loadLibraries()
            ->setupComposerAutoload()
            ->loadConfig()
            ->setupModuleAutoloaders()
            ->setupTimezone()
            ->prepareInternationalization()
            ->setupInternationalization()
            ->parseBasicParams()
            ->setupLogger()
            ->setupModuleManager()
            ->setupUserBackendFactory()
            ->setupFakeAuthentication();
    }

    public function setupAutoloader()
    {
        parent::setupAutoloader();

        if (($icingaLibDir = getenv('ICINGAWEB_ICINGA_LIB')) !== false) {
            $this->getLoader()->registerNamespace('Icinga', $icingaLibDir);
        }

        // Conflicts with `Tests\Icinga\Module\...\Lib`. But it seems it's not needed anyway...
        //$this->getLoader()->registerNamespace('Tests', $this->getBaseDir('test/php/library'));
        $this->getLoader()->registerNamespace('Tests\\Icinga\\Lib', $this->getBaseDir('test/php/Lib'));

        return $this;
    }

    protected function detectTimezone()
    {
        return 'UTC';
    }

    private function setupModuleAutoloaders(): self
    {
        $modulePaths = getenv('ICINGAWEB_MODULE_DIRS');

        if ($modulePaths) {
            $modulePaths = preg_split('/:/', $modulePaths, -1, PREG_SPLIT_NO_EMPTY);
        }

        if (! $modulePaths) {
            $modulePaths = [];
            foreach ($this->getAvailableModulePaths() as $path) {
                $candidates = array_flip(scandir($path));
                unset($candidates['.'], $candidates['..']);
                foreach ($candidates as $candidate => $_) {
                    $modulePaths[] = "$path/$candidate";
                }
            }
        }

        foreach ($modulePaths as $path) {
            $module = basename($path);

            $moduleNamespace = 'Icinga\\Module\\' . ucfirst($module);
            $moduleLibraryPath = "$path/library/" . ucfirst($module);

            if (is_dir($moduleLibraryPath)) {
                $this->getLoader()->registerNamespace($moduleNamespace, $moduleLibraryPath, "$path/application");
            }

            $moduleTestPath = "$path/test/php/Lib";
            if (is_dir($moduleTestPath)) {
                $this->getLoader()->registerNamespace('Tests\\' . $moduleNamespace . '\\Lib', $moduleTestPath);
            }

            $composerAutoloader = "$path/vendor/autoload.php";
            if (file_exists($composerAutoloader)) {
                require_once $composerAutoloader;
            }
        }

        return $this;
    }

    private function setupComposerAutoload(): self
    {
        $vendorAutoload = $this->getBaseDir('/vendor/autoload.php');
        if (file_exists($vendorAutoload)) {
            require_once $vendorAutoload;
        }

        return $this;
    }
}