Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: allow to use Factory::create() and factory service in data providers #648

Merged

Conversation

nikophil
Copy link
Member

@nikophil nikophil commented Jun 21, 2024

fixes #532

Since sebastianbergmann/phpunit#5913, PHPUnit's extensions bootstrap() methods are called before data provider and since sebastianbergmann/phpunit#5917, data provider related events are dispatched synchronously (before this, they were deferred, which wasn't useful for us).

This now opens the door for cool things:

  • we can boot Foundry "for kernel tests" before the data providers are called, allowing to use service factories in data providers
  • we can now detect whether or not we are inside a data provider, and decide to return a proxy instead of actually calling create() in the data provider.

so, after this PR will be merged, all of these will be working:

final class SomeUnitTest extends TestCase
{
    #[DataProvider('provide')]
    public function testSomethingWithoutDB(SomeObject $object): void
    {
        // ...
    }

    public static function provide(): iterable
    {
        yield [SomeObjectFactory::createOne()];
        yield [SomePersistentObjectFactory::createOne()]; // won't try to store anything in db
        yield [SomePersistentProxyObjectFactory::createOne()]; // won't try to store anything in db
    }
}

final class SomeIntegrationTest extends KernelTestCase
{
    #[DataProvider('provide')]
    public function testSomethingWithDB(SomeObject $object): void
    {
        // ...
    }

    public static function provide(): iterable
    {
        yield [SomeProxyObjectPersistentFactory::createOne()];
        yield [SomeProxyObjectPersistentFactoryAsAService::createOne()]; 
    }
}

Since we need to return a proxy object, I decided that trying to call NotAProxyButStillPersistentFactory::create() inside a data provider will throw an error, but I'm open to change this.

Sadly, the feature will land in PHPUnit 11.4, which will be released in October. So, we have plenty of time to test it :)


Beside of this, the newly merged PR in PHPUnit, will allow to use the "in-memory" behavior in data providers, which was my last blocker for this awesome feature.

@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch from ce7b4f7 to 28939c2 Compare June 21, 2024 08:09
@nikophil nikophil changed the title feature/create objects in data providers feature: allow to create objects in dataProvider thanks to lazy proxy Jun 21, 2024
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 2 times, most recently from 3a88b8e to 233d760 Compare June 21, 2024 08:29
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 6 times, most recently from 4456128 to 3a46f03 Compare June 21, 2024 09:17
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 2 times, most recently from 736f045 to 366d37e Compare August 7, 2024 13:01
@nikophil nikophil marked this pull request as draft August 7, 2024 13:03
phpunit-10.xml.dist Outdated Show resolved Hide resolved
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 2 times, most recently from a5291f8 to 630c311 Compare August 7, 2024 13:48
@nikophil nikophil changed the title feature: allow to create objects in dataProvider thanks to lazy proxy feature: allow to use Factory::create() and factory service in data providers Aug 7, 2024
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 3 times, most recently from a1c9816 to 02f172e Compare August 9, 2024 09:25
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 2 times, most recently from d7cc4f0 to 1c0c494 Compare August 9, 2024 10:26
@nikophil nikophil marked this pull request as ready for review August 9, 2024 10:45
@nikophil nikophil requested a review from kbond August 9, 2024 10:46
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 2 times, most recently from 89ece3f to c8b541d Compare August 18, 2024 14:18
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch from c8b541d to 24562e6 Compare August 18, 2024 17:41
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 6 times, most recently from 24cfd90 to bc0013e Compare October 17, 2024 18:13
src/Test/Factories.php Outdated Show resolved Hide resolved
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch from bc0013e to 4fd5ce8 Compare October 18, 2024 09:43
Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice! ❤️

@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch 5 times, most recently from 14ee628 to c475210 Compare October 23, 2024 16:57
@nikophil nikophil force-pushed the feature/create-objects-in-data-providers branch from c475210 to 9572b62 Compare October 24, 2024 09:08
@nikophil nikophil merged commit 2014ed9 into zenstruck:2.x Oct 24, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[Foundry 2] Allow objects creation in data provider
2 participants