framework: hasOneThrough failing during numerous model testing on MySQL

  • Laravel Version: 5.8.19
  • PHP Version: 7.3.2
  • Database Driver & Version: MySQL 8.0.15

Description:

I’m having a very strange issue wherein testing a hasOneThrough relationship is passing when I run the test individually, but when I run it as a suite of other tests it fails. To be clear, the suite is a bunch of unit tests that for model classes. Each test class uses the RefreshDatabase trait, so no two tests should affect one another.

Steps To Reproduce:

Here is my test:

    public function testShouldRetrieveCampaign()
    {
        $user     = factory(User::class)->create();
        $team     = factory(Team::class)->create(['owner_id' => $user->id]);
        $campaign = factory(Campaign::class)->create(['team_id' => $team->id]);
        $donation = factory(Donation::class)->create(['user_id' => $user->id, 'campaign_id' => $campaign->id]);
        $comment  = factory(DonationComment::class)->create(['donation_id' => $donation->id]);

        $this->assertTrue($comment->donation->campaign->is($campaign)); // works
        $this->assertTrue($comment->campaign->is($campaign)); // fails
    }

Couple very important things to note here:

  • When the test is run individually it passes; it only fails when I run all the model tests.
  • See the “works” and “fails” lines in the test. The “works” line works every time and is simply retrieving the middle model explicitly. The “fails” line is using a hasOneThrough eloquent relationship is the proper test. This is critical as it means that it’s the hasOneThrough that’s the issue.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

@JasonTheAdams just a small fyi: if you ever run in a situation again where you need to create a repo to show a bug try to commit the skeleton first and then all your changes which reproduce the bug. That really helps to find out what’s amis.