Skip to content

Commit

Permalink
Merge pull request #185 from ulcuber/test-relations
Browse files Browse the repository at this point in the history
n+1 tests
  • Loading branch information
jeremykenedy authored Feb 4, 2023
2 parents 6986354 + f2adc1c commit 8c14796
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ packages/
npm-debug.log
yarn-error.log

.phpunit.result.cache
47 changes: 19 additions & 28 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_KEY" value="base64:17D5MHqbPTIPEHCFqyaari5C7wKMoSceYyvwEXorYuI="/>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_KEY" value="base64:17D5MHqbPTIPEHCFqyaari5C7wKMoSceYyvwEXorYuI="/>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
7 changes: 5 additions & 2 deletions src/Database/Seeders/DefaultConnectRelationshipsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ public function run()
$roleAdmin = config('roles.models.role')::where('slug', '=', 'admin')->first()
?? config('roles.models.role')::where('name', '=', 'Admin')->first();

echo "\e[32mSeeding:\e[0m DefaultConnectRelationshipsSeeder\r\n";
$this->command->getOutput()->writeln("<info>Seeding:</info> DefaultConnectRelationshipsSeeder");
foreach ($permissions as $permission) {
$roleAdmin->attachPermission($permission);
echo "\e[32mSeeding:\e[0m DefaultConnectRelationshipsSeeder - Role:Admin attached to Permission:".$permission->slug."\r\n";
$this->command->getOutput()->writeln(
"<info>Seeding:</info> DefaultConnectRelationshipsSeeder - Role:Admin attached to Permission:"
. $permission->slug
);
}
}
}
10 changes: 7 additions & 3 deletions src/Database/Seeders/DefaultPermissionsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ public function run()
* Add Permission Items
*
*/
echo "\e[32mSeeding:\e[0m DefaultPermissionitemsTableSeeder\r\n";
$this->command->getOutput()->writeln("<info>Seeding:</info> DefaultPermissionitemsTableSeeder");
foreach ($Permissionitems as $Permissionitem) {
$newPermissionitem = config('roles.models.permission')::where('slug', '=', $Permissionitem['slug'])->first();
$newPermissionitem = config('roles.models.permission')::where('slug', '=', $Permissionitem['slug'])
->first();
if ($newPermissionitem === null) {
$newPermissionitem = config('roles.models.permission')::create([
'name' => $Permissionitem['name'],
'slug' => $Permissionitem['slug'],
'description' => $Permissionitem['description'],
'model' => $Permissionitem['model'],
]);
echo "\e[32mSeeding:\e[0m DefaultPermissionitemsTableSeeder - Permission:".$Permissionitem['slug']."\r\n";
$this->command->getOutput()->writeln(
"<info>Seeding:</info> DefaultPermissionitemsTableSeeder - Permission:"
. $Permissionitem['slug']
);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Database/Seeders/DefaultRolesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function run()
* Add Role Items
*
*/
echo "\e[32mSeeding:\e[0m DefaultRoleItemsTableSeeder\r\n";
$this->command->getOutput()->writeln("<info>Seeding:</info> DefaultRoleItemsTableSeeder");
foreach ($RoleItems as $RoleItem) {
$newRoleItem = config('roles.models.role')::where('slug', '=', $RoleItem['slug'])->first();
if ($newRoleItem === null) {
Expand All @@ -52,7 +52,9 @@ public function run()
'description' => $RoleItem['description'],
'level' => $RoleItem['level'],
]);
echo "\e[32mSeeding:\e[0m DefaultRoleItemsTableSeeder - Role:".$RoleItem['slug']."\r\n";
$this->command->getOutput()->writeln(
"<info>Seeding:</info> DefaultRoleItemsTableSeeder - Role:" . $RoleItem['slug']
);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/Database/Seeders/DefaultUsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function run()
* Add Users
*
*/
echo "\e[32mSeeding:\e[0m DefaultUsersTableSeeder\r\n";
$this->command->getOutput()->writeln("<info>Seeding:</info> DefaultUsersTableSeeder");

if (config('roles.models.defaultUser')::where('email', '=', '[email protected]')->first() === null) {
$newUser = config('roles.models.defaultUser')::create([
Expand All @@ -34,7 +34,9 @@ public function run()
foreach ($permissions as $permission) {
$newUser->attachPermission($permission);
}
echo "\e[32mSeeding:\e[0m DefaultUsersTableSeeder - User:[email protected]\r\n";
$this->command->getOutput()->writeln(
"<info>Seeding:</info> DefaultUsersTableSeeder - User:[email protected]"
);
}

if (config('roles.models.defaultUser')::where('email', '=', '[email protected]')->first() === null) {
Expand All @@ -45,7 +47,7 @@ public function run()
]);

$newUser->attachRole($userRole);
echo "\e[32mSeeding:\e[0m DefaultUsersTableSeeder - User:[email protected]\r\n";
$this->command->getOutput()->writeln("<info>Seeding:</info> DefaultUsersTableSeeder - User:[email protected]");
}
}
}
38 changes: 38 additions & 0 deletions src/Database/TestMigrations/create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace jeremykenedy\LaravelRoles\Database\TestMigrations;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
47 changes: 25 additions & 22 deletions src/RolesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,32 @@ private function loadMigrations()
*/
private function loadSeedsFrom()
{
if (config('roles.defaultSeeds.PermissionsTableSeeder')) {
$this->app['seed.handler']->register(
DefaultPermissionsTableSeeder::class
);
}

if (config('roles.defaultSeeds.RolesTableSeeder')) {
$this->app['seed.handler']->register(
DefaultRolesTableSeeder::class
);
}

if (config('roles.defaultSeeds.ConnectRelationshipsSeeder')) {
$this->app['seed.handler']->register(
DefaultConnectRelationshipsSeeder::class
);
}
$this->app->afterResolving('seed.handler', function ($handler) {
if (config('roles.defaultSeeds.PermissionsTableSeeder')) {
$handler->register(
DefaultPermissionsTableSeeder::class
);
}

if (config('roles.defaultSeeds.RolesTableSeeder')) {
$handler->register(
DefaultRolesTableSeeder::class
);
}

if (config('roles.defaultSeeds.ConnectRelationshipsSeeder')) {
$handler->register(
DefaultConnectRelationshipsSeeder::class
);
}

if (config('roles.defaultSeeds.UsersTableSeeder')) {
$handler->register(
DefaultUsersTableSeeder::class
);
}
});

if (config('roles.defaultSeeds.UsersTableSeeder')) {
$this->app['seed.handler']->register(
DefaultUsersTableSeeder::class
);
}
}

/**
Expand Down
Loading

0 comments on commit 8c14796

Please sign in to comment.