Skip to content

Commit 11e17e4

Browse files
committed
chore: update docs + rector set
1 parent fef57a3 commit 11e17e4

File tree

9 files changed

+127
-24
lines changed

9 files changed

+127
-24
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
@@ -1606,28 +1606,46 @@ Let's look at an example:
16061606

16071607
.. _enable-foundry-in-your-testcase:
16081608

1609-
Enable Foundry in your TestCase
1610-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1609+
Globally Enable Foundry In PHPUnit
1610+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16111611

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

1614-
::
1614+
.. configuration-block::
16151615

1616-
use App\Factory\PostFactory;
1617-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1618-
use Zenstruck\Foundry\Test\Factories;
1616+
.. code-block:: xml
16191617
1620-
class MyTest extends WebTestCase
1621-
{
1622-
use Factories;
1618+
<phpunit>
1619+
<extensions>
1620+
<bootstrap class="Zenstruck\Foundry\PHPUnit\FoundryExtension"/>
1621+
</extensions>
1622+
</phpunit>
1623+
1624+
.. versionadded:: 2.8
16231625

1624-
public function test_1(): void
1626+
The ability to globally enable Foundry with PHPUnit extension was introduced in Foundry 2.8 and requires at least
1627+
PHPUnit 10.
1628+
1629+
.. note::
1630+
1631+
If you're still using PHPUnit 9, Foundry can be enabled by adding the trait ``Zenstruck\Foundry\Test\Factories``
1632+
in each test::
1633+
1634+
use App\Factory\PostFactory;
1635+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1636+
use Zenstruck\Foundry\Test\Factories;
1637+
1638+
class MyTest extends WebTestCase
16251639
{
1626-
$post = PostFactory::createOne();
1640+
use Factories;
16271641

1628-
// ...
1642+
public function test_something(): void
1643+
{
1644+
$post = PostFactory::createOne();
1645+
1646+
// ...
1647+
}
16291648
}
1630-
}
16311649

16321650
Database Reset
16331651
~~~~~~~~~~~~~~
@@ -1780,7 +1798,7 @@ Foundry provides a mechanism to automatically refresh inside a functional test t
17801798

17811799
class MyTest extends WebTestCase
17821800
{
1783-
use Factories, ResetDatabase;
1801+
use ResetDatabase;
17841802

17851803
public function test_with_autorefresh(): void
17861804
{
@@ -2378,8 +2396,6 @@ any bundle configuration you have will not be picked up.
23782396

23792397
class MyUnitTest extends TestCase
23802398
{
2381-
use Factories;
2382-
23832399
public function some_test(): void
23842400
{
23852401
$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/Test/Factories.php

Lines changed: 1 addition & 2 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
}
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+
};

utils/rector/src/FoundrySetList.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
final class FoundrySetList
1515
{
16+
/**
17+
* @deprecated use FoundrySetList::FOUNDRY_2_7
18+
* @var string
19+
*/
20+
public const REMOVE_PROXIES = self::FOUNDRY_2_7;
21+
22+
/** @var string */
23+
public const FOUNDRY_2_7 = __DIR__.'/../config/foundry-2.7.php';
24+
1625
/** @var string */
17-
public const REMOVE_PROXIES = __DIR__.'/../config/foundry-set.php';
26+
public const FOUNDRY_2_8 = __DIR__.'/../config/foundry-2.8.php';
1827
}

utils/rector/tests/AllRules/AllRulesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public static function provideData(): \Iterator
2424

2525
public function provideConfigFilePath(): string
2626
{
27-
return __DIR__.'/../../config/foundry-set.php';
27+
return __DIR__.'/../../config/foundry-2.7.php';
2828
}
2929
}

0 commit comments

Comments
 (0)