Skip to content

Events are not dispatching #903

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

Closed
GeorgeInteli opened this issue Mar 4, 2024 · 11 comments
Closed

Events are not dispatching #903

GeorgeInteli opened this issue Mar 4, 2024 · 11 comments
Assignees
Labels
question Further information is requested

Comments

@GeorgeInteli
Copy link

Describe the bug
The events from the package are not dispatching.

To Reproduce
Steps to reproduce the behavior:

  1. Create user with wallet
  2. Deposit amount on wallet
  3. Expect to receive Balance Updated event
  4. No event received

Expected behavior
After a deposit I expect to receive event on Balance change and Transaction created.

Screenshots
Here is sample test
image

Server:

  • php version: 8.3
  • database: mysql 8.0
  • wallet version 10
  • cache lock: array
  • cache wallets: array
@GeorgeInteli GeorgeInteli added bug Something isn't working question Further information is requested labels Mar 4, 2024
@rez1dent3
Copy link
Member

@GeorgeInteli Hello. All events are sent when a transaction is committed. Add a check to your transaction level test, it must be 0 for the event to be sent.

I'm sure the test will fail, because... pest starts a transaction before the test starts and ends it after the test ends.

You need to call commit yourself before checking.

@rez1dent3 rez1dent3 added the help wanted Extra attention is needed label Mar 4, 2024
@GeorgeInteli
Copy link
Author

Hello @rez1dent3 , I can confirm that even on transaction level 0 the events are not dispaching.
It was level 1 inside pest, it is going to level 0 after commit, but the test still fails. The most confusing thing about all this is that balanceInt is actually being updated, but the event is not fired.

Note that even if the events did fire i dont think reverting or commiting something outside the scope of the function is a solution. I am sure this was discussed before. Any best practices or solutions you might recommend? And what is the reasoning behind transaction level 0. I see it hard coded inside TransactionCommittedListener.php (i read #412 and #455)

@rez1dent3
Copy link
Member

@GeorgeInteli The laravel architecture is such that if there is no listener, then the event is not sent.

Here you can see the tests of the package:

public function testBalanceUpdatedThrowDateListener(): void
{
$this->app?->bind(ClockServiceInterface::class, ClockFakeService::class);
Event::listen(BalanceUpdatedEventInterface::class, BalanceUpdatedThrowDateListener::class);
/** @var Buyer $buyer */
$buyer = BuyerFactory::new()->create();
self::assertSame(0, $buyer->wallet->balanceInt);
// unit
$this->expectException(UnknownEventException::class);
$this->expectExceptionCode(789);
$buyer->deposit(789);
}

https://github.com/bavix/laravel-wallet/blob/master/tests/Units/Domain/EventTest.php

@rez1dent3
Copy link
Member

And what is the reasoning behind transaction level 0

The transaction can be canceled and the event sent.

@rez1dent3 rez1dent3 removed the bug Something isn't working label Mar 5, 2024
@rez1dent3
Copy link
Member

I checked locally again. Events are sent. Need more data.

@rez1dent3
Copy link
Member

@GeorgeInteli I didn't notice at first, but you're testing the implementation, not the interface. You need to check the contract itself, the trigger for it.

@GeorgeInteli
Copy link
Author

image

Hmmm i got nothing. Not seeing the event at all inside the dispatcher events stack.

I am seeing however our own Laravel defined events. There is something specific to this package events.

@rez1dent3
Copy link
Member

As I already wrote above, you need to determine the listener.

@rez1dent3 rez1dent3 removed the help wanted Extra attention is needed label Mar 5, 2024
@rez1dent3
Copy link
Member

@GeorgeInteli Hello. I took some time and started debugging. The flush method is not implemented for Fake in laravel. You will not be able to implement your check.

https://github.com/laravel/framework/blob/0525a8819db53bc66c878413829a4368d63c3d47/src/Illuminate/Support/Testing/Fakes/EventFake.php#L278-L287

@GeorgeInteli
Copy link
Author

Thank you @rez1dent3
Now we know what the issue is :) We will just have incomplete test for the moment.

@rez1dent3
Copy link
Member

rez1dent3 commented Mar 7, 2024

@GeorgeInteli You can use it in a similar way to the tests in the package itself. Those,

  • refuse Event::fake
  • register listeners
  • throw errors at the listeners as a sign that the listener has worked

this will give you the ability to write tests.

Look at the tests in the project:
https://github.com/bavix/laravel-wallet/blob/master/tests/Units/Domain/EventTest.php

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants