Skip to content

Commit b0c4b6f

Browse files
committed
chore: update docs + rector set
1 parent be8f629 commit b0c4b6f

File tree

12 files changed

+131
-33
lines changed

12 files changed

+131
-33
lines changed

UPGRADE-2.7.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ return RectorConfig::configure()
5252
'src',
5353
'tests'
5454
])
55-
->withSets([FoundrySetList::REMOVE_PROXIES])
55+
->withSets([FoundrySetList::FOUNDRY_2_7])
5656
;
5757
```
5858

UPGRADE-2.8.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Migration guide from Foundry 2.7 to 2.8
2+
3+
The main feature of Foundry 2.8 is the deprecation of the `Factories` trait, in favor of the [PHPUnit extension](https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#phpunit-extension)
4+
shipped by Foundry. It was necessary to remember to add the trait in every test class. And in some cases, Foundry could
5+
still work even if the trait wasn’t added to the test, which could lead to subtle bugs. Now, Foundry is globally enabled
6+
once for all.
7+
8+
The trait will be removed in Foundry 3.0, and the extension will be mandatory.
9+
10+
> [!WARNING]
11+
> The PHPUnit extension mechanism was introduced in PHPUnit 10. This means that Foundry 3 won't be compatible
12+
> with PHPUnit 9 anymore (but Foundry 2 will remain compatible with PHPUnit 9).
13+
14+
## How to
15+
16+
> [!IMPORTANT]
17+
> If you're still not using PHPUnit 10 or grater, there is nothing to do (yet!)
18+
19+
Enable Foundry's [PHPUnit extension](https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#phpunit-extension)
20+
in your `phpunit.xml` file:
21+
22+
```xml
23+
<phpunit>
24+
<extensions>
25+
<bootstrap class="Zenstruck\Foundry\PHPUnit\FoundryExtension"/>
26+
</extensions>
27+
</phpunit>
28+
```
29+
30+
And then, remove all the `use Factories;` statements from your factories.
31+
32+
## Rector rules
33+
34+
A Rector set is available to automatically remove the usage of the trait in all your tests.
35+
36+
First, you'll need to install `rector/rector`:
37+
```shell
38+
composer require --dev rector/rector
39+
```
40+
41+
Then, create a `rector.php` file:
42+
43+
```php
44+
<?php
45+
46+
use Rector\Config\RectorConfig;
47+
use Zenstruck\Foundry\Utils\Rector\FoundrySetList;
48+
49+
return RectorConfig::configure()
50+
->withPaths(['tests'])
51+
->withSets([FoundrySetList::FOUNDRY_2_8])
52+
;
53+
```

docs/index.rst

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,28 +1621,46 @@ Let's look at an example:
16211621

16221622
.. _enable-foundry-in-your-testcase:
16231623

1624-
Enable Foundry in your TestCase
1625-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1624+
Globally Enable Foundry In PHPUnit
1625+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16261626

1627-
Add the ``Factories`` trait for tests using factories:
1627+
Add Foundry's `PHPUnit Extension`_ in your `phpunit.xml` file:
16281628

1629-
::
1629+
.. configuration-block::
16301630

1631-
use App\Factory\PostFactory;
1632-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1633-
use Zenstruck\Foundry\Test\Factories;
1631+
.. code-block:: xml
16341632
1635-
class MyTest extends WebTestCase
1636-
{
1637-
use Factories;
1633+
<phpunit>
1634+
<extensions>
1635+
<bootstrap class="Zenstruck\Foundry\PHPUnit\FoundryExtension"/>
1636+
</extensions>
1637+
</phpunit>
1638+
1639+
.. versionadded:: 2.8
16381640

1639-
public function test_1(): void
1641+
The ability to globally enable Foundry with PHPUnit extension was introduced in Foundry 2.8 and requires at least
1642+
PHPUnit 10.
1643+
1644+
.. note::
1645+
1646+
If you're still using PHPUnit 9, Foundry can be enabled by adding the trait ``Zenstruck\Foundry\Test\Factories``
1647+
in each test::
1648+
1649+
use App\Factory\PostFactory;
1650+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1651+
use Zenstruck\Foundry\Test\Factories;
1652+
1653+
class MyTest extends WebTestCase
16401654
{
1641-
$post = PostFactory::createOne();
1655+
use Factories;
16421656

1643-
// ...
1657+
public function test_something(): void
1658+
{
1659+
$post = PostFactory::createOne();
1660+
1661+
// ...
1662+
}
16441663
}
1645-
}
16461664

16471665
Database Reset
16481666
~~~~~~~~~~~~~~
@@ -1795,7 +1813,7 @@ Foundry provides a mechanism to automatically refresh inside a functional test t
17951813

17961814
class MyTest extends WebTestCase
17971815
{
1798-
use Factories, ResetDatabase;
1816+
use ResetDatabase;
17991817

18001818
public function test_with_autorefresh(): void
18011819
{
@@ -2393,8 +2411,6 @@ any bundle configuration you have will not be picked up.
23932411

23942412
class MyUnitTest extends TestCase
23952413
{
2396-
use Factories;
2397-
23982414
public function some_test(): void
23992415
{
24002416
$post = PostFactory::createOne();

phpunit-deprecation-baseline.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
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.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>
15+
16+
<issue><![CDATA[Since zenstruck/foundry 2.8: 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.8.md to upgrade.]]></issue>
17+
<issue><![CDATA[Since zenstruck/foundry 2.8: 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.8.md to upgrade.]]></issue>
1718
</line>
1819
</file>
1920
<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.8', '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. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.8.md to upgrade.');
141141
}
142142
}
143143

src/PHPUnit/BuildStoryOnTestPrepared.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use PHPUnit\Event;
1717
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1818
use Zenstruck\Foundry\Attribute\WithStory;
19-
use Zenstruck\Foundry\Exception\FactoriesTraitNotUsed;
2019

2120
/**
2221
* @internal
@@ -47,10 +46,6 @@ public function notify(Event\Test\Prepared $event): void
4746
throw new \InvalidArgumentException(\sprintf('The test class "%s" must extend "%s" to use the "%s" attribute.', $test->className(), KernelTestCase::class, WithStory::class));
4847
}
4948

50-
if (!FoundryExtension::isEnabled()) {
51-
FactoriesTraitNotUsed::throwIfClassDoesNotHaveFactoriesTrait($test->className());
52-
}
53-
5449
foreach ($withStoryAttributes as $withStoryAttribute) {
5550
$withStoryAttribute->newInstance()->story::load();
5651
}

src/PHPUnit/FoundryExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ public static function isEnabled(): bool
7474
} else {
7575
final class FoundryExtension
7676
{
77-
public static function shouldBeEnabled(): bool
77+
public static function shouldBeEnabled(): bool // @phpstan-ignore return.tooWideBool
7878
{
7979
return false;
8080
}
8181

82-
public static function isEnabled(): bool
82+
public static function isEnabled(): bool // @phpstan-ignore return.tooWideBool
8383
{
8484
return false;
8585
}

src/Test/Factories.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPUnit\Framework\Attributes\Before;
1616
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1717
use Zenstruck\Foundry\Configuration;
18-
1918
use Zenstruck\Foundry\PHPUnit\FoundryExtension;
2019

2120
use function Zenstruck\Foundry\Persistence\initialize_proxy_object;
@@ -33,7 +32,7 @@ trait Factories
3332
public function _beforeHook(): void
3433
{
3534
if (FoundryExtension::isEnabled()) {
36-
trigger_deprecation('zenstruck/foundry', '2.8', sprintf('Trait %s is deprecated and will be removed in Foundry 3.', Factories::class));
35+
trigger_deprecation('zenstruck/foundry', '2.8', \sprintf('Trait %s is deprecated and will be removed in Foundry 3. See https://github.com/zenstruck/foundry/blob/2.x/UPGRADE-2.8.md to upgrade.', Factories::class));
3736

3837
return;
3938
}
@@ -108,7 +107,7 @@ private function _loadDataProvidedProxies(): void
108107

109108
$providedData = \method_exists($this, 'getProvidedData') // @phpstan-ignore function.impossibleType
110109
? $this->getProvidedData() // @phpstan-ignore method.notFound
111-
: $this->providedData();
110+
: $this->providedData(); // @phpstan-ignore method.internal
112111

113112
initialize_proxy_object($providedData);
114113
}
File renamed without changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 <[email protected]>
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+
use Rector\Config\RectorConfig;
15+
use Rector\Removing\Rector\Class_\RemoveTraitUseRector;
16+
use Zenstruck\Foundry\Test\Factories;
17+
18+
return static function (RectorConfig $rectorConfig): void {
19+
$rectorConfig->ruleWithConfiguration(
20+
RemoveTraitUseRector::class,
21+
[
22+
Factories::class,
23+
]
24+
);
25+
};

0 commit comments

Comments
 (0)