tests: Use a real app for bootstrapping

Makes the tests less isolated, but streamlines
the autoloading and other stuff in a way that's
more maintainable than before.
This commit is contained in:
Johannes Meyer 2023-07-04 16:41:22 +02:00
parent 82d39be642
commit 597cb5c94d
4 changed files with 145 additions and 113 deletions

View File

@ -16,6 +16,7 @@
</rule>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>library/Icinga/Application/Cli.php</exclude-pattern>
<exclude-pattern>library/Icinga/Application/Test.php</exclude-pattern>
<exclude-pattern>library/Icinga/Application/StaticWeb.php</exclude-pattern>
<exclude-pattern>library/Icinga/Application/EmbeddedWeb.php</exclude-pattern>
<exclude-pattern>library/Icinga/Application/functions.php</exclude-pattern>

View File

@ -200,7 +200,7 @@ class Cli extends ApplicationBootstrap
* @throws ProgrammingError
* @return void
*/
private function assertRunningOnCli()
protected function assertRunningOnCli()
{
if (Platform::isCli()) {
return;

View File

@ -0,0 +1,134 @@
<?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'));
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);
}
}
return $this;
}
private function setupComposerAutoload(): self
{
$vendorAutoload = $this->getBaseDir('/vendor/autoload.php');
if (file_exists($vendorAutoload)) {
require_once $vendorAutoload;
}
return $this;
}
}

View File

@ -1,117 +1,14 @@
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
$basePath = getenv('ICINGAWEB_BASEDIR') ?: realpath(dirname(__FILE__) . '/../..');
$applicationPath = $basePath . '/application';
$modulePath = getenv('ICINGAWEB_MODULES_DIR') ?: ($basePath . '/modules');
$icingaLibPath = getenv('ICINGAWEB_ICINGA_LIB') ?: ($basePath . '/library/Icinga');
$libraryPath = $basePath . '/library';
$testLibraryPath = realpath(dirname(__FILE__) . '/library');
$configPath = $basePath . '/../config';
namespace Icinga\Test {
// Is usually done in the application's bootstrap and is used by some of our internals
if (!defined('ICINGAWEB_APPDIR')) {
define('ICINGAWEB_APPDIR', $applicationPath);
use Icinga\Application\Test;
$basePath = getenv('ICINGAWEB_BASEDIR') ?: realpath(dirname(__FILE__) . '/../..');
$libraryPath = getenv('ICINGAWEB_ICINGA_LIB') ?: ($basePath . '/library/Icinga');
$configPath = $basePath . '/test/config';
require $libraryPath . '/Application/Test.php';
Test::start($basePath, $configPath);
}
if (!defined('ICINGA_LIBDIR')) {
define('ICINGA_LIBDIR', $libraryPath);
}
// This is needed to get the Zend Plugin loader working
set_include_path(implode(PATH_SEPARATOR, [
$libraryPath,
$basePath . DIRECTORY_SEPARATOR . 'vendor',
get_include_path()
]));
$vendorAutoload = $basePath . '/vendor/autoload.php';
if (file_exists($vendorAutoload)) {
require_once $vendorAutoload;
}
require_once($icingaLibPath . '/Test/ClassLoader.php');
$loader = new Icinga\Test\ClassLoader();
$loader->registerNamespace('Tests', $testLibraryPath);
$loader->registerNamespace('Icinga', $icingaLibPath);
$loader->registerNamespace('Icinga\\Forms', $applicationPath . '/forms');
$libraryPaths = getenv('ICINGAWEB_LIBDIR');
if ($libraryPaths !== false) {
$libraryPaths = array_filter(array_map(
'realpath',
explode(':', $libraryPaths)
), 'is_dir');
} else {
$libraryPaths = is_dir('/usr/share/icinga-php')
? ['/usr/share/icinga-php']
: [];
}
foreach ($libraryPaths as $externalLibraryPath) {
$libPaths = array_flip(scandir($externalLibraryPath));
unset($libPaths['.']);
unset($libPaths['..']);
$libPaths = array_keys($libPaths);
foreach ($libPaths as $libPath) {
$libPath = join(DIRECTORY_SEPARATOR, [$externalLibraryPath, $libPath]);
if (is_dir(realpath($libPath))) {
$libAutoLoader = join(DIRECTORY_SEPARATOR, [$libPath, 'vendor', 'autoload.php']);
if (file_exists($libAutoLoader)) {
require_once $libAutoLoader;
}
}
}
}
$modulePaths = getenv('ICINGAWEB_MODULE_DIRS');
if ($modulePaths) {
$modulePaths = preg_split('/:/', $modulePaths, -1, PREG_SPLIT_NO_EMPTY);
}
if (! $modulePaths) {
$modulePaths = [];
foreach (preg_split('/:/', $modulePath, -1, PREG_SPLIT_NO_EMPTY) 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)) {
$loader->registerNamespace($moduleNamespace, $moduleLibraryPath);
}
$moduleTestPath = "$path/test/php/Lib";
if (is_dir($moduleTestPath)) {
$loader->registerNamespace('Tests\\' . $moduleNamespace . '\\Lib', $moduleTestPath);
}
$moduleFormPath = "$path/application/forms";
if (is_dir($moduleFormPath)) {
$loader->registerNamespace($moduleNamespace . '\\Forms', $moduleFormPath);
}
}
$loader->register();
set_include_path(
implode(
PATH_SEPARATOR,
array($libraryPath . '/vendor', get_include_path())
)
);
require_once 'Zend/Loader/Autoloader.php';
\Zend_Loader_Autoloader::getInstance();
Icinga\Application\Config::$configDir = $configPath;