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

Model Factory from Unit test does not trigger model events #428

Closed
denis019 opened this issue May 21, 2018 · 12 comments
Closed

Model Factory from Unit test does not trigger model events #428

denis019 opened this issue May 21, 2018 · 12 comments

Comments

@denis019
Copy link

Calling model factory create in unit test
for example
factory(App\User::class)->create()
Will not trigger Model Events (https://laravel.com/docs/5.6/eloquent#events)

Steps to Reproduce the Problem

Create the following Observers https://laravel.com/docs/5.6/eloquent#observers
In for example User Container add some User factory and call it from unit tests you
will see that model events are not triggered.

Specifications

  • Apiato Version: "apiato/core": "dev-master",

Additional Details

I have tested the same behavior with laravel 5.6 and the events are fired
While debugging this bug in apiato I came to
The custom apiato eventserviceprovider
\Apiato\Core\Abstracts\Events\Providers\EventServiceProvider
If we comment the loading of this provider in
\Apiato\Core\Providers\ApiatoProvider::$serviceProviders
the model event will be triggered.

Maybe the order of provider register should be different, need more time to debug.
Please advice if you already have a solution.

@johannesschobel
Copy link
Contributor

Hey there.. Is this specifically related to Unit-Tests or the model-events in general?

@denis019
Copy link
Author

When you call the model factory from seeder it's working fine,
only bug is when you have factory model ->create in Unit-Tests
So it's specifically related to Unit-Tests

@johannesschobel
Copy link
Contributor

i am not entirely sure,.. but i think, i once read something, that the seeders may "disable" the events.. because usually you want to dump a f*cking lot of data at once.. maybe this can be the issue!?

@denis019
Copy link
Author

denis019 commented May 21, 2018

But with seeders is working fine. when you call seeders directly not from UnitTests

@johannesschobel
Copy link
Contributor

and it works in a regular laravel project? just to cross "phpunit" from the list? ;)

@denis019
Copy link
Author

denis019 commented May 21, 2018

In regular laravel project it works.
The issue is when you run phpunit test and in the test you have factory(User::class)->create();
the model events will not be triggered in this case.

@johannesschobel
Copy link
Contributor

hm, ok.. now i get it - sorry ;)
However, i am currently not able to give any hints where to search for the solution 😞 i am very sorry

@denis019 denis019 reopened this May 21, 2018
@denis019
Copy link
Author

This issue is related with apiato/core, my mistake adding it here

@johannesschobel
Copy link
Contributor

thank you very much for the feedback and your time / effort to further investigate this issue! I will take a look at it shortly 👍

@denis019
Copy link
Author

@johannesschobel I have create the pull request on apiato/core which will solve this issue.
Please check apiato/core#100

@johannesschobel
Copy link
Contributor

î've seen it.. will take a closer look later and then merge.. thanks a lot..

@johannesschobel
Copy link
Contributor

i will close the issue here, as we have another issue in core.. thanks for reporting this issue!

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

No branches or pull requests

2 participants