mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 18:48:51 +02:00
N°7870 ✅ Prerequisites to test portal services (instantiate a symfony service)
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
bootstrap="unittestautoload.php"
|
||||
backupGlobals="true"
|
||||
colors="true"
|
||||
columns="120"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
bootstrap="unittestautoload.php"
|
||||
backupGlobals="true"
|
||||
colors="true"
|
||||
columns="120"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
bootstrap="unittestautoload.php"
|
||||
backupGlobals="true"
|
||||
colors="true"
|
||||
columns="120"
|
||||
|
||||
@@ -9,19 +9,21 @@ namespace Combodo\iTop\Test\UnitTest;
|
||||
use CMDBSource;
|
||||
use DeprecatedCallsLog;
|
||||
use MySQLTransactionNotClosedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionMethod;
|
||||
use SetupUtils;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use const DEBUG_BACKTRACE_IGNORE_ARGS;
|
||||
|
||||
/**
|
||||
* Class ItopTestCase
|
||||
*
|
||||
* Helper class to extend for tests that DO NOT need to access the DataModel or the Database
|
||||
* Helper class to extend for tests that DO NOT need to access the DataModel or the Database,
|
||||
* but still need to access the iTop core classes and optionally boot the Symfony kernel (to access the services container).
|
||||
*
|
||||
* @since 3.0.4 3.1.1 3.2.0 N°6658 move some setUp/tearDown code to the corresponding methods *BeforeClass to speed up tests process time.
|
||||
*/
|
||||
abstract class ItopTestCase extends TestCase
|
||||
abstract class ItopTestCase extends KernelTestCase
|
||||
{
|
||||
public const TEST_LOG_DIR = 'test';
|
||||
|
||||
@@ -70,6 +72,9 @@ abstract class ItopTestCase extends TestCase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Required to boot the portal symfony Kernel
|
||||
$_ENV['PORTAL_ID'] = 'itop-portal';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,6 +147,9 @@ abstract class ItopTestCase extends TestCase
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
// Hack - Required the first time the Portal kernel is booted on a newly installed iTop
|
||||
$_ENV['COMBODO_PORTAL_BASE_ABSOLUTE_PATH'] = __DIR__ . '/../../../../../env-production/itop-portal-base/portal/public/';
|
||||
|
||||
$this->LoadRequiredItopFiles();
|
||||
$this->LoadRequiredTestFiles();
|
||||
}
|
||||
@@ -212,7 +220,7 @@ abstract class ItopTestCase extends TestCase
|
||||
protected function LoadRequiredItopFiles(): void
|
||||
{
|
||||
// At least make sure that the autoloader will be loaded, and that the APPROOT constant is defined
|
||||
require_once __DIR__.'/../../../../approot.inc.php';
|
||||
require_once __DIR__.'/../../../../approot.inc.php';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -541,4 +549,27 @@ abstract class ItopTestCase extends TestCase
|
||||
|
||||
$this->AssertArraysHaveSameItems($aExpected, $aFiles, $sMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Control which Kernel will be loaded when invoking the bootKernel method
|
||||
*
|
||||
* @see static::bootKernel(), static::getContainer()
|
||||
* @see \Combodo\iTop\Kernel, \Combodo\iTop\Portal\Kernel
|
||||
*
|
||||
* @param string $sKernelClass
|
||||
*
|
||||
* @since 3.2.1
|
||||
*/
|
||||
static protected function SetKernelClass(string $sKernelClass): void
|
||||
{
|
||||
$_SERVER['KERNEL_CLASS'] = $sKernelClass;
|
||||
}
|
||||
|
||||
static protected function bootKernel(array $options = []): KernelInterface
|
||||
{
|
||||
if (!array_key_exists('KERNEL_CLASS', $_SERVER)) {
|
||||
throw new \LogicException('static::SetKernelClass() must be called before booting the kernel.');
|
||||
}
|
||||
return parent::bootKernel($options);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Sources\Controller;
|
||||
|
||||
use Combodo\iTop\Portal\Controller\AggregatePageBrickController;
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
|
||||
class BootSymfonyKernelTest extends ItopDataTestCase
|
||||
{
|
||||
public function testInstantiateServiceWithAnExplicitCallToBootTheKernel()
|
||||
{
|
||||
$this->SetKernelClass(\Combodo\iTop\Portal\Kernel::class);
|
||||
self::bootKernel();
|
||||
$controller = static::getContainer()->get(AggregatePageBrickController::class);
|
||||
|
||||
$this->assertInstanceOf(AggregatePageBrickController::class, $controller);
|
||||
}
|
||||
|
||||
public function testInstantiateServiceWithAnAutomaticKernelBoot()
|
||||
{
|
||||
$this->SetKernelClass(\Combodo\iTop\Portal\Kernel::class);
|
||||
$controller = static::getContainer()->get(AggregatePageBrickController::class);
|
||||
|
||||
$this->assertInstanceOf(AggregatePageBrickController::class, $controller);
|
||||
}
|
||||
|
||||
public function testUnspecifiedKernelClassThrowsAnException()
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
$this->expectExceptionMessage('static::SetKernelClass() must be called before booting the kernel');
|
||||
|
||||
static::getContainer();
|
||||
}
|
||||
|
||||
public function testTwoDifferentKernelsCanBeStartedConsecutively()
|
||||
{
|
||||
self::markTestSkipped('This test is still failing: the second kernel container does not find the requested service');
|
||||
|
||||
$this->SetKernelClass(\Combodo\iTop\Kernel::class);
|
||||
self::bootKernel();
|
||||
|
||||
$this->SetKernelClass(\Combodo\iTop\Portal\Kernel::class);
|
||||
self::bootKernel();
|
||||
$controller = static::getContainer()->get(AggregatePageBrickController::class);
|
||||
|
||||
$this->assertInstanceOf(AggregatePageBrickController::class, $controller);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
// Main autoload, this is the one to use in the PHPUnit configuration
|
||||
//
|
||||
// It was previously used to include both the vendor autoloader and our custom base test case classes, but these are now autoloaded from ./src/BasetestCase
|
||||
// This file should then no longer be necessary, but we have to keep it until projects / branches / modules have been corrected.
|
||||
// This file was previously mentioned as deprecated, and now it HAS to be used (see phpunit.xml/bootstrap attribute)
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
// Required to benefit from symfony/framework-bundle's KernelTestCase, which is in a package which is a mix of runtime and test tools
|
||||
require_once __DIR__.'/../../lib/autoload.php';
|
||||
|
||||
Reference in New Issue
Block a user