Skip to content

Commit c376dea

Browse files
committed
chore: update docs + rector set
1 parent 42dfe9b commit c376dea

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
@@ -1683,28 +1683,46 @@ Let's look at an example:
16831683

16841684
.. _enable-foundry-in-your-testcase:
16851685

1686-
Enable Foundry in your TestCase
1687-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1686+
Globally Enable Foundry In PHPUnit
1687+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16881688

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

1691-
::
1691+
.. configuration-block::
16921692

1693-
use App\Factory\PostFactory;
1694-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1695-
use Zenstruck\Foundry\Test\Factories;
1693+
.. code-block:: xml
16961694
1697-
class MyTest extends WebTestCase
1698-
{
1699-
use Factories;
1695+
<phpunit>
1696+
<extensions>
1697+
<bootstrap class="Zenstruck\Foundry\PHPUnit\FoundryExtension"/>
1698+
</extensions>
1699+
</phpunit>
1700+
1701+
.. versionadded:: 2.8
1702+
1703+
The ability to globally enable Foundry with PHPUnit extension was introduced in Foundry 2.8 and requires at least
1704+
PHPUnit 10.
1705+
1706+
.. note::
17001707

1701-
public function test_1(): void
1708+
If you're still using PHPUnit 9, Foundry can be enabled by adding the trait ``Zenstruck\Foundry\Test\Factories``
1709+
in each test::
1710+
1711+
use App\Factory\PostFactory;
1712+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1713+
use Zenstruck\Foundry\Test\Factories;
1714+
1715+
class MyTest extends WebTestCase
17021716
{
1703-
$post = PostFactory::createOne();
1717+
use Factories;
17041718

1705-
// ...
1719+
public function test_something(): void
1720+
{
1721+
$post = PostFactory::createOne();
1722+
1723+
// ...
1724+
}
17061725
}
1707-
}
17081726

17091727
Database Reset
17101728
~~~~~~~~~~~~~~
@@ -1857,7 +1875,7 @@ Foundry provides a mechanism to automatically refresh inside a functional test t
18571875

18581876
class MyTest extends WebTestCase
18591877
{
1860-
use Factories, ResetDatabase;
1878+
use ResetDatabase;
18611879

18621880
public function test_with_autorefresh(): void
18631881
{
@@ -2455,8 +2473,6 @@ any bundle configuration you have will not be picked up.
24552473

24562474
class MyUnitTest extends TestCase
24572475
{
2458-
use Factories;
2459-
24602476
public function some_test(): void
24612477
{
24622478
$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

1819
<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>
1920
</line>

src/Configuration.php

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

151151
if (FoundryExtension::shouldBeEnabled()) {
152-
trigger_deprecation('zenstruck/foundry', '2.8', 'Not using Foundry\'s PHPUnit extension is deprecated and will throw an error in Foundry 3.');
152+
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.');
153153
}
154154
}
155155

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 <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+
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)