-
Notifications
You must be signed in to change notification settings - Fork 24
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
User from Security service is not available in Doctrine Listeners #34
Comments
Hey, @BastienLedon I don't think what those answers suggest is necessary. However, there are several things that should be verified, the best way i could help you is in a real application. You can choose a branch for the version of symfony you are using, and from there we can see what the real problem is. |
@TavoNiievez I've added one other user and just added the amLoggedAs inside those test. (And created a fake Listener) You can see that inside the controller the user of Security is available but not inside the Listener. |
Yep, i can confirm that there is unexpected behavior here. The Security object requires the symfony container as a parameter. In fact, if you manually inject the test container everything works correctly: public function prePersist(LifecycleEventArgs $entity)
{
$user = $entity->getObject();
if($user instanceof User) {
/** @var ContainerInterface $testContainer */
$testContainer = $this->container->get('test.service_container');
$security = new Security($testContainer);
$user->setUser($security->getUser());
}
} I'm still not sure if it's Codeception behavior or Symfony itself. But at least there should be documentation on this. Lastly, I added an Entity Listener to the test project to be able to test this easily in the future. |
In theory, a workaround would be to inject the correct service into the env-specific services config file: |
Any news on this? |
Ok. Let's say there is a service. <?php
namespace App\Service;
use App\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
class LocatorConsumerService implements ServiceSubscriberInterface
{
/**
* @var ContainerInterface
*/
private $locator;
public function __construct(ContainerInterface $locator)
{
$this->locator = $locator;
}
public function getUserFromLocator(): ?User
{
$token = $this->locator->get('security.token_storage')->getToken();
$user = null;
if($token) {
$user = $token->getUser();
}
return $user;
}
public static function getSubscribedServices(): array
{
return [
'security.token_storage' => TokenStorageInterface::class
];
}
} If service is injected into controller, user is logged in: public function my(LocatorConsumerService $service): Response
{
// ...
$user = $service->getUserFromLocator(); // $user is not null
// ...
} If service injected into any doctrine subscriber - <?php
namespace App\EventSubscriber;
use App\Service\LocatorConsumerService;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
class MySubscriber implements EventSubscriber
{
/**
* @var LocatorConsumerService
*/
private $service;
public function __construct(LocatorConsumerService $service) {
$this->service = $service;
}
public function postLoad(LifecycleEventArgs $lifecycleEventArgs): void
{
// ...
$user = $this->service->getUserFromLocator(); // $user is null here
// ...
}
public function getSubscribedEvents(): array
{
return [
Events::postLoad
];
}
} However, if I configure App\Service\LocatorConsumerService:
arguments:
- '@test.service_container'
I can't figure out which service I have to re-declare this way though. And I'm not sure if such workaround can help at all |
|
same problem. no solution? |
Hello services_test.yaml
|
Hello,
When trying to test a part of our code with an Event Listener like:
$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 ?
The text was updated successfully, but these errors were encountered: