Skip to content

User from Security service is not available in Doctrine Listeners #34

Open
@BastienLedon

Description

@BastienLedon

Hello,

When trying to test a part of our code with an Event Listener like:

<?php
namespace App\EventListener\Doctrine;

use Symfony\Component\Security\Core\Security;

class Listener
{
    private Security $security;
    public function __construct(Security $security)
    {
        $this->security = $security;
    }

    public function prePersist(Entity $entity)
    {
        $user = $this->security->getUser();
        $entity->setUser($user);
    }
}

$user is always null

I found this SO question with the exact same problem:
https://stackoverflow.com/questions/57609821/make-security-available-for-doctrine-onflush-within-functional-test

Do you recommend using the workaround given in the SO answers or something else ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions