diff --git a/src/PhpUnit/BaseDatabaseTrait.php b/src/PhpUnit/BaseDatabaseTrait.php index 69bde19..c279f5f 100644 --- a/src/PhpUnit/BaseDatabaseTrait.php +++ b/src/PhpUnit/BaseDatabaseTrait.php @@ -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 { @@ -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; @@ -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, + ), ); } } diff --git a/src/PhpUnit/FixtureStore.php b/src/PhpUnit/FixtureStore.php new file mode 100644 index 0000000..b2a57a4 --- /dev/null +++ b/src/PhpUnit/FixtureStore.php @@ -0,0 +1,111 @@ + + * + * 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() + { + } +} diff --git a/src/PhpUnit/RefreshDatabaseTrait.php b/src/PhpUnit/RefreshDatabaseTrait.php index b1c34c8..6d83006 100644 --- a/src/PhpUnit/RefreshDatabaseTrait.php +++ b/src/PhpUnit/RefreshDatabaseTrait.php @@ -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; } @@ -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(); }