Skip to content

Commit

Permalink
[5.5] flush listeners of custom eloquent events (#21688)
Browse files Browse the repository at this point in the history
*        flush listeners of custom eloquent events

*    fix style
  • Loading branch information
themsaid authored and taylorotwell committed Oct 16, 2017
1 parent 9c9b86c commit 039b565
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ public static function flushEventListeners()
foreach ($instance->getObservableEvents() as $event) {
static::$dispatcher->forget("eloquent.{$event}: ".static::class);
}

foreach (array_values($instance->dispatchesEvents) as $event) {
static::$dispatcher->forget($event);
}
}

/**
Expand Down
72 changes: 72 additions & 0 deletions tests/Integration/Database/EloquentModelCustomEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentModelCustomEventsTest;

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;

/**
* @group integration
*/
class EloquentModelCustomEventsTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

public function setUp()
{
parent::setUp();

Schema::create('test_model1', function ($table) {
$table->increments('id');
});

Event::listen(CustomEvent::class, function () {
$_SERVER['fired_event'] = true;
});
}

public function testFlushListenersClearsCustomEvents()
{
$_SERVER['fired_event'] = false;

TestModel1::flushEventListeners();

$user = TestModel1::create();

$this->assertFalse($_SERVER['fired_event']);
}

public function testCustomEventListenersAreFired()
{
$_SERVER['fired_event'] = false;

$user = TestModel1::create();

$this->assertTrue($_SERVER['fired_event']);
}
}

class TestModel1 extends Model
{
public $dispatchesEvents = ['created' => CustomEvent::class];
public $table = 'test_model1';
public $timestamps = false;
protected $guarded = ['id'];
}

class CustomEvent
{
}

0 comments on commit 039b565

Please sign in to comment.