Skip to content

Commit

Permalink
fix: Move the ReloadDatabaseTrait state to the FixtureStore (#45)
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
theofidry authored Nov 22, 2023
1 parent 841ac01 commit 76485f1
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 7 deletions.
83 changes: 78 additions & 5 deletions src/PhpUnit/BaseDatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

namespace Hautelook\AliceBundle\PhpUnit;

use function count;
use function is_a;
use LogicException;
use function sprintf;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use function trigger_deprecation;

trait BaseDatabaseTrait
{
Expand Down Expand Up @@ -47,6 +49,8 @@ trait BaseDatabaseTrait
protected static ?string $connection = null;

/**
* @deprecated Use FixtureStore::getFixtures() instead.
*
* @var array|null Contain loaded fixture from alice
*/
protected static ?array $fixtures = null;
Expand All @@ -61,13 +65,82 @@ protected static function ensureKernelTestCase(): void
protected static function populateDatabase(): void
{
$container = static::$kernel->getContainer();
static::$fixtures = $container->get('hautelook_alice.loader')->load(
$loader = $container->get('hautelook_alice.loader');

self::populateFixtureStore();

static::$fixtures = $loader->load(
new Application(static::$kernel), // OK this is ugly... But there is no other way without redesigning LoaderInterface from the ground.
$container->get('doctrine')->getManager(static::$manager),
static::$bundles,
$container->get('doctrine')->getManager(FixtureStore::getManagerName()),
FixtureStore::getBundles(),
static::$kernel->getEnvironment(),
static::$append,
static::$purgeWithTruncate,
FixtureStore::isAppend(),
FixtureStore::isPurgeWithTruncate(),
);

FixtureStore::setFixtures(static::$fixtures);
}

private static function populateFixtureStore(): void
{
if (null !== static::$manager) {
self::triggerFixtureStoreDeprecation(
'the database manager',
'setManagerName',
);

FixtureStore::setManagerName(static::$manager);
}

if (count(static::$bundles) !== 0) {
self::triggerFixtureStoreDeprecation(
'the loaded bundles',
'setBundles',
);

FixtureStore::setBundles(static::$bundles);
}

if (false !== static::$append) {
self::triggerFixtureStoreDeprecation(
'the append parameter',
'setAppend',
);

FixtureStore::setAppend(static::$append);
}

if (true !== static::$purgeWithTruncate) {
self::triggerFixtureStoreDeprecation(
'the purge with truncate parameter',
'setPurgeWithTruncate',
);

FixtureStore::setPurgeWithTruncate(static::$purgeWithTruncate);
}

if (null !== static::$connection) {
self::triggerFixtureStoreDeprecation(
'the connection name',
'setConnectionName',
);

FixtureStore::setConnectionName(static::$connection);
}
}

private static function triggerFixtureStoreDeprecation(
string $subject,
string $methodNameReplacement,
): void {
trigger_deprecation(
'hautelook/alice-bundle',
'2.12.2',
sprintf(
'Setting up %s via the class static is deprecated. Use FixtureStore::%s() instead.',
$subject,
$methodNameReplacement,
),
);
}
}
111 changes: 111 additions & 0 deletions src/PhpUnit/FixtureStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/*
* This file is part of the Hautelook\AliceBundle package.
*
* (c) Baldur Rensch <brensch@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Hautelook\AliceBundle\PhpUnit;

final class FixtureStore
{
/**
* @var string|null The name of the Doctrine manager to use
*/
private static ?string $managerName = null;

/**
* @var string[] The list of bundles where to look for fixtures
*/
private static array $bundles = [];

/**
* @var bool Append fixtures instead of purging
*/
private static bool $append = false;

/**
* @var bool Use TRUNCATE to purge
*/
private static bool $purgeWithTruncate = true;

/**
* @var string|null The name of the Doctrine connection to use
*/
private static ?string $connectionName = null;

/**
* @var array|null Contain loaded fixture from alice
*/
private static ?array $fixtures = null;

public static function getFixtures(): ?array
{
return self::$fixtures;
}

public static function setFixtures(array $fixtures): void
{
self::$fixtures = $fixtures;
}

public static function getManagerName(): ?string
{
return self::$managerName;
}

public static function setManagerName(?string $managerName): void
{
self::$managerName = $managerName;
}

public static function getBundles(): array
{
return self::$bundles;
}

public static function setBundles(array $bundles): void
{
self::$bundles = $bundles;
}

public static function isAppend(): bool
{
return self::$append;
}

public static function setAppend(bool $append): void
{
self::$append = $append;
}

public static function isPurgeWithTruncate(): bool
{
return self::$purgeWithTruncate;
}

public static function setPurgeWithTruncate(bool $purgeWithTruncate): void
{
self::$purgeWithTruncate = $purgeWithTruncate;
}

public static function getConnectionName(): ?string
{
return self::$connectionName;
}

public static function setConnectionName(?string $connectionName): void
{
self::$connectionName = $connectionName;
}

private function __construct()
{
}
}
6 changes: 4 additions & 2 deletions src/PhpUnit/RefreshDatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ protected static function bootKernel(array $options = []): KernelInterface
static::populateDatabase();

RefreshDatabaseState::setDbPopulated(true);
} else {
static::$fixtures = FixtureStore::getFixtures();
}

$container = static::$kernel->getContainer();
$container->get('doctrine')->getConnection(static::$connection)->beginTransaction();
$container->get('doctrine')->getConnection(FixtureStore::getConnectionName())->beginTransaction();

return $kernel;
}
Expand All @@ -50,7 +52,7 @@ protected static function ensureKernelShutdown(): void
}

if (null !== $container) {
$connection = $container->get('doctrine')->getConnection(static::$connection);
$connection = $container->get('doctrine')->getConnection(FixtureStore::getConnectionName());
if ($connection->isTransactionActive()) {
$connection->rollback();
}
Expand Down

0 comments on commit 76485f1

Please sign in to comment.