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

Error sending message to new transport from an EntityListener #81

Open
mbopp opened this issue Apr 17, 2024 · 3 comments
Open

Error sending message to new transport from an EntityListener #81

mbopp opened this issue Apr 17, 2024 · 3 comments

Comments

@mbopp
Copy link

mbopp commented Apr 17, 2024

I'm using API Platform. I have many transports and many tests that work great. But after adding a new transport and implementing a test I am getting an error:

 TypeError: Zenstruck\Messenger\Test\Transport\TestTransport::isIntercepting(): Return value must be of type bool, null returned

Which seems to indicate that the new transport isn't found. Here are some of the details that cause this error ('hubspot_transport' is the name of the one that fails)

messenger.yaml

framework:
    messenger:
        reset_on_message: true
        default_bus: event.bus
        buses:
            event.bus:
                default_middleware: allow_no_handlers
                middleware:
                    - App\Messenger\AuditMiddleware

        failure_transport: failed

        transports:
            async_transport:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
            hubspot_transport:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                retry_strategy:
                    max_retries: 1
            failed: 'doctrine://default?queue_name=failed'

        routing:
            'App\Message\ActivityMessage': async_transport
            'App\Message\HubspotUpdateMessage': hubspot_transport

when@test:
    framework:
        messenger:
            transports:
              async_transport: 'test://'
              hubspot_transport: 'test://'


when@dev:
    framework:
        messenger:
            transports:
              async_transport: '%env(MESSENGER_TRANSPORT_DSN)%'
              hubspot_transport: '%env(MESSENGER_TRANSPORT_DSN)%'

relevant test code:

    $client = self::createClient();

    $messenger = $this->transport('hubspot_transport');

    // Error ONLY occurs when this statement exists.
    $messenger->resetAll(); 

    // This request produces error 
    $client->request('POST', '/api/authentication/token', [
      'headers' => ['Content-Type' => 'application/json'],
      'json' => [
        'username' => $username,
        'password' => $password,
      ],
    ]);

The login request alters a field on the user entity...

In my entity listener:

#[AsEntityListener(event: Events::preUpdate, method: 'preUpdate', entity: User::class)]
class UserListener
{
  public function __construct(
    private readonly MessageBusInterface $messageBus,
  ) {
  }

  public function preUpdate(User $user, PreUpdateEventArgs $event): void
  {
    $hubspotMessage = new HubspotUpdateMessage(HubspotUpdateType::CONTACT, $user->getId(), ['updating user']);
    $this->messageBus->dispatch($hubspotMessage); // <<< Error
  }
...
}

Please let me know if there is more info I can provide to help put a finger on what is happening here. Thank you so much for your help!

@nikophil
Copy link
Member

Hello @mbopp

I think that if you use $messenger->reset() you won't have the problem.

nonetheless, there is a problem with resetAll() method: we're resetting $intercept and $catchExceptions to null, but isIntercepting() and isCatchingExceptions() must return false. @kbond WDYT?

@kbond
Copy link
Member

kbond commented Apr 18, 2024

I think that if you use $messenger->reset() you won't have the problem.

@mbopp, can you confirm this solves the problem?

I don't think resetAll should be used in userland - we should mark as internal. It should only be called in the test trait.

nonetheless, there is a problem with resetAll() method: we're resetting $intercept and $catchExceptions to null, but isIntercepting() and isCatchingExceptions() must return false.

I think the problem is an undefined array key (there's a warning but returns null). We should throw an exception here I think.

@mbopp
Copy link
Author

mbopp commented Apr 18, 2024

Thanks for the quick response!

That fixed it! I'm very appreciative! This is a fantastic utility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants