Skip to content

Commit

Permalink
Merge branch 'master' into l11-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykenedy authored Oct 3, 2024
2 parents 560d004 + 56eb5cb commit b8f55cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jeremykenedy/laravel-roles",
"description": "A Powerful package for handling roles and permissions in Laravel. Supports Laravel 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6.0, 7.0, 8.0 and 9.0.",
"description": "A Powerful package for handling roles and permissions in Laravel. Supports Laravel 5.3 up to 11.",
"keywords": [
"laravel-roles",
"laravel-permissions",
Expand All @@ -24,16 +24,16 @@
}
],
"require": {
"php": "^7.2|^8.0.2|^8.1",
"php": "^7.2|^8.0|^8.1|^8.2|^8.3",
"laravel/framework": "5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|^9.0|^10.0|^11.0",
"eklundkristoffer/seedster": "^7.0",
"laravel/helpers": "^1.5"
"eklundkristoffer/seedster": "^8.0",
"laravel/helpers": "^1.7"
},
"require-dev": {
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
"laravel/tinker": "^2.7",
"laravel/tinker": "^2.9",
"illuminate/support": "^8.5|^9.0|^10.0|^11.0",
"laravel/laravel": "^8.0|^9.0|^10.0"
"laravel/laravel": "^8.0|^9.0|^10.0|^11.0"
},
"autoload": {
"psr-4": {
Expand Down
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 b8f55cb

Please sign in to comment.