Skip to content

Commit 76485f1

Browse files
authored
fix: Move the ReloadDatabaseTrait state to the FixtureStore (#45)
Closes #44
1 parent 841ac01 commit 76485f1

File tree

3 files changed

+193
-7
lines changed

3 files changed

+193
-7
lines changed

Diff for: src/PhpUnit/BaseDatabaseTrait.php

+78-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
namespace Hautelook\AliceBundle\PhpUnit;
1515

16+
use function count;
1617
use function is_a;
1718
use LogicException;
1819
use function sprintf;
1920
use Symfony\Bundle\FrameworkBundle\Console\Application;
2021
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
22+
use function trigger_deprecation;
2123

2224
trait BaseDatabaseTrait
2325
{
@@ -47,6 +49,8 @@ trait BaseDatabaseTrait
4749
protected static ?string $connection = null;
4850

4951
/**
52+
* @deprecated Use FixtureStore::getFixtures() instead.
53+
*
5054
* @var array|null Contain loaded fixture from alice
5155
*/
5256
protected static ?array $fixtures = null;
@@ -61,13 +65,82 @@ protected static function ensureKernelTestCase(): void
6165
protected static function populateDatabase(): void
6266
{
6367
$container = static::$kernel->getContainer();
64-
static::$fixtures = $container->get('hautelook_alice.loader')->load(
68+
$loader = $container->get('hautelook_alice.loader');
69+
70+
self::populateFixtureStore();
71+
72+
static::$fixtures = $loader->load(
6573
new Application(static::$kernel), // OK this is ugly... But there is no other way without redesigning LoaderInterface from the ground.
66-
$container->get('doctrine')->getManager(static::$manager),
67-
static::$bundles,
74+
$container->get('doctrine')->getManager(FixtureStore::getManagerName()),
75+
FixtureStore::getBundles(),
6876
static::$kernel->getEnvironment(),
69-
static::$append,
70-
static::$purgeWithTruncate,
77+
FixtureStore::isAppend(),
78+
FixtureStore::isPurgeWithTruncate(),
79+
);
80+
81+
FixtureStore::setFixtures(static::$fixtures);
82+
}
83+
84+
private static function populateFixtureStore(): void
85+
{
86+
if (null !== static::$manager) {
87+
self::triggerFixtureStoreDeprecation(
88+
'the database manager',
89+
'setManagerName',
90+
);
91+
92+
FixtureStore::setManagerName(static::$manager);
93+
}
94+
95+
if (count(static::$bundles) !== 0) {
96+
self::triggerFixtureStoreDeprecation(
97+
'the loaded bundles',
98+
'setBundles',
99+
);
100+
101+
FixtureStore::setBundles(static::$bundles);
102+
}
103+
104+
if (false !== static::$append) {
105+
self::triggerFixtureStoreDeprecation(
106+
'the append parameter',
107+
'setAppend',
108+
);
109+
110+
FixtureStore::setAppend(static::$append);
111+
}
112+
113+
if (true !== static::$purgeWithTruncate) {
114+
self::triggerFixtureStoreDeprecation(
115+
'the purge with truncate parameter',
116+
'setPurgeWithTruncate',
117+
);
118+
119+
FixtureStore::setPurgeWithTruncate(static::$purgeWithTruncate);
120+
}
121+
122+
if (null !== static::$connection) {
123+
self::triggerFixtureStoreDeprecation(
124+
'the connection name',
125+
'setConnectionName',
126+
);
127+
128+
FixtureStore::setConnectionName(static::$connection);
129+
}
130+
}
131+
132+
private static function triggerFixtureStoreDeprecation(
133+
string $subject,
134+
string $methodNameReplacement,
135+
): void {
136+
trigger_deprecation(
137+
'hautelook/alice-bundle',
138+
'2.12.2',
139+
sprintf(
140+
'Setting up %s via the class static is deprecated. Use FixtureStore::%s() instead.',
141+
$subject,
142+
$methodNameReplacement,
143+
),
71144
);
72145
}
73146
}

Diff for: src/PhpUnit/FixtureStore.php

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Hautelook\AliceBundle package.
5+
*
6+
* (c) Baldur Rensch <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Hautelook\AliceBundle\PhpUnit;
15+
16+
final class FixtureStore
17+
{
18+
/**
19+
* @var string|null The name of the Doctrine manager to use
20+
*/
21+
private static ?string $managerName = null;
22+
23+
/**
24+
* @var string[] The list of bundles where to look for fixtures
25+
*/
26+
private static array $bundles = [];
27+
28+
/**
29+
* @var bool Append fixtures instead of purging
30+
*/
31+
private static bool $append = false;
32+
33+
/**
34+
* @var bool Use TRUNCATE to purge
35+
*/
36+
private static bool $purgeWithTruncate = true;
37+
38+
/**
39+
* @var string|null The name of the Doctrine connection to use
40+
*/
41+
private static ?string $connectionName = null;
42+
43+
/**
44+
* @var array|null Contain loaded fixture from alice
45+
*/
46+
private static ?array $fixtures = null;
47+
48+
public static function getFixtures(): ?array
49+
{
50+
return self::$fixtures;
51+
}
52+
53+
public static function setFixtures(array $fixtures): void
54+
{
55+
self::$fixtures = $fixtures;
56+
}
57+
58+
public static function getManagerName(): ?string
59+
{
60+
return self::$managerName;
61+
}
62+
63+
public static function setManagerName(?string $managerName): void
64+
{
65+
self::$managerName = $managerName;
66+
}
67+
68+
public static function getBundles(): array
69+
{
70+
return self::$bundles;
71+
}
72+
73+
public static function setBundles(array $bundles): void
74+
{
75+
self::$bundles = $bundles;
76+
}
77+
78+
public static function isAppend(): bool
79+
{
80+
return self::$append;
81+
}
82+
83+
public static function setAppend(bool $append): void
84+
{
85+
self::$append = $append;
86+
}
87+
88+
public static function isPurgeWithTruncate(): bool
89+
{
90+
return self::$purgeWithTruncate;
91+
}
92+
93+
public static function setPurgeWithTruncate(bool $purgeWithTruncate): void
94+
{
95+
self::$purgeWithTruncate = $purgeWithTruncate;
96+
}
97+
98+
public static function getConnectionName(): ?string
99+
{
100+
return self::$connectionName;
101+
}
102+
103+
public static function setConnectionName(?string $connectionName): void
104+
{
105+
self::$connectionName = $connectionName;
106+
}
107+
108+
private function __construct()
109+
{
110+
}
111+
}

Diff for: src/PhpUnit/RefreshDatabaseTrait.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ protected static function bootKernel(array $options = []): KernelInterface
3434
static::populateDatabase();
3535

3636
RefreshDatabaseState::setDbPopulated(true);
37+
} else {
38+
static::$fixtures = FixtureStore::getFixtures();
3739
}
3840

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

4244
return $kernel;
4345
}
@@ -50,7 +52,7 @@ protected static function ensureKernelShutdown(): void
5052
}
5153

5254
if (null !== $container) {
53-
$connection = $container->get('doctrine')->getConnection(static::$connection);
55+
$connection = $container->get('doctrine')->getConnection(FixtureStore::getConnectionName());
5456
if ($connection->isTransactionActive()) {
5557
$connection->rollback();
5658
}

0 commit comments

Comments
 (0)