mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-26 23:34:08 +02:00
Update test bootstrapping (#5050)
This commit is contained in:
commit
d311005089
@ -16,6 +16,7 @@
|
|||||||
</rule>
|
</rule>
|
||||||
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
|
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
|
||||||
<exclude-pattern>library/Icinga/Application/Cli.php</exclude-pattern>
|
<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/StaticWeb.php</exclude-pattern>
|
||||||
<exclude-pattern>library/Icinga/Application/EmbeddedWeb.php</exclude-pattern>
|
<exclude-pattern>library/Icinga/Application/EmbeddedWeb.php</exclude-pattern>
|
||||||
<exclude-pattern>library/Icinga/Application/functions.php</exclude-pattern>
|
<exclude-pattern>library/Icinga/Application/functions.php</exclude-pattern>
|
||||||
|
@ -452,10 +452,15 @@ abstract class ApplicationBootstrap
|
|||||||
|
|
||||||
protected function getAvailableModulePaths()
|
protected function getAvailableModulePaths()
|
||||||
{
|
{
|
||||||
$paths = array();
|
$paths = [];
|
||||||
$configured = $this->config->get('global', 'module_path', $this->baseDir . '/modules');
|
|
||||||
|
$configured = getenv('ICINGAWEB_MODULES_DIR');
|
||||||
|
if (! $configured) {
|
||||||
|
$configured = $this->config->get('global', 'module_path', $this->baseDir . '/modules');
|
||||||
|
}
|
||||||
|
|
||||||
$nextIsPhar = false;
|
$nextIsPhar = false;
|
||||||
foreach (explode(':', $configured) as $path) {
|
foreach (explode(PATH_SEPARATOR, $configured) as $path) {
|
||||||
if ($path === 'phar') {
|
if ($path === 'phar') {
|
||||||
$nextIsPhar = true;
|
$nextIsPhar = true;
|
||||||
continue;
|
continue;
|
||||||
|
@ -200,7 +200,7 @@ class Cli extends ApplicationBootstrap
|
|||||||
* @throws ProgrammingError
|
* @throws ProgrammingError
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function assertRunningOnCli()
|
protected function assertRunningOnCli()
|
||||||
{
|
{
|
||||||
if (Platform::isCli()) {
|
if (Platform::isCli()) {
|
||||||
return;
|
return;
|
||||||
|
134
library/Icinga/Application/Test.php
Normal file
134
library/Icinga/Application/Test.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,26 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
if (!function_exists('t')) {
|
|
||||||
function t()
|
|
||||||
{
|
|
||||||
return func_get_arg(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!function_exists('mt')) {
|
|
||||||
function mt()
|
|
||||||
{
|
|
||||||
return func_get_arg(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Icinga\Test {
|
namespace Icinga\Test {
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Icinga\Web\Request;
|
||||||
|
use Icinga\Web\Response;
|
||||||
use ipl\I18n\NoopTranslator;
|
use ipl\I18n\NoopTranslator;
|
||||||
use ipl\I18n\StaticTranslator;
|
use ipl\I18n\StaticTranslator;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
@ -39,6 +24,7 @@ namespace Icinga\Test {
|
|||||||
* Path to application/
|
* Path to application/
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
|
* @deprecated Use Icinga::app()->getApplicationDir() instead
|
||||||
*/
|
*/
|
||||||
public static $appDir;
|
public static $appDir;
|
||||||
|
|
||||||
@ -46,13 +32,15 @@ namespace Icinga\Test {
|
|||||||
* Path to library/Icinga
|
* Path to library/Icinga
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
|
* @deprecated Use Icinga::app()->getLibraryDir('Icinga') instead
|
||||||
*/
|
*/
|
||||||
public static $libDir;
|
public static $libDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to etc/
|
* Path to etc/
|
||||||
*
|
*
|
||||||
* @var
|
* @var string
|
||||||
|
* @deprecated Use Icinga::app()->getBaseDir('etc') instead
|
||||||
*/
|
*/
|
||||||
public static $etcDir;
|
public static $etcDir;
|
||||||
|
|
||||||
@ -60,6 +48,7 @@ namespace Icinga\Test {
|
|||||||
* Path to test/php/
|
* Path to test/php/
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
|
* @deprecated Use Icinga::app()->getBaseDir('test/php') instead
|
||||||
*/
|
*/
|
||||||
public static $testDir;
|
public static $testDir;
|
||||||
|
|
||||||
@ -67,6 +56,7 @@ namespace Icinga\Test {
|
|||||||
* Path to share/icinga2-web
|
* Path to share/icinga2-web
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
|
* @deprecated Unused
|
||||||
*/
|
*/
|
||||||
public static $shareDir;
|
public static $shareDir;
|
||||||
|
|
||||||
@ -74,6 +64,7 @@ namespace Icinga\Test {
|
|||||||
* Path to modules/
|
* Path to modules/
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
|
* @deprecated Use Icinga::app()->getModuleManager()->getModuleDirs() instead
|
||||||
*/
|
*/
|
||||||
public static $moduleDir;
|
public static $moduleDir;
|
||||||
|
|
||||||
@ -103,45 +94,11 @@ namespace Icinga\Test {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/** @var Request */
|
||||||
* Setup the default timezone
|
private $requestMock;
|
||||||
*/
|
|
||||||
public static function setupTimezone()
|
|
||||||
{
|
|
||||||
date_default_timezone_set('UTC');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/** @var Response */
|
||||||
* Setup test path environment
|
private $responseMock;
|
||||||
*
|
|
||||||
* @throws RuntimeException
|
|
||||||
*/
|
|
||||||
public static function setupDirectories()
|
|
||||||
{
|
|
||||||
$baseDir = getenv('ICINGAWEB_BASEDIR') ?: realpath(__DIR__ . '/../../../');
|
|
||||||
if ($baseDir === false) {
|
|
||||||
throw new RuntimeException('Application base dir not found');
|
|
||||||
}
|
|
||||||
|
|
||||||
$libDir = getenv('ICINGAWEB_ICINGA_LIB') ?: realpath($baseDir . '/library/Icinga');
|
|
||||||
if ($libDir === false) {
|
|
||||||
throw new RuntimeException('Icinga library dir not found');
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$appDir = $baseDir . '/application';
|
|
||||||
self::$libDir = $libDir;
|
|
||||||
self::$etcDir = $baseDir . '/etc';
|
|
||||||
self::$testDir = $baseDir . '/test/php';
|
|
||||||
self::$shareDir = $baseDir . '/share/icinga2-web';
|
|
||||||
|
|
||||||
$modulesDir = getenv('ICINGAWEB_MODULES_DIR');
|
|
||||||
if ($modulesDir && strpos($modulesDir, ':') !== false) {
|
|
||||||
// Only use the env variable if it contains a single path
|
|
||||||
$modulesDir = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$moduleDir = $modulesDir ?: $baseDir . '/modules';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup MVC bootstrapping and ensure that the Icinga-Mock gets reinitialized
|
* Setup MVC bootstrapping and ensure that the Icinga-Mock gets reinitialized
|
||||||
@ -149,20 +106,16 @@ namespace Icinga\Test {
|
|||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
$this->setupRequestMock();
|
||||||
|
$this->setupResponseMock();
|
||||||
|
|
||||||
StaticTranslator::$instance = new NoopTranslator();
|
StaticTranslator::$instance = new NoopTranslator();
|
||||||
$this->setupIcingaMock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function setupRequestMock()
|
||||||
* Setup mock object for the application's bootstrap
|
|
||||||
*
|
|
||||||
* @return Mockery\Mock
|
|
||||||
*/
|
|
||||||
protected function setupIcingaMock()
|
|
||||||
{
|
{
|
||||||
$requestMock = Mockery::mock('Icinga\Web\Request')->shouldDeferMissing();
|
$this->requestMock = Mockery::mock('Icinga\Web\Request')->shouldDeferMissing();
|
||||||
$requestMock->shouldReceive('getPathInfo')->andReturn('')->byDefault()
|
$this->requestMock->shouldReceive('getPathInfo')->andReturn('')->byDefault()
|
||||||
->shouldReceive('getBaseUrl')->andReturn('/')->byDefault()
|
->shouldReceive('getBaseUrl')->andReturn('/')->byDefault()
|
||||||
->shouldReceive('getQuery')->andReturn(array())->byDefault()
|
->shouldReceive('getQuery')->andReturn(array())->byDefault()
|
||||||
->shouldReceive('getParam')->with(Mockery::type('string'), Mockery::type('string'))
|
->shouldReceive('getParam')->with(Mockery::type('string'), Mockery::type('string'))
|
||||||
@ -170,43 +123,33 @@ namespace Icinga\Test {
|
|||||||
return $default;
|
return $default;
|
||||||
})->byDefault();
|
})->byDefault();
|
||||||
|
|
||||||
$responseMock = Mockery::mock('Icinga\Web\Response')->shouldDeferMissing();
|
Icinga::app()->setRequest($this->requestMock);
|
||||||
// Can't express this as demeter chains. See: https://github.com/padraic/mockery/issues/59
|
}
|
||||||
$bootstrapMock = Mockery::mock('Icinga\Application\ApplicationBootstrap')->shouldDeferMissing();
|
|
||||||
$libDir = dirname(self::$libDir);
|
|
||||||
$bootstrapMock->shouldReceive('getFrontController')->andReturn($bootstrapMock)
|
|
||||||
->shouldReceive('getApplicationDir')->andReturn(self::$appDir)
|
|
||||||
->shouldReceive('getLibraryDir')->andReturnUsing(function ($subdir = null) use ($libDir) {
|
|
||||||
if ($subdir !== null) {
|
|
||||||
$libDir .= '/' . ltrim($subdir, '/');
|
|
||||||
}
|
|
||||||
return $libDir;
|
|
||||||
})
|
|
||||||
->shouldReceive('getRequest')->andReturn($requestMock)
|
|
||||||
->shouldReceive('getResponse')->andReturn($responseMock);
|
|
||||||
|
|
||||||
Icinga::setApp($bootstrapMock, true);
|
private function setupResponseMock()
|
||||||
return $bootstrapMock;
|
{
|
||||||
|
$this->responseMock = Mockery::mock('Icinga\Web\Response')->shouldDeferMissing();
|
||||||
|
Icinga::app()->setResponse($this->responseMock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the currently active request mock object
|
* Return the currently active request mock object
|
||||||
*
|
*
|
||||||
* @return Icinga\Web\Request
|
* @return Request
|
||||||
*/
|
*/
|
||||||
public function getRequestMock()
|
public function getRequestMock()
|
||||||
{
|
{
|
||||||
return Icinga::app()->getRequest();
|
return $this->requestMock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the currently active response mock object
|
* Return the currently active response mock object
|
||||||
*
|
*
|
||||||
* @return Icinga\Web\Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function getResponseMock()
|
public function getResponseMock()
|
||||||
{
|
{
|
||||||
return Icinga::app()->getFrontController()->getResponse();
|
return $this->responseMock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -362,7 +305,4 @@ namespace Icinga\Test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseTestCase::setupTimezone();
|
|
||||||
BaseTestCase::setupDirectories();
|
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,22 @@
|
|||||||
|
|
||||||
namespace Tests\Icinga\Module\Monitoring\Application\Views\Helpers;
|
namespace Tests\Icinga\Module\Monitoring\Application\Views\Helpers;
|
||||||
|
|
||||||
|
use Icinga\Application\Icinga;
|
||||||
use Zend_View_Helper_MonitoringFlags;
|
use Zend_View_Helper_MonitoringFlags;
|
||||||
use Icinga\Test\BaseTestCase;
|
use Icinga\Test\BaseTestCase;
|
||||||
|
|
||||||
require_once realpath(BaseTestCase::$moduleDir . '/monitoring/application/views/helpers/MonitoringFlags.php');
|
|
||||||
|
|
||||||
class MonitoringFlagsTest extends BaseTestCase
|
class MonitoringFlagsTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
require_once realpath(
|
||||||
|
Icinga::app()->getModuleManager()->getModuleDir('monitoring')
|
||||||
|
. '/application/views/helpers/MonitoringFlags.php'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testHosts1()
|
public function testHosts1()
|
||||||
{
|
{
|
||||||
$testArray = array(
|
$testArray = array(
|
||||||
|
@ -3,12 +3,11 @@
|
|||||||
|
|
||||||
namespace Tests\Icinga\Module\Monitoring\Application\Views\Helpers;
|
namespace Tests\Icinga\Module\Monitoring\Application\Views\Helpers;
|
||||||
|
|
||||||
|
use Icinga\Application\Icinga;
|
||||||
use Icinga\Web\View;
|
use Icinga\Web\View;
|
||||||
use Zend_View_Helper_PluginOutput;
|
use Zend_View_Helper_PluginOutput;
|
||||||
use Icinga\Test\BaseTestCase;
|
use Icinga\Test\BaseTestCase;
|
||||||
|
|
||||||
require_once realpath(BaseTestCase::$moduleDir . '/monitoring/application/views/helpers/PluginOutput.php');
|
|
||||||
|
|
||||||
class PluginOutputTest extends BaseTestCase
|
class PluginOutputTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
/** @var Zend_View_Helper_PluginOutput */
|
/** @var Zend_View_Helper_PluginOutput */
|
||||||
@ -24,6 +23,11 @@ class PluginOutputTest extends BaseTestCase
|
|||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
|
require_once realpath(
|
||||||
|
Icinga::app()->getModuleManager()->getModuleDir('monitoring')
|
||||||
|
. '/application/views/helpers/PluginOutput.php'
|
||||||
|
);
|
||||||
|
|
||||||
$this->helper = $h = new Zend_View_Helper_PluginOutput;
|
$this->helper = $h = new Zend_View_Helper_PluginOutput;
|
||||||
$h->setView(new View());
|
$h->setView(new View());
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,6 @@ use Mockery;
|
|||||||
use Icinga\Test\BaseTestCase;
|
use Icinga\Test\BaseTestCase;
|
||||||
use Icinga\Module\Monitoring\Object\Macro;
|
use Icinga\Module\Monitoring\Object\Macro;
|
||||||
|
|
||||||
require_once realpath(BaseTestCase::$moduleDir . '/monitoring/library/Monitoring/Object/Macro.php');
|
|
||||||
|
|
||||||
class MacroTest extends BaseTestCase
|
class MacroTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
public function testHostMacros()
|
public function testHostMacros()
|
||||||
|
@ -28,6 +28,7 @@ class PhpCommand extends Command
|
|||||||
* --verbose Be more verbose.
|
* --verbose Be more verbose.
|
||||||
* --build Enable reporting.
|
* --build Enable reporting.
|
||||||
* --include Pattern to use for including files/test cases.
|
* --include Pattern to use for including files/test cases.
|
||||||
|
* --phpunit Path to the phpunit executable
|
||||||
*
|
*
|
||||||
* EXAMPLES
|
* EXAMPLES
|
||||||
*
|
*
|
||||||
@ -42,11 +43,15 @@ class PhpCommand extends Command
|
|||||||
*/
|
*/
|
||||||
public function unitAction()
|
public function unitAction()
|
||||||
{
|
{
|
||||||
$build = $this->params->shift('build');
|
$build = (bool) $this->params->shift('build', false);
|
||||||
$include = $this->params->shift('include');
|
$include = $this->params->shift('include');
|
||||||
|
$phpUnit = $this->params->shift('phpunit');
|
||||||
|
|
||||||
$phpUnit = exec('which phpunit');
|
if (! $phpUnit) {
|
||||||
if (!file_exists($phpUnit)) {
|
$phpUnit = exec('which phpunit');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! file_exists($phpUnit)) {
|
||||||
$this->fail('PHPUnit not found. Please install PHPUnit to be able to run the unit-test suites.');
|
$this->fail('PHPUnit not found. Please install PHPUnit to be able to run the unit-test suites.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +80,7 @@ class PhpCommand extends Command
|
|||||||
$temp->create('phpunit.xml', $phpunitXml->saveXML());
|
$temp->create('phpunit.xml', $phpunitXml->saveXML());
|
||||||
|
|
||||||
chdir($baseDir);
|
chdir($baseDir);
|
||||||
$command = $this->getEnvironmentVariables()
|
$command = $this->getEnvironmentVariables($build)
|
||||||
. $phpUnit
|
. $phpUnit
|
||||||
. " -c {$temp->resolvePath('phpunit.xml')}"
|
. " -c {$temp->resolvePath('phpunit.xml')}"
|
||||||
. ' ' . join(' ', array_merge($options, $this->params->getAllStandalone()));
|
. ' ' . join(' ', array_merge($options, $this->params->getAllStandalone()));
|
||||||
@ -182,7 +187,7 @@ class PhpCommand extends Command
|
|||||||
/**
|
/**
|
||||||
* Setup some required environment variables
|
* Setup some required environment variables
|
||||||
*/
|
*/
|
||||||
protected function getEnvironmentVariables()
|
protected function getEnvironmentVariables(bool $build)
|
||||||
{
|
{
|
||||||
$modulePaths = [];
|
$modulePaths = [];
|
||||||
foreach (Icinga::app()->getModuleManager()->getModuleInfo() as $module) {
|
foreach (Icinga::app()->getModuleManager()->getModuleInfo() as $module) {
|
||||||
@ -197,10 +202,14 @@ class PhpCommand extends Command
|
|||||||
$vars[] = sprintf('ICINGAWEB_MODULE_DIRS=%s', implode(':', $modulePaths));
|
$vars[] = sprintf('ICINGAWEB_MODULE_DIRS=%s', implode(':', $modulePaths));
|
||||||
|
|
||||||
// Disabled as the bootstrap.php for PHPUnit and class BaseTestCase can't handle multiple paths yet
|
// Disabled as the bootstrap.php for PHPUnit and class BaseTestCase can't handle multiple paths yet
|
||||||
/*$vars[] = sprintf(
|
$vars[] = sprintf(
|
||||||
'ICINGAWEB_MODULES_DIR=%s',
|
'ICINGAWEB_MODULES_DIR=%s',
|
||||||
implode(PATH_SEPARATOR, $this->app->getModuleManager()->getModuleDirs())
|
implode(PATH_SEPARATOR, $this->app->getModuleManager()->getModuleDirs())
|
||||||
);*/
|
);
|
||||||
|
|
||||||
|
if ($build) {
|
||||||
|
$vars[] = 'XDEBUG_MODE=coverage';
|
||||||
|
}
|
||||||
|
|
||||||
return join(' ', $vars) . ' ';
|
return join(' ', $vars) . ' ';
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,6 @@ class AuthenticationBackendReorderFormTest extends BaseTestCase
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
Icinga::app()->shouldReceive('getModuleManager->getLoadedModules')->andReturn([]);
|
|
||||||
$this->getRequestMock()->shouldReceive('getMethod')->andReturn('POST')
|
$this->getRequestMock()->shouldReceive('getMethod')->andReturn('POST')
|
||||||
->shouldReceive('isPost')->andReturn(true)
|
->shouldReceive('isPost')->andReturn(true)
|
||||||
->shouldReceive('getPost')->andReturn(array('backend_newpos' => 'test3|1'));
|
->shouldReceive('getPost')->andReturn(array('backend_newpos' => 'test3|1'));
|
||||||
|
@ -1,139 +0,0 @@
|
|||||||
<?php
|
|
||||||
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
|
||||||
|
|
||||||
namespace Tests\Icinga\Views\Helper;
|
|
||||||
|
|
||||||
use Mockery;
|
|
||||||
use Zend_View_Helper_DateFormat;
|
|
||||||
use Icinga\Test\BaseTestCase;
|
|
||||||
use Icinga\Util\DateTimeFactory;
|
|
||||||
|
|
||||||
require_once BaseTestCase::$appDir . '/views/helpers/DateFormat.php';
|
|
||||||
|
|
||||||
class DateFormatTest extends BaseTestCase
|
|
||||||
{
|
|
||||||
public function tearDown(): void
|
|
||||||
{
|
|
||||||
DateTimeFactory::setConfig(array('timezone' => date_default_timezone_get()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFormatReturnsCorrectDateWithTimezoneApplied()
|
|
||||||
{
|
|
||||||
DateTimeFactory::setConfig(array('timezone' => 'Europe/Berlin'));
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'12:05',
|
|
||||||
$helper->format(1397729100, 'H:i'),
|
|
||||||
'Zend_View_Helper_DateFormat::format does not return a valid' .
|
|
||||||
' formatted time or does not apply the user\'s timezone'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFormatDateReturnsCorrectDate()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d_m_y'));
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'17_04_14',
|
|
||||||
$helper->formatDate(1397729100),
|
|
||||||
'Zend_View_Helper_DateFormat::formatDate does not return a valid formatted date'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFormatTimeReturnsCorrectTime()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock(null, 'H:i'));
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'10:05',
|
|
||||||
$helper->formatTime(1397729100),
|
|
||||||
'Zend_View_Helper_DateFormat::formatTime does not return a valid formatted time'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFormatDatetimeReturnsCorrectDatetime()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d m Y', 'H:i a'));
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'17 04 2014 10:05 am',
|
|
||||||
$helper->formatDateTime(1397729100),
|
|
||||||
'Zend_View_Helper_DateFormat::formatDateTime does not return a valid formatted date and time'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetDateFormatReturnsCorrectFormat()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d/m-y'));
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'd/m-y',
|
|
||||||
$helper->getDateFormat(),
|
|
||||||
'Zend_View_Helper_DateFormat::getDateFormat does not return the user\'s date format'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetTimeFormatReturnsCorrectFormat()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock(null, 'H.i A'));
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'H.i A',
|
|
||||||
$helper->getTimeFormat(),
|
|
||||||
'Zend_View_Helper_DateFormat::getTimeFormat does not return the user\'s time format'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetDatetimeFormatReturnsCorrectFormat()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d/m-y', 'H.i A'));
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'd/m-y H.i A',
|
|
||||||
$helper->getDateTimeFormat(),
|
|
||||||
'Zend_View_Helper_DateFormat::getDateTimeFormat does not return the user\'s date and time format'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetDateFormatReturnsFormatFromConfig()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'd-m-y',
|
|
||||||
$helper->getDateFormat(),
|
|
||||||
'Zend_View_Helper_DateFormat::getDateFormat does not return the format set' .
|
|
||||||
' in the global configuration if the user\'s preferences do not provide one'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetTimeFormatReturnsFormatFromConfig()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'G:i a',
|
|
||||||
$helper->getTimeFormat(),
|
|
||||||
'Zend_View_Helper_DateFormat::getTimeFormat does not return the format set' .
|
|
||||||
' in the global configuration if the user\'s preferences do not provide one'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetDatetimeFormatReturnsFormatFromConfig()
|
|
||||||
{
|
|
||||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'd-m-y G:i a',
|
|
||||||
$helper->getDateTimeFormat(),
|
|
||||||
'Zend_View_Helper_DateFormat::getDateTimeFormat does not return the format set' .
|
|
||||||
' in the global configuration if the user\'s preferences do not provide one'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getRequestMock($dateFormat = null, $timeFormat = null)
|
|
||||||
{
|
|
||||||
return Mockery::mock('\Zend_Controller_Request_Abstract');
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,117 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
||||||
|
|
||||||
$basePath = getenv('ICINGAWEB_BASEDIR') ?: realpath(dirname(__FILE__) . '/../..');
|
namespace Icinga\Test {
|
||||||
$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';
|
|
||||||
|
|
||||||
// Is usually done in the application's bootstrap and is used by some of our internals
|
use Icinga\Application\Test;
|
||||||
if (!defined('ICINGAWEB_APPDIR')) {
|
|
||||||
define('ICINGAWEB_APPDIR', $applicationPath);
|
$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;
|
|
||||||
|
@ -245,7 +245,7 @@ class ConfigTest extends BaseTestCase
|
|||||||
*/
|
*/
|
||||||
public function testWhetherItIsPossibleToRetrieveApplicationConfiguration()
|
public function testWhetherItIsPossibleToRetrieveApplicationConfiguration()
|
||||||
{
|
{
|
||||||
$config = Config::app();
|
$config = Config::app('config', true);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array(
|
array(
|
||||||
|
@ -3,14 +3,23 @@
|
|||||||
|
|
||||||
namespace Tests\Icinga\Web\Paginator\ScrollingStyle;
|
namespace Tests\Icinga\Web\Paginator\ScrollingStyle;
|
||||||
|
|
||||||
|
use Icinga\Application\Icinga;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
use Zend_Paginator;
|
use Zend_Paginator;
|
||||||
use Icinga\Test\BaseTestCase;
|
use Icinga\Test\BaseTestCase;
|
||||||
|
|
||||||
require_once realpath(BaseTestCase::$libDir . '/Web/Paginator/ScrollingStyle/SlidingWithBorder.php');
|
class SlidingWithborderTest extends BaseTestCase
|
||||||
|
|
||||||
class SlidingwithborderTest extends BaseTestCase
|
|
||||||
{
|
{
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
require_once realpath(
|
||||||
|
Icinga::app()->getLibraryDir('Icinga')
|
||||||
|
. '/Web/Paginator/ScrollingStyle/SlidingWithBorder.php'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testGetPages2()
|
public function testGetPages2()
|
||||||
{
|
{
|
||||||
$scrollingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder();
|
$scrollingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder();
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
namespace Tests\Icinga\Web;
|
namespace Tests\Icinga\Web;
|
||||||
|
|
||||||
|
use Icinga\Application\Icinga;
|
||||||
use Icinga\Authentication\Role;
|
use Icinga\Authentication\Role;
|
||||||
use Mockery;
|
|
||||||
use Icinga\Test\BaseTestCase;
|
use Icinga\Test\BaseTestCase;
|
||||||
use Icinga\User;
|
use Icinga\User;
|
||||||
use Icinga\Web\Widget\SearchDashboard;
|
use Icinga\Web\Widget\SearchDashboard;
|
||||||
@ -13,23 +13,12 @@ class SearchDashboardTest extends BaseTestCase
|
|||||||
{
|
{
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$moduleMock = Mockery::mock('Icinga\Application\Modules\Module');
|
parent::setUp();
|
||||||
$searchUrl = (object) array(
|
|
||||||
'title' => 'Hosts',
|
|
||||||
'url' => 'monitoring/list/hosts?sort=host_severity&limit=10'
|
|
||||||
);
|
|
||||||
$moduleMock->shouldReceive('getSearchUrls')->andReturn(array(
|
|
||||||
$searchUrl
|
|
||||||
));
|
|
||||||
$moduleMock->shouldReceive('getName')->andReturn('test');
|
|
||||||
|
|
||||||
$moduleManagerMock = Mockery::mock('Icinga\Application\Modules\Manager');
|
Icinga::app()->getModuleManager()
|
||||||
$moduleManagerMock->shouldReceive('getLoadedModules')->andReturn(array(
|
->loadModule('test-module', '/tmp')
|
||||||
'test-module' => $moduleMock
|
->getModule('test-module')
|
||||||
));
|
->provideSearchUrl('Hosts', 'monitoring/list/hosts?sort=host_severity&limit=10');
|
||||||
|
|
||||||
$bootstrapMock = $this->setupIcingaMock();
|
|
||||||
$bootstrapMock->shouldReceive('getModuleManager')->andReturn($moduleManagerMock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWhetherRenderThrowsAnExceptionWhenHasNoDashlets()
|
public function testWhetherRenderThrowsAnExceptionWhenHasNoDashlets()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user