Skip to content

Commit be8f629

Browse files
committed
feat: trigger persistence from data providers without Factories trait
1 parent c77c643 commit be8f629

17 files changed

+208
-49
lines changed

phpstan.neon

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ parameters:
3030
- identifier: missingType.iterableValue
3131
path: tests/
3232

33-
# We support both PHPUnit versions (this method changed in PHPUnit 10)
34-
- identifier: function.impossibleType
35-
path: src/Test/Factories.php
36-
3733
# PHPStan does not understand PHP version checks
3834
- message: '#Comparison operation "(<|>|<=|>=)" between int<80\d+, 80\d+> and 80\d+ is always (false|true).#'
3935

phpunit-deprecation-baseline.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<issue><![CDATA[Since zenstruck/foundry 2.7: Proxy usage is deprecated in PHP 8.4. You should extend directly PersistentObjectFactory in your factories.
1313
Foundry now leverages the native PHP lazy system to auto-refresh objects (it can be enabled with "zenstruck_foundry.enable_auto_refresh_with_lazy_objects" configuration).
1414
See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.7.md to upgrade.]]></issue>
15-
<issue><![CDATA[Since zenstruck/foundry 2.7: Trait Zenstruck\Foundry\Test\Factories is deprecated and will be removed in Foundry 3.]]></issue>
16-
<issue><![CDATA[Since zenstruck/foundry 2.7: Not using Foundry's PHPUnit extension is deprecated and will throw an error in Foundry 3.]]></issue>
15+
<issue><![CDATA[Since zenstruck/foundry 2.8: Trait Zenstruck\Foundry\Test\Factories is deprecated and will be removed in Foundry 3.]]></issue>
16+
<issue><![CDATA[Since zenstruck/foundry 2.8: Not using Foundry's PHPUnit extension is deprecated and will throw an error in Foundry 3.]]></issue>
1717
</line>
1818
</file>
1919
<file path="vendor/doctrine/deprecations/src/Deprecation.php">

src/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public static function boot(\Closure|self $configuration): void
137137
self::$instance = $configuration;
138138

139139
if (FoundryExtension::shouldBeEnabled()) {
140-
trigger_deprecation('zenstruck/foundry', '2.7', 'Not using Foundry\'s PHPUnit extension is deprecated and will throw an error in Foundry 3.');
140+
trigger_deprecation('zenstruck/foundry', '2.8', 'Not using Foundry\'s PHPUnit extension is deprecated and will throw an error in Foundry 3.');
141141
}
142142
}
143143

src/PHPUnit/BootFoundryOnPreparationStarted.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ final class BootFoundryOnPreparationStarted implements Event\Test\PreparationSta
2727
{
2828
public function notify(Event\Test\PreparationStarted $event): void
2929
{
30-
if (!$event->test()->isTestMethod()) {
30+
$test = $event->test();
31+
32+
if (!$test->isTestMethod()) {
3133
return;
3234
}
35+
/** @var Event\Code\TestMethod $test */
3336

34-
$this->bootFoundry($event->test()->className());
37+
$this->bootFoundry($test->className());
3538
}
3639

3740
/**

src/PHPUnit/BuildStoryOnTestPrepared.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public function notify(Event\Test\Prepared $event): void
4747
throw new \InvalidArgumentException(\sprintf('The test class "%s" must extend "%s" to use the "%s" attribute.', $test->className(), KernelTestCase::class, WithStory::class));
4848
}
4949

50-
FactoriesTraitNotUsed::throwIfClassDoesNotHaveFactoriesTrait($test->className());
50+
if (!FoundryExtension::isEnabled()) {
51+
FactoriesTraitNotUsed::throwIfClassDoesNotHaveFactoriesTrait($test->className());
52+
}
5153

5254
foreach ($withStoryAttributes as $withStoryAttribute) {
5355
$withStoryAttribute->newInstance()->story::load();

src/PHPUnit/BootFoundryOnDataProviderMethodCalled.php renamed to src/PHPUnit/DataProvider/BootFoundryOnDataProviderMethodCalled.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Zenstruck\Foundry\PHPUnit;
14+
namespace Zenstruck\Foundry\PHPUnit\DataProvider;
1515

1616
use PHPUnit\Event;
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1919
use Zenstruck\Foundry\Configuration;
2020
use Zenstruck\Foundry\InMemory\AsInMemoryTest;
21+
use Zenstruck\Foundry\Persistence\PersistentObjectFromDataProviderRegistry;
22+
use Zenstruck\Foundry\PHPUnit\KernelTestCaseHelper;
2123
use Zenstruck\Foundry\Test\UnitTestConfig;
2224

2325
/**
@@ -30,6 +32,12 @@ public function notify(Event\Test\DataProviderMethodCalled $event): void
3032
{
3133
$this->bootFoundryForDataProvider($event->testMethod()->className());
3234

35+
PersistentObjectFromDataProviderRegistry::instance()->addDataset(
36+
$event->testMethod()->className(),
37+
$event->testMethod()->methodName(),
38+
"{$event->dataProviderMethod()->className()}::{$event->dataProviderMethod()->methodName()}"(...) // @phpstan-ignore callable.nonCallable
39+
);
40+
3341
$testMethod = $event->testMethod();
3442

3543
if (AsInMemoryTest::shouldEnableInMemory($testMethod->className(), $testMethod->methodName())) {

src/PHPUnit/ShutdownFoundryOnDataProviderMethodFinished.php renamed to src/PHPUnit/DataProvider/ShutdownFoundryOnDataProviderMethodFinished.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Zenstruck\Foundry\PHPUnit;
14+
namespace Zenstruck\Foundry\PHPUnit\DataProvider;
1515

1616
use PHPUnit\Event;
1717
use Zenstruck\Foundry\Configuration;
18+
use Zenstruck\Foundry\PHPUnit\KernelTestCaseHelper;
1819

1920
/**
2021
* @internal
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <kevinbond@gmail.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\PHPUnit\DataProvider;
15+
16+
use PHPUnit\Event;
17+
use Zenstruck\Foundry\Persistence\PersistentObjectFromDataProviderRegistry;
18+
19+
/**
20+
* @internal
21+
* @author Nicolas PHILIPPE <nikophil@gmail.com>
22+
*/
23+
final class TriggerDataProviderPersistenceOnTestPrepared implements Event\Test\PreparedSubscriber
24+
{
25+
public function notify(Event\Test\Prepared $event): void
26+
{
27+
$test = $event->test();
28+
29+
if (!$test->isTestMethod()) {
30+
return;
31+
}
32+
/** @var Event\Code\TestMethod $test */
33+
34+
if (!$test->testData()->hasDataFromDataProvider() || $test->metadata()->isDataProvider()->isEmpty()) {
35+
return;
36+
}
37+
38+
PersistentObjectFromDataProviderRegistry::instance()->triggerPersistenceForDataset(
39+
$test->className(),
40+
$test->methodName(),
41+
$test->testData()->dataFromDataProvider()->dataSetName(),
42+
);
43+
}
44+
}

src/PHPUnit/FoundryExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use PHPUnit\Runner;
1818
use PHPUnit\TextUI;
1919
use Zenstruck\Foundry\Configuration;
20+
use Zenstruck\Foundry\PHPUnit\DataProvider\BootFoundryOnDataProviderMethodCalled;
21+
use Zenstruck\Foundry\PHPUnit\DataProvider\ShutdownFoundryOnDataProviderMethodFinished;
22+
use Zenstruck\Foundry\PHPUnit\DataProvider\TriggerDataProviderPersistenceOnTestPrepared;
2023

2124
/**
2225
* @internal
@@ -50,6 +53,7 @@ public function bootstrap(
5053
// those deal with data provider events which can be useful only if PHPUnit >=11.4 is used
5154
$subscribers[] = new BootFoundryOnDataProviderMethodCalled();
5255
$subscribers[] = new ShutdownFoundryOnDataProviderMethodFinished();
56+
$subscribers[] = new TriggerDataProviderPersistenceOnTestPrepared();
5357
}
5458

5559
$facade->registerSubscribers(...$subscribers);

src/Persistence/PersistentObjectFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function create(callable|array $attributes = []): object
238238
&& $this->isPersisting()
239239
&& !$this instanceof PersistentProxyObjectFactory
240240
) {
241-
return ProxyGenerator::wrapFactoryNativeProxy($this, $attributes);
241+
return PersistentObjectFromDataProviderRegistry::instance()->deferObjectCreation($this->with($attributes));
242242
}
243243

244244
$object = parent::create($attributes);

0 commit comments

Comments
 (0)