dmaicher/doctrine-test-bundle

[HOW TO] I want to edit an entity after event (on flush) and validate it

AurelienConte opened this issue · 1 comments

I know this is the normal behavior but in some of my tests i want to check an entity updated.

For example i have an Entity update after the onFlush event of doctrine. (See event bellow)

    App\EventSubscriber\DoctrineEntities\MyListener:
        tags:
            - { name: doctrine.event_listener, event: onFlush }
            

On this Event i have a function where i want to remove the child of the parent if the child is set "DISABLED"

/**
     * @param EntityManagerInterface $em
     * @param UnitOfWork $uow
     * @param array $entitiesToPersist
     */
    private function persistor(EntityManagerInterface $em, UnitOfWork $uow, array $entitiesToPersist)
    {
        //In the entity list i have my parent to update
        
        foreach ($entitiesToPersist as $entity) {
            $classMetaData = $em->getClassMetadata(get_class($entity));
            $em->persist($entity);
            if(count($uow->getEntityChangeSet($entity)) > 0) {
                /** If the entity has pending changes, we need to recompute/merge. */
                $uow->recomputeSingleEntityChangeSet($classMetaData, $entity);
            } else {
                /** If there are no changes, we compute from scratch */
                $uow->computeChangeSet($classMetaData, $entity);
            }
        }
    }

And i want to test it like :

    public function testParentLoseChildOnChildDisabled()
    {
        $myParentEntity = $this->getParent();
        $myChildEntity = $myParentEntity->getChild();

        $this->assertNotNull($myParentEntity->getChild());

        $requestBody = [
            "status" => ChildStatusEnum::DISABLED
        ];

        //The side effect is : if the child is disabled, the parent lose his child
        $client = $this->doRequestAs('PATCH', "childs/" . $myChildEntity->getId(), $this->asAdministrator, [], $requestBody);
        $this->assertEquals(200, $client->getResponseStatusCode());

        $updatedParentEntity = $this->parentRepository->find($myParentEntity->getId());
        $this->assertNull($updatedParentEntity->getChild());
    }

But when i'm retrieving the parent via the repository, i got exactly the same entity and i can't verify the change.

Any Help will be usefull, thanks !

I fail to see how this issue is related to this bundle? Your test will behave the same way if the bundle is not enabled, or?