diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:46:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:46:43 +0000 |
commit | 3e02d5aff85babc3ffbfcf52313f2108e313aa23 (patch) | |
tree | b01f3923360c20a6a504aff42d45670c58af3ec5 /library/Icinga/Application/Test.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-3e02d5aff85babc3ffbfcf52313f2108e313aa23.tar.xz icingaweb2-3e02d5aff85babc3ffbfcf52313f2108e313aa23.zip |
Adding upstream version 2.12.1.upstream/2.12.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Icinga/Application/Test.php')
-rw-r--r-- | library/Icinga/Application/Test.php | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/library/Icinga/Application/Test.php b/library/Icinga/Application/Test.php new file mode 100644 index 0000000..74321ea --- /dev/null +++ b/library/Icinga/Application/Test.php @@ -0,0 +1,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; + } +} |