How to prevent Model events from firing using phpunit? How to prevent Model events from firing using phpunit? laravel laravel

How to prevent Model events from firing using phpunit?


Looking into Laravel Api Model::flushEventListeners() should "Remove all of the event listeners for the model."

EDIT

You can write a custom method base on this one:

public static function flushEventListeners(){    if (! isset(static::$dispatcher)) {        return;    }    $instance = new static;    foreach ($instance->getObservableEvents() as $event) {        static::$dispatcher->forget("eloquent.{$event}: ".get_called_class());    }}

Something like this maybe:

public static function flushEventListenersProvided($events){    if (! isset(static::$dispatcher)) {        return;    }    $instance = new static;    foreach ($instance->getObservableEvents() as $event) {        if(in_array($event, $events)){          static::$dispatcher->forget("eloquent.{$event}: ".get_called_class());        }    }}

and maybe add it as a trait to the models, or a base model that all your models will extend. This code is untested but should give you an idea of how it can be done.


If you really just want to disable the model's observer, such as creating, created, updated, etc. then you can call the model's façade method unsetEventDispatcher.

For example, you can place it in the test class's setUp method. And you don't have to reset it in tearDown because the framework automatically does this in setUp for the next test.

protected function setUp(): void{    parent::setUp();    App\MyModel::unsetEventDispatcher();}

If you want this to be temporary, then look into withoutEventDispatcher.

Also you could getEventDispatcher and save that to a temporary variable then setEventDispatcher again after you've completed your business as is demonstrated in this answer: https://stackoverflow.com/a/51301753/4233593


Method 1:

You can prevent model events from firing by mocking the Observer. Here's how:

public function my_sexy_test()    {        $this->app->bind(MyModelObserver::class, function () {            return $this->getMockBuilder(MyModelObserver::class)->disableOriginalConstructor()->getMock();        });        factory(MyModel::class)->states('pending')->create([                'user_id' => 1,                'publisher_revenue' => 10,        ]); // Your test logic goes here}

Method 2:

You can call the following in your setUp() method:

 MyModel::unsetEventDispatcher();

Both Tested and working in Laravel 5.6.