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

Flush from the seeInRepository method #14

Open
kavw opened this issue May 25, 2020 · 3 comments
Open

Flush from the seeInRepository method #14

kavw opened this issue May 25, 2020 · 3 comments

Comments

@kavw
Copy link

kavw commented May 25, 2020

Hello. First of all, thank you for the great testing framework.
I would like to clarify how to use the seeInRepository method. I have read the documentation and can't understand why this method executes the doctrine's flush. I had a few cases when I had forgotten to add the flush in the app and had got green tests because of this behavior. Maybe the flush from this method is not a good idea?

@Offout
Copy link

Offout commented May 27, 2020

I run into this too. No flush in my code -> my code is broken on production, but tests runs OK.

@Offout
Copy link

Offout commented May 27, 2020

I made a workaround.
tests/_support/Module/Doctrine2.php:

<?php

declare(strict_types=1);

namespace App\Tests\Module;

use Codeception\Module;

class Doctrine2 extends Module\Doctrine2
{
    // we don't need to run em->flush(), we need to test that our code has it's own flush() call
    protected function proceedSeeInRepository($entity, $params = [])
    {
        $data = $this->em->getClassMetadata($entity);
        $qb = $this->em->getRepository($entity)->createQueryBuilder('s');
        $this->buildAssociationQuery($qb, $entity, 's', $params);
        $this->debug($qb->getDQL());
        $res = $qb->getQuery()->getArrayResult();

        return ['True', (count($res) > 0), "$entity with " . json_encode($params)];
    }
}

tests/functional.suite.yml:

 - \App\Tests\Module\Doctrine2:
        depends: Symfony
        cleanup: true

@ThomasLandauer
Copy link
Member

Because of this flush() problem, I added a recommendation for Symfony users to use the "built-in" repository, instead of this module's grab...() methods: https://codeception.com/docs/modules/Doctrine2#Grabbing-Entities-with-Symfony
This is (in essence) the same as @Offout suggested above - at least a step forward for most users...
So I think this issue can be closed.

@TavoNiievez TavoNiievez transferred this issue from Codeception/module-doctrine2 Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants