Skip to content

Commit

Permalink
Merge pull request #206 from D0nVitalio/master
Browse files Browse the repository at this point in the history
Fix tests when $this->call(UsersTableSeeder::class) runs
  • Loading branch information
jeremykenedy authored Apr 11, 2024
2 parents 986430a + 55190e8 commit 56eb5cb
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions tests/Feature/NPlusOneQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ class NPlusOneQueriesTest extends TestCase
protected $seed = true;

protected $usersCount = 10;
protected $rolesCount = 3;
protected $permissionsCount = 4;
/**
* @var int in case UsersTableSeeder seeds your users,
* please indicate their number here
*/
protected $usersCountCorrection = 0;
protected $rolesCount = 3; //correct according to your data
protected $permissionsCount = 4; //correct according to your data

protected $queries = 0;

Expand All @@ -27,7 +32,7 @@ protected function setUp(): void
$this->assertEquals($this->rolesCount, config('roles.models.role')::count());
$this->assertEquals($this->permissionsCount, config('roles.models.permission')::count());

DB::listen(function (QueryExecuted $query) {
DB::listen(function(QueryExecuted $query) {
$this->queries++;
});
}
Expand All @@ -38,27 +43,28 @@ public function canPreloadRolesOnCollection(): void
$roleIds = config('roles.models.role')::pluck('id');

User::factory($this->usersCount)->create()
->each(function (User $user) use ($roleIds) {
->each(function(User $user) use ($roleIds) {
$user->roles()->attach($roleIds);
});
$this->assertEquals($this->usersCount, User::count());
$this->assertEquals($this->usersCount, User::count() - $this->usersCountCorrection);

$this->queries = 0;

// without eager load
$users = User::get();
$this->assertQueries(1);

$users->each(function (User $user) {
$users->each(function(User $user) {
$user->getRoles();
});
$this->queries = $this->queries - $this->usersCountCorrection;
$this->assertQueries($this->usersCount);

// with eager load
$users = User::with('roles')->get();
$this->assertQueries(2);

$users->each(function (User $user) {
$users->each(function(User $user) {
$user->getRoles();
});
$this->assertQueries(0);
Expand Down Expand Up @@ -136,33 +142,33 @@ public function canPreloadPermissionsOnCollection(): void
$roleIds = config('roles.models.role')::pluck('id');

User::factory($this->usersCount)->create()
->each(function (User $user) use ($roleIds) {
->each(function(User $user) use ($roleIds) {
$user->roles()->attach($roleIds);
});
$this->assertEquals($this->usersCount, User::count());
$this->assertEquals($this->usersCount, User::count() - $this->usersCountCorrection);

$this->queries = 0;

// without eager load
$users = User::get();
$this->assertQueries(1);

$users->each(function (User $user) {
$users->each(function(User $user) {
$user->getPermissions();
});
// rolePermissions(+getRoles) + userPermissions
$this->assertQueries($this->usersCount * 3);
$this->assertQueries(($this->usersCount + $this->usersCountCorrection) * 3);

// with eager load
// TODO: 'rolePermissions' relation
$users = User::with('roles', 'userPermissions')->get();
$this->assertQueries(3);

$users->each(function (User $user) {
$users->each(function(User $user) {
$user->getPermissions();
});
// TODO: optimize via relations: userPermissions and rolePermissions
$this->assertQueries(20);
$this->assertQueries(20 + $this->usersCountCorrection * 2);
// $this->assertQueries(0);
}

Expand Down

0 comments on commit 56eb5cb

Please sign in to comment.