Skip to content

Commit 4b60475

Browse files
committed
refactor(v3): remove Factories and ResetDatabase traits
1 parent b58a4cd commit 4b60475

File tree

71 files changed

+377
-1462
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+377
-1462
lines changed

phpunit-deprecation-baseline.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
<line number="25" hash="c6af5d66288d0667e424978000f29571e4954b81">
55
<issue><![CDATA[Since symfony/framework-bundle 6.4: Not setting the "framework.php_errors.log" config option is deprecated. It will default to "true" in 7.0.]]></issue>
66

7-
<issue><![CDATA[Since zenstruck/foundry 2.9: Trait Zenstruck\Foundry\Test\Factories is deprecated and will be removed in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.9.md to upgrade.]]></issue>
8-
<issue><![CDATA[Since 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.]]></issue>
9-
107
<issue><![CDATA[Since doctrine/mongodb-odm 2.14: Not using native lazy objects is deprecated and will be impossible in Doctrine MongoDB ODM 3.0.]]></issue>
118
<issue><![CDATA[Since symfony/framework-bundle 7.3: Not setting the "property_info.with_constructor_extractor" option explicitly is deprecated because its default value will change in version 8.0.]]></issue>
129
<issue><![CDATA[Since symfony/console 7.4: The "Symfony\Component\Console\Application::add()" method is deprecated and will be removed in Symfony 8.0, use "Symfony\Component\Console\Application::addCommand()" instead.]]></issue>

src/Configuration.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313

