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.6] Register many observers for a model #23507

Merged
merged 4 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,38 @@ trait HasEvents
protected $observables = [];

/**
* Register an observer with the Model.
* Register observers with the Model.
*
* @param object|string $class
* @param object|string|array $classes
* @return void
*/
public static function observe($class)
public static function observe($classes)
{
$instance = new static;

if (! is_array($classes)) {
$classes = [$classes];
}

foreach ($classes as $class) {
$instance->registerObserver($class);
}
}

/**
* Registers a single observer with the Model.
*
* @param object|string $class
* @return void
*/
private function registerObserver($class)
{
$className = is_string($class) ? $class : get_class($class);

// When registering a model observer, we will spin through the possible events
// and determine if this observer has that method. If it does, we will hook
// it into the model's event system, making it convenient to watch these.
foreach ($instance->getObservableEvents() as $event) {
foreach ($this->getObservableEvents() as $event) {
if (method_exists($class, $event)) {
static::registerModelEvent($event, $className.'@'.$event);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,35 @@ public function testModelObserversCanBeAttachedToModelsWithString()
EloquentModelStub::flushEventListeners();
}

public function testModelObserversCanBeAttachedToModelsThroughAnArray()
{
EloquentModelStub::setEventDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher'));
$events->shouldReceive('listen')->once()->with('eloquent.creating: Illuminate\Tests\Database\EloquentModelStub', 'Illuminate\Tests\Database\EloquentTestObserverStub@creating');
$events->shouldReceive('listen')->once()->with('eloquent.saved: Illuminate\Tests\Database\EloquentModelStub', 'Illuminate\Tests\Database\EloquentTestObserverStub@saved');
$events->shouldReceive('forget');
EloquentModelStub::observe(['Illuminate\Tests\Database\EloquentTestObserverStub']);
EloquentModelStub::flushEventListeners();
}

public function testModelObserversCanBeAttachedToModelsThroughCallingObserveMethodOnlyOnce()
{
EloquentModelStub::setEventDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher'));
$events->shouldReceive('listen')->once()->with('eloquent.creating: Illuminate\Tests\Database\EloquentModelStub', 'Illuminate\Tests\Database\EloquentTestObserverStub@creating');
$events->shouldReceive('listen')->once()->with('eloquent.saved: Illuminate\Tests\Database\EloquentModelStub', 'Illuminate\Tests\Database\EloquentTestObserverStub@saved');

$events->shouldReceive('listen')->once()->with('eloquent.creating: Illuminate\Tests\Database\EloquentModelStub', 'Illuminate\Tests\Database\EloquentTestAnotherObserverStub@creating');
$events->shouldReceive('listen')->once()->with('eloquent.saved: Illuminate\Tests\Database\EloquentModelStub', 'Illuminate\Tests\Database\EloquentTestAnotherObserverStub@saved');

$events->shouldReceive('forget');

EloquentModelStub::observe([
'Illuminate\Tests\Database\EloquentTestObserverStub',
'Illuminate\Tests\Database\EloquentTestAnotherObserverStub',
]);

EloquentModelStub::flushEventListeners();
}

public function testSetObservableEvents()
{
$class = new EloquentModelStub;
Expand Down Expand Up @@ -1719,6 +1748,17 @@ public function saved()
}
}

class EloquentTestAnotherObserverStub
{
public function creating()
{
}

public function saved()
{
}
}

class EloquentModelStub extends Model
{
public $connection;
Expand Down