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

tests: automatically create cascade persist permutations #666

Conversation

nikophil
Copy link
Member

@nikophil nikophil commented Jul 3, 2024

fixes #597

This PR introduces a way to automatically create all permutations for "cascade persist" relationships.

By using a combination of the trait WithEntityRelationShip, and the attribute UsingRelationShips, a data provider is automatically created with all possible combinations of the "cascade persist" and "standard" associations.

ex:

final class EntityRelationshipTest extends KernelTestCase
{
    use WithEntityRelationShip, Factories, ResetDatabase;

    // a matrix with all relationships will be created. Here, for example, there will be 4 permutations:
    // [`Contact::$category` => "cascade", `Category::$contacts` => "cascade"]
    // [`Contact::$category` => "standard", `Category::$contacts` => "cascade"]
    // [`Contact::$category` => "cascade", `Category::$contacts` => "standard"]
    // [`Contact::$category` => "standard", `Category::$contacts` => "standard"]
    #[UsingRelationShips(Contact::class, ['category'])]
    #[UsingRelationShips(Category::class, ['contacts'])]
    /**
     * @test
     * @dataProvider provideCascadeRelationshipsCombination
     */
    public function many_to_one(): void
    {
        // add some test which used `Contact::$category` and `Category::$contacts`
    }
}

Here are some thoughts about this work:

  • We must add the data provider on each method using the attribute UsingRelationShips. I don't think it is possible to automatically add it, but I've added a check which enforces to add the provider if the attribute is used
  • It is currently not possible to have another data provider. But for now, the tests about "orm relationships" do not use data providers, so it's good enough
  • I do think we should test ALL these permutations (I mean, the full matrix, with full cases), we may find some weird edge cases
  • sadly, I had to introduce a (really tiny) PHPUnit extension for this kind of test: it is used to be able to access current method name within the data provider. That's the only way I've found to do it

@nikophil nikophil marked this pull request as draft July 3, 2024 17:38
@nikophil nikophil force-pushed the tests/automatic-test-of-cascade-persist-combination branch 10 times, most recently from 38087c3 to c157428 Compare December 12, 2024 17:19
@nikophil nikophil force-pushed the tests/automatic-test-of-cascade-persist-combination branch 8 times, most recently from ed7f3da to c7dc0ad Compare December 14, 2024 15:53
if (!\getenv('DATABASE_URL')) {
// this test requires the ORM, but trait RequiresORM is analysed after data provider are called
// then we need to return at least one empty array to avoid an error
// in PHPUnit 12, we will be able to use #[RequiresEnvironmentVariable('DATABASE_URL')] to prevent this
Copy link
Member Author

Choose a reason for hiding this comment

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

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [ 8.1, 8.2, 8.3, 8.4 ]
symfony: [ 6.4.*, 7.1.*, 7.2.* ]
database: [ mysql, mongo ]
phpunit: [ 9, 11 ]
Copy link
Member Author

Choose a reason for hiding this comment

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

since all relationships combinations are ran only with PHPUnit 11, I'd rather having this version to be first class citizen of the CI

@nikophil nikophil marked this pull request as ready for review December 14, 2024 16:01
@nikophil nikophil requested a review from kbond December 14, 2024 16:53
@nikophil nikophil force-pushed the tests/automatic-test-of-cascade-persist-combination branch from c7dc0ad to c56a991 Compare December 14, 2024 17:09
@nikophil nikophil force-pushed the tests/automatic-test-of-cascade-persist-combination branch from c56a991 to 0703dfd Compare December 14, 2024 18:24
@nikophil nikophil force-pushed the tests/automatic-test-of-cascade-persist-combination branch from 0703dfd to 7433984 Compare December 14, 2024 21:11
@nikophil nikophil merged commit 23b4ec4 into zenstruck:2.x Dec 14, 2024
71 checks passed
@nikophil nikophil deleted the tests/automatic-test-of-cascade-persist-combination branch December 14, 2024 21:17
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.

[2.x] Automatically test all "cascade" combinations in tests
2 participants