1414
use Faker;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Zenstruck\Foundry\Exception\FactoriesTraitNotUsed;
1716
use Zenstruck\Foundry\Exception\FoundryNotBooted;
1817
use Zenstruck\Foundry\Exception\PersistenceDisabled;
1918
use Zenstruck\Foundry\Exception\PersistenceNotAvailable;
2019
use Zenstruck\Foundry\InMemory\CannotEnableInMemory;
2120
use Zenstruck\Foundry\InMemory\InMemoryRepositoryRegistry;
2221
use Zenstruck\Foundry\Persistence\PersistedObjectsTracker;
2322
use Zenstruck\Foundry\Persistence\PersistenceManager;
24-
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
25-
2623
/**
2724
* @author Kevin Bond <kevinbond@gmail.com>
2825
*
@@ -118,10 +115,6 @@ public static function instance(): self
118115
throw new FoundryNotBooted();
119116
}
120117

121-
if (!FoundryExtension::isEnabled()) {
122-
FactoriesTraitNotUsed::throwIfComingFromKernelTestCaseWithoutFactoriesTrait();
123-
}
124-
125118
return \is_callable(self::$instance) ? (self::$instance)() : self::$instance;
126119
}
127120

@@ -143,10 +136,6 @@ public static function bootForDataProvider(\Closure|self $configuration): void
143136
self::$instance->bootedForDataProvider = true;
144137
}
145138

146-
/**
147-
* /!\ Until PHPUnit 9 support is not dropped, this method MUST NOT call Configuration::instance()
148-
* Otherwise, it will reboot the kernel, leading to complex bugs.
149-
*/
150139
public static function shutdown(): void
151140
{
152141
PersistedObjectsTracker::reset();

src/Exception/FactoriesTraitNotUsed.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/Exception/FoundryNotBooted.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@
1111

1212
namespace Zenstruck\Foundry\Exception;
1313

14-
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
15-
1614
/**
1715
* @author Kevin Bond <kevinbond@gmail.com>
1816
*/
1917
final class FoundryNotBooted extends \LogicException
2018
{
2119
public function __construct()
2220
{
23-
$message = FoundryExtension::shouldBeEnabled()
24-
? 'Foundry is not yet booted. Ensure ZenstruckFoundryBundle is enabled. If in a test, ensure Foundry\'s extension is enabled.'
25-
: 'Foundry is not yet booted. Ensure ZenstruckFoundryBundle is enabled. If in a test, ensure your TestCase has the Factories trait.';
26-
27-
parent::__construct($message);
21+
parent::__construct('Foundry is not yet booted. Ensure ZenstruckFoundryBundle is enabled. If in a test, ensure Foundry\'s extension is enabled.');
2822
}
2923
}

src/PHPUnit/FoundryExtension.php

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,38 @@
2727
* @internal
2828
* @author Nicolas PHILIPPE <nikophil@gmail.com>
2929
*/
30-
if (\interface_exists(Runner\Extension\Extension::class)) {
31-
final class FoundryExtension implements Runner\Extension\Extension
32-
{
33-
public const PARAMETER_AUTO_RESET_DATABASE_CLASS = 'enabled-auto-reset';
30+
final class FoundryExtension implements Runner\Extension\Extension
31+
{
32+
public const PARAMETER_AUTO_RESET_DATABASE_CLASS = 'enabled-auto-reset';
3433

35-
private static bool $enabled = false;
34+
private static bool $enabled = false;
3635

37-
public function bootstrap(
38-
TextUI\Configuration\Configuration $configuration,
39-
Runner\Extension\Facade $facade,
40-
Runner\Extension\ParameterCollection $parameters,
41-
): void {
42-
// shutdown Foundry if for some reason it has been booted before
43-
if (Configuration::isBooted()) {
44-
Configuration::shutdown();
45-
}
36+
public function bootstrap(
37+
TextUI\Configuration\Configuration $configuration,
38+
Runner\Extension\Facade $facade,
39+
Runner\Extension\ParameterCollection $parameters,
40+
): void {
41+
// shutdown Foundry if for some reason it has been booted before
42+
if (Configuration::isBooted()) {
43+
Configuration::shutdown();
44+
}
4645

47-
$autoResetEnabled = $parameters->has(self::PARAMETER_AUTO_RESET_DATABASE_CLASS)
48-
&& 'true' === $parameters->get(self::PARAMETER_AUTO_RESET_DATABASE_CLASS);
46+
$autoResetEnabled = $parameters->has(self::PARAMETER_AUTO_RESET_DATABASE_CLASS)
47+
&& 'true' === $parameters->get(self::PARAMETER_AUTO_RESET_DATABASE_CLASS);
4948

50-
// ⚠️ order matters within each event
49+
// ⚠️ order matters within each event
5150
$subscribers = [
5251
Event\TestSuite\Started::class => [new ResetDatabaseOnTestSuiteStarted($autoResetEnabled)],
5352
Event\Test\DataProviderMethodCalled::class => [new BootFoundryOnDataProviderMethodCalled()],
5453
Event\Test\DataProviderMethodFinished::class => [new ShutdownFoundryOnDataProviderMethodFinished()],
5554
Event\Test\PreparationStarted::class => [
56-
new BootFoundryOnPreparationStarted(),
57-
new ResetDatabaseOnPreparationStarted($autoResetEnabled),
58-
new EnableInMemoryOnPreparationStarted(),
55+
new BootFoundryOnPreparationStarted(),
56+
new ResetDatabaseOnPreparationStarted($autoResetEnabled),
57+
new EnableInMemoryOnPreparationStarted(),
5958
],
6059
Event\Test\Prepared::class => [
61-
new BuildStoryOnTestPrepared(),
62-
new TriggerDataProviderPersistenceOnTestPrepared(),
60+
new BuildStoryOnTestPrepared(),
61+
new TriggerDataProviderPersistenceOnTestPrepared(),
6362
],
6463
Event\Test\Finished::class => [new ShutdownFoundryOnTestFinished()],
6564
Event\TestRunner\Finished::class => [new DisplayFakerSeedOnApplicationFinished()],
@@ -72,35 +71,21 @@ public function bootstrap(
7271
$subscribers = \array_filter(
7372
$subscribers,
7473
static fn($subscriber) => !$subscriber instanceof DataProviderSubscriberInterface
75-
);
74+
);
7675
}
7776

7877
$facade->registerSubscribers(...$subscribers);
7978

80-
self::$enabled = true;
81-
}
82-
83-
public static function shouldBeEnabled(): bool
84-
{
85-
return \defined('PHPUNIT_COMPOSER_INSTALL') && !self::isEnabled() && ConstraintRequirement::from('>=10')->isSatisfiedBy(Runner\Version::id());
86-
}
87-
88-
public static function isEnabled(): bool
89-
{
90-
return self::$enabled;
91-
}
79+
self::$enabled = true;
9280
}
93-
} else {
94-
final class FoundryExtension
81+
82+
public static function shouldBeEnabled(): bool
9583
{
96-
public static function shouldBeEnabled(): bool
97-
{
98-
return false;
99-
}
84+
return \defined('PHPUNIT_COMPOSER_INSTALL') && !self::isEnabled() && ConstraintRequirement::from('>=10')->isSatisfiedBy(Runner\Version::id());
85+
}
10086

101-
public static function isEnabled(): bool
102-
{
103-
return false;
104-
}
87+
public static function isEnabled(): bool
88+
{
89+
return self::$enabled;
10590
}
10691
}

src/PHPUnit/ResetDatabase/ResetDatabaseOnPreparationStarted.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ private function shouldReset(Event\Code\TestMethod $test): bool
7676
return true;
7777
}
7878

79-
return $hasResetDatabaseAttribute
80-
81-
// let's use ResetDatabase trait as a marker, the same way we're using the attribute
82-
|| (new \ReflectionClass($test->className()))->hasMethod('_resetDatabaseBeforeFirstTest');
79+
return $hasResetDatabaseAttribute;
8380
}
8481
}

src/PHPUnit/ResetDatabase/ResetDatabaseOnTestSuiteStarted.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ private function shouldReset(string $testClassName): bool
6969
return true;
7070
}
7171

72-
return AttributeReader::classOrParentsHasAttribute($testClassName, ResetDatabase::class)
73-
74-
// let's use ResetDatabase trait as a marker, the same way we're using the attribute
75-
|| (new \ReflectionClass($testClassName))->hasMethod('_resetDatabaseBeforeFirstTest');
72+
return AttributeReader::classOrParentsHasAttribute($testClassName, ResetDatabase::class);
7673
}
7774
}

src/Test/Factories.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)