Skip to content

Commit

Permalink
fix 'loadAggregate' unwanted event dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Jun 7, 2024
1 parent ae0bc31 commit 99d8828
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function loadAggregate($relations, $column, $function = null)
$models = $this->first()->newModelQuery()
->whereKey($this->modelKeys())
->select($this->first()->getKeyName())
->withAggregate($relations, $column, $function)
->get()
->withAggregate($relations, $column, $function);
$models = Model::withoutEvents(fn () => $models->get())
->keyBy($this->first()->getKeyName());

$attributes = Arr::except(
Expand Down
20 changes: 20 additions & 0 deletions tests/Integration/Database/EloquentCollectionLoadCountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public function testLoadCountWithSameModels()
$this->assertSame('2', (string) $posts[2]->comments_count);
}

public function testLoadCountDoesNotFireAnyModelRelatedEvents()
{
// arrange:
$posts = Post::all()->push(Post::first());

$_SERVER['-'] = [];

$listener = function($model) {
$_SERVER['-'][] = $model;
};

Post::getEventDispatcher()->listen('eloquent.*: '.Post::class, $listener);
// act:
$posts->loadCount('comments');

// assert:
$this->assertEquals([], $_SERVER['-']);
unset($_SERVER['-']);
}

public function testLoadCountOnDeletedModels()
{
$posts = Post::all()->each->delete();
Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/Database/EloquentModelLoadMaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public function testLoadMaxSingleRelation()
$this->assertEquals(11, $model->related1_max_number);
}

public function testLoadMaxDoesNotFireEvent()
{
$model = BaseModel::first();

$_SERVER['-'] = [];
$listener = function($model) {
$_SERVER['-'][] = $model;
};
BaseModel::getEventDispatcher()->listen('eloquent.*: '.BaseModel::class, $listener);
// act:
$model->loadMax('related1', 'number');

// assert:
$this->assertEquals([], $_SERVER['-']);
unset($_SERVER['-']);
}

public function testLoadMaxMultipleRelations()
{
$model = BaseModel::first();
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/Database/EloquentModelLoadMinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ public function testLoadMinSingleRelation()
$this->assertEquals(10, $model->related1_min_number);
}

public function testLoadMinDoesNotFireEvent()
{
$model = BaseModel::first();

$_SERVER['-'] = [];
$listener = function($model) {
$_SERVER['-'][] = $model;
};
BaseModel::getEventDispatcher()->listen('eloquent.*: '.BaseModel::class, $listener);

// act:
$model->loadMin('related1', 'number');

// assert:
$this->assertEquals([], $_SERVER['-']);
unset($_SERVER['-']);
}

public function testLoadMinMultipleRelations()
{
$model = BaseModel::first();
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/Database/EloquentModelLoadSumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public function testLoadSumSingleRelation()
$this->assertEquals(21, $model->related1_sum_number);
}

public function testLoadSumDoesNotFireEvent()
{
$model = BaseModel::first();

$_SERVER['-'] = [];
$listener = function($model) {
$_SERVER['-'][] = $model;
};
BaseModel::getEventDispatcher()->listen('eloquent.*: '.BaseModel::class, $listener);

// act:
$model->loadSum('related1', 'number');

// assert:
$this->assertEquals([], $_SERVER['-']);
unset($_SERVER['-']);
}

public function testLoadSumMultipleRelations()
{
$model = BaseModel::first();
Expand Down

0 comments on commit 99d8828

Please sign in to comment.