Skip to content

Commit 1f8d452

Browse files
committed
fix: arrange compatibility with PHPUnit 10
1 parent 304e533 commit 1f8d452

14 files changed

+207
-112
lines changed

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141

4242
# old PHPUnit versions
4343
- {php: 8.3, symfony: 7.4.*, phpunit: 9, database: mysql, use-phpunit-extension: 0}
44-
- {php: 8.4, symfony: 7.4.*, phpunit: 10, database: mysql, use-phpunit-extension: 0}
44+
- {php: 8.2, symfony: 7.4.*, phpunit: 10, database: mysql, use-phpunit-extension: 0}
4545
- {php: 8.4, symfony: 7.4.*, phpunit: 11, database: mysql, use-phpunit-extension: 0}
4646
- {php: 8.4, symfony: 7.4.*, phpunit: 11, database: mysql}
4747
- {php: 8.4, symfony: 7.4.*, phpunit: 12, database: mysql, use-phpunit-extension: 0}

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
$csFixerConfig->setFinder(
1313
$csFixerConfig->getFinder()
1414
->notName('WebTestCaseWithBothTraitsInWrongOrderTest.php')
15+
->notName('GenericFactoryUsingBeforeHooksAndResetDatabaseTraitTest.php')
1516
->in(__DIR__.'/utils')
1617
->in(__DIR__.'/config')
1718
);

