dmaicher / doctrine-test-bundle

Symfony bundle to isolate your app's doctrine database tests and improve the test performance
MIT License
1.08k stars 60 forks source link

Entity does not persists on $em->persist($enity){->flush()} inside test case. #305

Closed sergey-belous closed 4 weeks ago

sergey-belous commented 1 month ago

I have a test, where i've persists entity inside a test case. But when i'm inside a controller, which i'm testing, is called by api request, there is no entity changes.

class UserProductionTest extends WebTestCase
{
    public function testProduction(): void
    {
        $client = self::getAuthenticatedClient();

        $user = $this->getCurrentUser(); //#1: existing user with lastLoginAt = null

        $client->jsonRequest(
            "GET",
            "/api/user/online_heartbeat"
        );

        $user = $this->getCurrentUser(); //#2: existing user, with lastLoginAt = 'now'. Changes caused by api call atop.
        $user->setLastLoginAt($user->getLastLoginAt()->modify('-5 minutes'));
        /** @var UserRepository $userRepository */
        $userRepository = $this->getContainer()->get(UserRepository::class);
        $userRepository->save($user, true); //#3: saves changes by repository

        $client->jsonRequest(
            "GET",
            "/api/production/offline_income/en" //There i call controller, which inside i get current user again, but  there is #2 value, not #3
        );

        $user = $this->getCurrentUser();

        $this->assertResponseIsSuccessful();
    }
}

StaticDriver::commit() line after #3 successfully writes changes into DB. I guess there are maybe kind of config issue, or maybe cause is User entity, used in authorization hooks.

dmaicher commented 1 month ago

And this works/passes when you disable this bundle? 🤔

sergey-belous commented 4 weeks ago

Ok, thats not your issue. =\ It works so even bundle is disabled. Thanx anyway!