Skip to content

Commit

Permalink
When DatabaseSeeder runs $this->call(UsersTableSeeder::class) with cu…
Browse files Browse the repository at this point in the history
…stom users count assertions fail.
  • Loading branch information
VitalyEvrica committed Mar 12, 2024
1 parent c8b2a56 commit 406201f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 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 $usersCountCorrection 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 @@ -41,7 +46,8 @@ public function canPreloadRolesOnCollection(): void
->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;

Expand All @@ -52,6 +58,7 @@ public function canPreloadRolesOnCollection(): void
$users->each(function (User $user) {
$user->getRoles();
});
$this->queries = $this->queries - $this->usersCountCorrection;
$this->assertQueries($this->usersCount);

// with eager load
Expand Down Expand Up @@ -139,7 +146,7 @@ public function canPreloadPermissionsOnCollection(): void
->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;

Expand All @@ -151,7 +158,7 @@ public function canPreloadPermissionsOnCollection(): void
$user->getPermissions();
});
// rolePermissions(+getRoles) + userPermissions
$this->assertQueries($this->usersCount * 3);
$this->assertQueries(($this->usersCount + $this->usersCountCorrection) * 3);

// with eager load
// TODO: 'rolePermissions' relation
Expand All @@ -162,7 +169,7 @@ public function canPreloadPermissionsOnCollection(): void
$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 406201f

Please sign in to comment.