src/Test/Factories.php

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,68 @@
2020
/**
2121
* @author Kevin Bond <kevinbond@gmail.com>
2222
*/
23-
trait Factories
24-
{
25-
/**
26-
* @internal
27-
* @before
28-
*/
29-
#[Before(5)]
30-
public function _beforeHook(): void
23+
if (!\method_exists(Before::class, '__construct')) { // @phpstan-ignore function.alreadyNarrowedType
24+
trait Factories
3125
{
32-
if (FoundryExtension::isEnabled()) {
33-
trigger_deprecation('zenstruck/foundry', '2.9', \sprintf('Trait %s is deprecated and will be removed in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.', Factories::class));
26+
use CommonFactories;
3427

35-
return;
28+
/**
29+
* @internal
30+
* @before
31+
*/
32+
#[Before]
33+
public function _beforeHook(): void
34+
{
35+
$this->_bootFoundry();
3636
}
3737

38-
if (FoundryExtension::shouldBeEnabled()) {
39-
trigger_deprecation('zenstruck/foundry', '2.9', 'Not using Foundry\'s PHPUnit extension is deprecated and will throw an error in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.');
38+
/**
39+
* @internal
40+
* @after
41+
*/
42+
#[After]
43+
public static function _afterHook(): void
44+
{
45+
self::_shutdownFoundry();
46+
}
47+
}
48+
} else {
49+
trait Factories
50+
{
51+
use CommonFactories;
52+
53+
/** @internal */
54+
#[Before(5)]
55+
public function _beforeHook(): void
56+
{
57+
$this->_bootFoundry();
4058
}
4159

42-
$this->_bootFoundry();
60+
/** @internal */
61+
#[After(5)]
62+
public static function _afterHook(): void
63+
{
64+
self::_shutdownFoundry();
65+
}
4366
}
67+
}
4468

45-
/**
46-
* @internal
47-
* @after
48-
*/
49-
#[After(5)]
50-
public static function _shutdownFoundry(): void
69+
/** @internal */
70+
trait CommonFactories
71+
{
72+
/** @internal */
73+
private function _bootFoundry(): void
5174
{
5275
if (FoundryExtension::isEnabled()) {
76+
trigger_deprecation('zenstruck/foundry', '2.9', \sprintf('Trait %s is deprecated and will be removed in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.', Factories::class));
77+
5378
return;
5479
}
5580

56-
Configuration::shutdown();
57-
}
81+
if (FoundryExtension::shouldBeEnabled()) {
82+
trigger_deprecation('zenstruck/foundry', '2.9', 'Not using Foundry\'s PHPUnit extension is deprecated and will throw an error in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.');
83+
}
5884

59-
/**
60-
* @internal
61-
*/
62-
private function _bootFoundry(): void
63-
{
6485
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore function.impossibleType, function.alreadyNarrowedType
6586
// unit test
6687
Configuration::boot(UnitTestConfig::build());
@@ -77,4 +98,14 @@ private function _bootFoundry(): void
7798
return static::getContainer()->get('.zenstruck_foundry.configuration'); // @phpstan-ignore staticMethod.notFound, return.type
7899
});
79100
}
101+
102+
/** @internal */
103+
private static function _shutdownFoundry(): void
104+
{
105+
if (FoundryExtension::isEnabled()) {
106+
return;
107+
}
108+
109+
Configuration::shutdown();
110+
}
80111
}

src/Test/ResetDatabase.php

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,37 @@
2323
/**
2424
* @author Kevin Bond <kevinbond@gmail.com>
2525
*/
26-
trait ResetDatabase
26+
if (!\method_exists(Before::class, '__construct')) { // @phpstan-ignore function.alreadyNarrowedType
27+
trait ResetDatabase
28+
{
29+
use CommonResetDatabase;
30+
31+
/**
32+
* @internal
33+
* @before
34+
*/
35+
#[Before]
36+
public static function _resetDatabaseBeforeEachTest(): void
37+
{
38+
self::_commonResetDatabaseBeforeEachTest();
39+
}
40+
}
41+
} else {
42+
trait ResetDatabase
43+
{
44+
use CommonResetDatabase;
45+
46+
/** @internal */
47+
#[Before(10)]
48+
public static function _resetDatabaseBeforeEachTest(): void
49+
{
50+
self::_commonResetDatabaseBeforeEachTest();
51+
}
52+
}
53+
}
54+
55+
/** @internal */
56+
trait CommonResetDatabase
2757
{
2858
/**
2959
* @internal
@@ -45,32 +75,7 @@ public static function _resetDatabaseBeforeFirstTest(): void
4575
static::_shutdown(); // @phpstan-ignore staticClassAccess.privateMethod
4676
}
4777

48-
/**
49-
* @internal
50-
* @before
51-
*/
52-
#[Before(10)]
53-
public static function _resetDatabaseBeforeEachTest(): void
54-
{
55-
if (FoundryExtension::isEnabled()) {
56-
return;
57-
}
58-
59-
if (ResetDatabaseManager::canSkipSchemaReset()) {
60-
// can fully skip booting the kernel
61-
return;
62-
}
63-
64-
$kernel = static::_boot(); // @phpstan-ignore staticClassAccess.privateMethod
65-
66-
ResetDatabaseManager::resetBeforeEachTest($kernel);
67-
68-
static::_shutdown(); // @phpstan-ignore staticClassAccess.privateMethod
69-
}
70-
71-
/**
72-
* @internal
73-
*/
78+
/** @internal */
7479
private static function _boot(): KernelInterface
7580
{
7681
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore function.alreadyNarrowedType
@@ -88,9 +93,7 @@ private static function _boot(): KernelInterface
8893
return $kernel;
8994
}
9095

91-
/**
92-
* @internal
93-
*/
96+
/** @internal */
9497
private static function _shutdown(): void
9598
{
9699
if (!\is_subclass_of(static::class, KernelTestCase::class)) { // @phpstan-ignore function.alreadyNarrowedType
@@ -100,4 +103,23 @@ private static function _shutdown(): void
100103
Configuration::shutdown();
101104
static::ensureKernelShutdown();
102105
}
106+
107+
/** @internal */
108+
private static function _commonResetDatabaseBeforeEachTest(): void
109+
{
110+
if (FoundryExtension::isEnabled()) {
111+
return;
112+
}
113+
114+
if (ResetDatabaseManager::canSkipSchemaReset()) {
115+
// can fully skip booting the kernel
116+
return;
117+
}
118+
119+
$kernel = static::_boot(); // @phpstan-ignore staticClassAccess.privateMethod
120+
121+
ResetDatabaseManager::resetBeforeEachTest($kernel);
122+
123+
static::_shutdown(); // @phpstan-ignore staticClassAccess.privateMethod
124+
}
103125
}

tests/Integration/DataProvider/DataProviderWithInMemoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static function provideContact(): iterable
111111
#[DataProvider('provideContactWithLegacyProxy')]
112112
#[AsInMemoryTest]
113113
#[RequiresMethod(\Symfony\Component\VarExporter\LazyProxyTrait::class, 'createLazyProxy')]
114-
#[IgnoreDeprecations('(p|P)roxy')]
114+
#[IgnoreDeprecations]
115115
public function it_can_create_in_memory_objects_in_data_provider_with_legacy_proxy(?Contact $contact = null): void
116116
{
117117
self::assertInstanceOf(Contact::class, $contact);

tests/Integration/Faker/ResetFakerTestTrait.php

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,50 @@
1414
namespace Zenstruck\Foundry\Tests\Integration\Faker;
1515

1616
use PHPUnit\Framework\Attributes\AfterClass;
17+
use PHPUnit\Framework\Attributes\Before;
1718
use PHPUnit\Framework\Attributes\BeforeClass;
1819
use Zenstruck\Foundry\FakerAdapter;
1920

20-
trait ResetFakerTestTrait
21-
{
22-
private static ?string $savedServerSeed = null;
23-
private static ?string $savedEnvSeed = null;
24-
private static ?string $savedGetEnvSeed = null;
25-
26-
private static ?int $currentSeed = null;
27-
28-
#[BeforeClass(10)]
29-
public static function __saveAndResetFakerSeed(): void
21+
if (\method_exists(Before::class, '__construct')) { // @phpstan-ignore function.alreadyNarrowedType
22+
trait ResetFakerTestTrait
3023
{
31-
self::$savedServerSeed = $_SERVER['FOUNDRY_FAKER_SEED'] ?? null;
32-
self::$savedEnvSeed = $_ENV['FOUNDRY_FAKER_SEED'] ?? null;
33-
self::$savedGetEnvSeed = \getenv('FOUNDRY_FAKER_SEED') ?: null;
24+
private static ?string $savedServerSeed = null;
25+
private static ?string $savedEnvSeed = null;
26+
private static ?string $savedGetEnvSeed = null;
3427

35-
$_SERVER['FOUNDRY_FAKER_SEED'] = null;
36-
$_ENV['FOUNDRY_FAKER_SEED'] = null;
37-
\putenv('FOUNDRY_FAKER_SEED');
28+
private static ?int $currentSeed = null;
3829

39-
FakerAdapter::resetFakerSeed();
40-
}
30+
#[BeforeClass(10)]
31+
public static function __saveAndResetFakerSeed(): void
32+
{
33+
self::$savedServerSeed = $_SERVER['FOUNDRY_FAKER_SEED'] ?? null;
34+
self::$savedEnvSeed = $_ENV['FOUNDRY_FAKER_SEED'] ?? null;
35+
self::$savedGetEnvSeed = \getenv('FOUNDRY_FAKER_SEED') ?: null;
4136

42-
#[AfterClass(-10)]
43-
public static function __restoreFakerSeed(): void
44-
{
45-
$_SERVER['FOUNDRY_FAKER_SEED'] = self::$savedServerSeed;
46-
$_ENV['FOUNDRY_FAKER_SEED'] = self::$savedEnvSeed;
47-
if (null === self::$savedGetEnvSeed) {
37+
$_SERVER['FOUNDRY_FAKER_SEED'] = null;
38+
$_ENV['FOUNDRY_FAKER_SEED'] = null;
4839
\putenv('FOUNDRY_FAKER_SEED');
49-
} else {
50-
\putenv('FOUNDRY_FAKER_SEED='.self::$savedGetEnvSeed);
40+
41+
FakerAdapter::resetFakerSeed();
5142
}
5243

53-
$savedValue = self::$savedServerSeed ?? self::$savedEnvSeed ?? self::$savedGetEnvSeed;
54-
FakerAdapter::resetFakerSeed($savedValue ? (int) $savedValue : null);
44+
#[AfterClass(-10)]
45+
public static function __restoreFakerSeed(): void
46+
{
47+
$_SERVER['FOUNDRY_FAKER_SEED'] = self::$savedServerSeed;
48+
$_ENV['FOUNDRY_FAKER_SEED'] = self::$savedEnvSeed;
49+
if (null === self::$savedGetEnvSeed) {
50+
\putenv('FOUNDRY_FAKER_SEED');
51+
} else {
52+
\putenv('FOUNDRY_FAKER_SEED='.self::$savedGetEnvSeed);
53+
}
54+
55+
$savedValue = self::$savedServerSeed ?? self::$savedEnvSeed ?? self::$savedGetEnvSeed;
56+
FakerAdapter::resetFakerSeed($savedValue ? (int) $savedValue : null);
57+
}
58+
}
59+
} else {
60+
trait ResetFakerTestTrait
61+
{
5562
}
5663
}

tests/Integration/Persistence/GenericFactoryUsingBeforeHooksAndResetDatabaseAttributeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Integration\Persistence;
12+
namespace Zenstruck\Foundry\Tests\Integration\Persistence;
1313

1414
use PHPUnit\Framework\Attributes\RequiresPhpunit;
1515
use PHPUnit\Framework\Attributes\RequiresPhpunitExtension;
1616
use Zenstruck\Foundry\Attribute\ResetDatabase;
1717
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
18-
use Zenstruck\Foundry\Tests\Integration\Persistence\GenericFactoryUsingBeforeHooksTestCase;
1918

2019
/**
2120
* @author Nicolas PHILIPPE <nikophil@gmail.com>
@@ -26,4 +25,5 @@
2625
#[ResetDatabase]
2726
final class GenericFactoryUsingBeforeHooksAndResetDatabaseAttributeTest extends GenericFactoryUsingBeforeHooksTestCase
2827
{
28+
use GenericFactoryUsingBeforeHooksTrait;
2929
}

tests/Integration/Persistence/GenericFactoryUsingBeforeHooksAndResetDatabaseTraitTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111

1212
namespace Zenstruck\Foundry\Tests\Integration\Persistence;
1313

14+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1415
use Zenstruck\Foundry\Test\Factories;
1516
use Zenstruck\Foundry\Test\ResetDatabase;
1617

1718
/**
1819
* @author Nicolas PHILIPPE <nikophil@gmail.com>
20+
* @group legacy
1921
*/
22+
#[IgnoreDeprecations]
2023
final class GenericFactoryUsingBeforeHooksAndResetDatabaseTraitTest extends GenericFactoryUsingBeforeHooksTestCase
2124
{
22-
use Factories, ResetDatabase;
25+
use GenericFactoryUsingBeforeHooksTrait, Factories, ResetDatabase;
2326
}

0 commit comments

Comments
 (0)