Codeception / module-doctrine

Next gen Doctrine module for Codeception
MIT License
3 stars 2 forks source link

Flush from the seeInRepository method #14

Open kavw opened 4 years ago

kavw commented 4 years ago

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 commented 4 years ago

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

Offout commented 4 years ago

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 commented 1 year ago

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.