Add pseudo test class to allow to populate DB (#6478)

Normal use will not do anything other than succeed!
```
php $HUMHUB_VENDOR_BIN/codecept run unit LoadDbTest
```

However, if run with the environment variable HUMHUB_DB_INITIALIZE set, then the default fixtures will be loaded and not unloaded after the test.
```
HUMHUB_DB_INITIALIZE= php $HUMHUB_VENDOR_BIN/codecept run unit LoadDbTest
```
This commit is contained in:
Martin Rüegg 2023-08-16 17:12:43 +02:00 committed by GitHub
parent 4a29d7a729
commit 28fa6d9485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 0 deletions

View File

@ -3,6 +3,7 @@ HumHub Changelog
1.15.0-beta.2 (Unreleased)
--------------------------
- Enh #6478: Add pseudo test class to allow population of DB with standard test data
- Enh #6480: Convert assert* and db* methods to static, in line with general usage pattern
- Enh #6505: Introduce Application interface; now also fire the `onInit` event when the web application has initialized
- Fix #6502: Link notification for pending space approval to manage page

View File

@ -0,0 +1,86 @@
<?php
/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\tests\codeception\unit;
use Codeception\Exception\InjectionException;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Console\Output;
use Codeception\Lib\ModuleContainer;
use Codeception\Module\Yii2;
use Codeception\Test\Metadata;
use tests\codeception\_support\HumHubDbTestCase;
use Yii;
use humhub\modules\queue\models\QueueExclusive;
/**
* Test Debugging DbLoad Helper
*
* Use this test to fill up the database with fixture test data to manually debug
*
* Example usage:
* ```
* HUMHUB_DB_INITIALIZE= php $HUMHUB_VENDOR_BIN/codecept run unit LoadDbTest
* ```
*
* @since 1.15
*/
class LoadDbTest extends HumHubDbTestCase
{
protected bool $humhubLoadDb = false;
protected static Output $humhubConsole;
public function _fixtures(...$args): array
{
if (false !== getenv('HUMHUB_DB_INITIALIZE') || in_array('--load', $_SERVER['argv'], true)) {
$this->humhubLoadDb = true;
$this->fixtureConfig = ['default'];
foreach (['queue', QueueExclusive::tableName()] as $table) {
self::dbDelete($table);
}
self::$humhubConsole->writeln(sprintf(' - Going to load fixtures: %s ...', implode(', ', $this->fixtureConfig)));
}
return parent::_fixtures();
}
public static function setUpBeforeClass(...$args): void
{
self::$humhubConsole = new Output([]);
parent::setUpBeforeClass();
}
/**
* @throws InjectionException
* @throws ModuleException
*/
public function testLoadFixtures()
{
/**
* @var Yii2 $yii2
* @var Metadata $metadata
* @var ModuleContainer $service
*/
$metadata = $this->getMetadata();
$service = $metadata->getService('modules');
$yii2 = $service->getModule('Yii2');
if ($this->humhubLoadDb) {
static::assertNotEmpty($yii2->loadedFixtures);
// prevent fixtures from being unloaded
$yii2->loadedFixtures = [];
} else {
static::assertEmpty($yii2->loadedFixtures);
}
}
}