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

[5.3] expectsEvents fired from ModelEvents #15052

Merged
merged 3 commits into from
Aug 26, 2016
Merged

[5.3] expectsEvents fired from ModelEvents #15052

merged 3 commits into from
Aug 26, 2016

Conversation

SeoFood
Copy link
Contributor

@SeoFood SeoFood commented Aug 25, 2016

If we Mock the ModelEvents you cant Test anymore when ModelEvents fires an event, because withoutEvents mocks als the ModelEvents.

TestFile:

/**
 * @test
 */
public function it_fires_an_created_event()
{
    $this->expectsEvents(ModelCreated::class);

    factory(Model::class)->create();
}

Model:

/**
 * Register events
 *
 * @return void
 */
protected static function boot()
{
    parent::boot();

    static::created(function ($model) {
        event(new ModelCreated($model));
    });
}

This was ofc. working in Laravel 5.2. Added a Unit Test for better understanding.

event(ExampleEvent::class);
});

$this->expectsEvents(ExampleEvent::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you put that code here before you register that created callback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes no difference because the event will be mocked before the creation and override the ModelEvents.

@thecrypticace
Copy link
Contributor

thecrypticace commented Aug 26, 2016

Oh, good catch @SeoFood.

cc @taylorotwell

You made this change after my PR but it definitely causes unexpected behavior. I get why you did it but it can definitely cause issues like the above.

And it's not just observers that break like I originally thought — firing events from a model event listener breaks too.

(related #14374 (comment))

@taylorotwell taylorotwell merged commit dcfe40f into laravel:5.3 Aug 26, 2016
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

Successfully merging this pull request may close these issues.

4 participants