diff --git a/composer.json b/composer.json
index 04cbfe0..0ae46ec 100644
--- a/composer.json
+++ b/composer.json
@@ -41,7 +41,6 @@
"illuminate/support": "^9.0|^10.0"
},
"require-dev": {
- "laravel/legacy-factories": "^1.3",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^7.0|^8.0",
"phpunit/phpunit": "^9.6"
@@ -54,7 +53,8 @@
},
"autoload-dev": {
"psr-4": {
- "Cog\\Tests\\Laravel\\Ban\\": "tests/"
+ "Cog\\Tests\\Laravel\\Ban\\": "tests/",
+ "Cog\\Tests\\Laravel\\Ban\\Database\\Factories\\": "tests/database/factories"
}
},
"scripts": {
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index f20e9e3..51f1008 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -13,7 +13,6 @@
tests/
- tests/database/factories/
diff --git a/src/Models/Ban.php b/src/Models/Ban.php
index 5644894..0477959 100644
--- a/src/Models/Ban.php
+++ b/src/Models/Ban.php
@@ -16,6 +16,7 @@
use Cog\Contracts\Ban\Ban as BanContract;
use Cog\Contracts\Ban\Bannable as BannableContract;
use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -23,6 +24,7 @@
class Ban extends Model implements BanContract
{
+ use HasFactory;
use SoftDeletes;
/**
diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php
index 014790d..06e1c86 100644
--- a/tests/AbstractTestCase.php
+++ b/tests/AbstractTestCase.php
@@ -15,6 +15,7 @@
use Cog\Laravel\Ban\Providers\BanServiceProvider;
use Cog\Tests\Laravel\Ban\Stubs\Models\User;
+use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\File;
use Orchestra\Testbench\TestCase as Orchestra;
@@ -110,8 +111,9 @@ protected function migratePackageTables(): void
*/
private function registerPackageFactories(): void
{
- $pathToFactories = realpath(__DIR__ . '/database/factories');
- $this->withFactories($pathToFactories);
+ Factory::guessFactoryNamesUsing(function (string $modelName) {
+ return 'Cog\\Tests\\Laravel\\Ban\\Database\\Factories\\' . class_basename($modelName) . 'Factory';
+ });
}
/**
diff --git a/tests/Stubs/Models/User.php b/tests/Stubs/Models/User.php
index 4af25b2..077ecc0 100644
--- a/tests/Stubs/Models/User.php
+++ b/tests/Stubs/Models/User.php
@@ -15,11 +15,14 @@
use Cog\Contracts\Ban\Bannable as BannableInterface;
use Cog\Laravel\Ban\Traits\Bannable;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
-final class User extends Authenticatable implements BannableInterface
+final class User extends Authenticatable implements
+ BannableInterface
{
use Bannable;
+ use HasFactory;
/**
* The table associated with the model.
diff --git a/tests/Stubs/Models/UserWithBannedAtScopeApplied.php b/tests/Stubs/Models/UserWithBannedAtScopeApplied.php
index 9dbce50..dff0f64 100644
--- a/tests/Stubs/Models/UserWithBannedAtScopeApplied.php
+++ b/tests/Stubs/Models/UserWithBannedAtScopeApplied.php
@@ -15,10 +15,13 @@
use Cog\Contracts\Ban\Bannable as BannableInterface;
use Cog\Laravel\Ban\Traits\Bannable;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
-final class UserWithBannedAtScopeApplied extends Authenticatable implements BannableInterface
+final class UserWithBannedAtScopeApplied extends Authenticatable implements
+ BannableInterface
{
+ use HasFactory;
use Bannable;
/**
diff --git a/tests/Unit/Events/ModelWasBannedTest.php b/tests/Unit/Events/ModelWasBannedTest.php
index 428a3ec..3fad8e1 100644
--- a/tests/Unit/Events/ModelWasBannedTest.php
+++ b/tests/Unit/Events/ModelWasBannedTest.php
@@ -23,10 +23,8 @@ final class ModelWasBannedTest extends AbstractTestCase
/** @test */
public function it_can_fire_event_on_helper_call(): void
{
- Event::fake([
- ModelWasBanned::class,
- ]);
- $entity = factory(User::class)->create();
+ Event::fake(ModelWasBanned::class);
+ $entity = User::factory()->create();
$entity->ban();
@@ -36,12 +34,10 @@ public function it_can_fire_event_on_helper_call(): void
/** @test */
public function it_can_fire_event_on_relation_create(): void
{
- Event::fake([
- ModelWasBanned::class,
- ]);
- $entity = factory(User::class)->create();
+ Event::fake(ModelWasBanned::class);
+ $entity = User::factory()->create();
- $entity->bans()->create([]);
+ $entity->bans()->create();
Event::assertDispatched(ModelWasBanned::class);
}
diff --git a/tests/Unit/Events/ModelWasUnbannedTest.php b/tests/Unit/Events/ModelWasUnbannedTest.php
index 58dd6bc..ce64e4a 100644
--- a/tests/Unit/Events/ModelWasUnbannedTest.php
+++ b/tests/Unit/Events/ModelWasUnbannedTest.php
@@ -23,10 +23,8 @@ final class ModelWasUnbannedTest extends AbstractTestCase
/** @test */
public function it_can_fire_event_on_helper_call(): void
{
- Event::fake([
- ModelWasUnbanned::class,
- ]);
- $ban = factory(Ban::class)->create();
+ Event::fake(ModelWasUnbanned::class);
+ $ban = Ban::factory()->create();
$ban->bannable->unban();
@@ -36,10 +34,8 @@ public function it_can_fire_event_on_helper_call(): void
/** @test */
public function it_can_fire_event_on_relation_delete(): void
{
- Event::fake([
- ModelWasUnbanned::class,
- ]);
- $ban = factory(Ban::class)->create();
+ Event::fake(ModelWasUnbanned::class);
+ $ban = Ban::factory()->create();
$ban->delete();
diff --git a/tests/Unit/Models/BanTest.php b/tests/Unit/Models/BanTest.php
index 9d539dc..04d342f 100644
--- a/tests/Unit/Models/BanTest.php
+++ b/tests/Unit/Models/BanTest.php
@@ -75,7 +75,7 @@ public function it_casts_expired_at(): void
/** @test */
public function it_casts_deleted_at(): void
{
- $ban = factory(Ban::class)->create([
+ $ban = Ban::factory()->create([
'deleted_at' => '2018-03-28 00:00:00',
]);
@@ -95,9 +95,9 @@ public function it_not_modify_null_expired_at(): void
/** @test */
public function it_can_has_ban_creator(): void
{
- $bannedBy = factory(User::class)->create();
+ $bannedBy = User::factory()->create();
- $ban = factory(Ban::class)->create([
+ $ban = Ban::factory()->create([
'created_by_type' => $bannedBy->getMorphClass(),
'created_by_id' => $bannedBy->getKey(),
]);
@@ -108,8 +108,8 @@ public function it_can_has_ban_creator(): void
/** @test */
public function it_can_set_custom_ban_creator(): void
{
- $bannable = factory(User::class)->create();
- $bannedBy = factory(User::class)->create();
+ $bannable = User::factory()->create();
+ $bannedBy = User::factory()->create();
$ban = $bannable->bans()->create([
'created_by_type' => $bannedBy->getMorphClass(),
@@ -122,9 +122,9 @@ public function it_can_set_custom_ban_creator(): void
/** @test */
public function it_not_overwrite_ban_creator_with_auth_user_if_custom_value_is_provided(): void
{
- $bannable = factory(User::class)->create();
- $bannedBy = factory(User::class)->create();
- $currentUser = factory(User::class)->create();
+ $bannable = User::factory()->create();
+ $bannedBy = User::factory()->create();
+ $currentUser = User::factory()->create();
$this->actingAs($currentUser);
@@ -175,9 +175,9 @@ public function it_can_make_model_with_expire_relative_date(): void
/** @test */
public function it_can_has_bannable_model(): void
{
- $user = factory(User::class)->create();
+ $user = User::factory()->create();
- $ban = factory(Ban::class)->create([
+ $ban = Ban::factory()->create([
'bannable_id' => $user->getKey(),
'bannable_type' => $user->getMorphClass(),
]);
@@ -188,13 +188,13 @@ public function it_can_has_bannable_model(): void
/** @test */
public function it_can_scope_bannable_models(): void
{
- $user1 = factory(User::class)->create();
- factory(Ban::class, 4)->create([
+ $user1 = User::factory()->create();
+ Ban::factory()->count(4)->create([
'bannable_id' => $user1->getKey(),
'bannable_type' => $user1->getMorphClass(),
]);
- $user2 = factory(User::class)->create();
- factory(Ban::class, 3)->create([
+ $user2 = User::factory()->create();
+ Ban::factory()->count(3)->create([
'bannable_id' => $user2->getKey(),
'bannable_type' => $user2->getMorphClass(),
]);
diff --git a/tests/Unit/Observers/BanObserverTest.php b/tests/Unit/Observers/BanObserverTest.php
index 3581ea6..8968193 100644
--- a/tests/Unit/Observers/BanObserverTest.php
+++ b/tests/Unit/Observers/BanObserverTest.php
@@ -21,11 +21,11 @@ final class BanObserverTest extends AbstractTestCase
/** @test */
public function it_can_set_banned_flag_to_owner_model_on_create(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
- $user->bans()->create([]);
+ $user->bans()->create();
$user->refresh();
$this->assertNotNull($user->banned_at);
diff --git a/tests/Unit/Scopes/BannedAtScopeTest.php b/tests/Unit/Scopes/BannedAtScopeTest.php
index afa49d9..d11fa0e 100644
--- a/tests/Unit/Scopes/BannedAtScopeTest.php
+++ b/tests/Unit/Scopes/BannedAtScopeTest.php
@@ -23,11 +23,10 @@ final class BannedAtScopeTest extends AbstractTestCase
/** @test */
public function it_can_get_all_models_by_default(): void
{
- factory(User::class, 2)->create([
+ User::factory()->count(2)->create([
'banned_at' => Carbon::now()->subDay(),
]);
-
- factory(User::class, 3)->create([
+ User::factory()->count(3)->create([
'banned_at' => null,
]);
@@ -39,10 +38,10 @@ public function it_can_get_all_models_by_default(): void
/** @test */
public function it_can_get_models_without_banned(): void
{
- factory(User::class, 2)->create([
+ User::factory()->count(2)->create([
'banned_at' => Carbon::now()->subDay(),
]);
- factory(User::class, 3)->create([
+ User::factory()->count(3)->create([
'banned_at' => null,
]);
@@ -54,10 +53,10 @@ public function it_can_get_models_without_banned(): void
/** @test */
public function it_can_get_models_with_banned(): void
{
- factory(User::class, 2)->create([
+ User::factory()->count(2)->create([
'banned_at' => Carbon::now()->subDay(),
]);
- factory(User::class, 3)->create([
+ User::factory()->count(3)->create([
'banned_at' => null,
]);
@@ -69,10 +68,10 @@ public function it_can_get_models_with_banned(): void
/** @test */
public function it_can_get_only_banned_models(): void
{
- factory(User::class, 2)->create([
+ User::factory()->count(2)->create([
'banned_at' => Carbon::now()->subDay(),
]);
- factory(User::class, 3)->create([
+ User::factory()->count(3)->create([
'banned_at' => null,
]);
@@ -84,10 +83,10 @@ public function it_can_get_only_banned_models(): void
/** @test */
public function it_can_auto_apply_banned_at_default_scope(): void
{
- factory(User::class, 3)->create([
+ User::factory()->count(3)->create([
'banned_at' => Carbon::now()->subDay(),
]);
- factory(User::class, 2)->create([
+ User::factory()->count(2)->create([
'banned_at' => null,
]);
diff --git a/tests/Unit/Services/BanServiceTest.php b/tests/Unit/Services/BanServiceTest.php
index 57431ce..b9eef0e 100644
--- a/tests/Unit/Services/BanServiceTest.php
+++ b/tests/Unit/Services/BanServiceTest.php
@@ -23,10 +23,10 @@ final class BanServiceTest extends AbstractTestCase
/** @test */
public function it_can_delete_all_expired_bans(): void
{
- factory(Ban::class, 3)->create([
+ Ban::factory()->count(3)->create([
'expired_at' => Carbon::now()->subMonth(),
]);
- factory(Ban::class, 4)->create([
+ Ban::factory()->count(4)->create([
'expired_at' => Carbon::now()->addMonth(),
]);
$banService = new BanService();
diff --git a/tests/Unit/Traits/BannableTest.php b/tests/Unit/Traits/BannableTest.php
index 7de791d..b5af3c9 100644
--- a/tests/Unit/Traits/BannableTest.php
+++ b/tests/Unit/Traits/BannableTest.php
@@ -24,9 +24,9 @@ final class BannableTest extends AbstractTestCase
/** @test */
public function it_can_has_related_ban(): void
{
- $user = factory(User::class)->create();
+ $user = User::factory()->create();
- $ban = factory(Ban::class)->create([
+ $ban = Ban::factory()->create([
'bannable_id' => $user->getKey(),
'bannable_type' => $user->getMorphClass(),
]);
@@ -38,9 +38,9 @@ public function it_can_has_related_ban(): void
/** @test */
public function it_can_has_many_related_bans(): void
{
- $user = factory(User::class)->create();
+ $user = User::factory()->create();
- factory(Ban::class, 2)->create([
+ Ban::factory()->count(2)->create([
'bannable_id' => $user->getKey(),
'bannable_type' => $user->getMorphClass(),
]);
@@ -51,7 +51,7 @@ public function it_can_has_many_related_bans(): void
/** @test */
public function it_can_ban(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
@@ -64,10 +64,10 @@ public function it_can_ban(): void
/** @test */
public function it_can_unban(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => Carbon::now(),
]);
- factory(Ban::class)->create([
+ Ban::factory()->create([
'bannable_id' => $user->getKey(),
'bannable_type' => $user->getMorphClass(),
]);
@@ -81,7 +81,7 @@ public function it_can_unban(): void
/** @test */
public function it_can_ban_user_with_banned_at_scope_applied(): void
{
- $user = factory(UserWithBannedAtScopeApplied::class)->create([
+ $user = UserWithBannedAtScopeApplied::factory()->create([
'banned_at' => Carbon::now(),
]);
@@ -95,10 +95,10 @@ public function it_can_ban_user_with_banned_at_scope_applied(): void
/** @test */
public function it_can_unban_user_with_banned_at_scope_applied(): void
{
- $user = factory(UserWithBannedAtScopeApplied::class)->create([
+ $user = UserWithBannedAtScopeApplied::factory()->create([
'banned_at' => Carbon::now(),
]);
- factory(Ban::class)->create([
+ Ban::factory()->create([
'bannable_id' => $user->getKey(),
'bannable_type' => $user->getMorphClass(),
]);
@@ -112,10 +112,10 @@ public function it_can_unban_user_with_banned_at_scope_applied(): void
/** @test */
public function it_can_delete_ban_on_unban(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => Carbon::now(),
]);
- factory(Ban::class)->create([
+ Ban::factory()->create([
'bannable_id' => $user->getKey(),
'bannable_type' => $user->getMorphClass(),
]);
@@ -129,10 +129,10 @@ public function it_can_delete_ban_on_unban(): void
/** @test */
public function it_can_soft_delete_ban_on_unban(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => Carbon::now(),
]);
- factory(Ban::class)->create([
+ Ban::factory()->create([
'bannable_id' => $user->getKey(),
'bannable_type' => $user->getMorphClass(),
]);
@@ -146,7 +146,7 @@ public function it_can_soft_delete_ban_on_unban(): void
/** @test */
public function it_can_return_ban_model(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
@@ -158,7 +158,7 @@ public function it_can_return_ban_model(): void
/** @test */
public function it_can_has_empty_banned_by(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
@@ -170,8 +170,8 @@ public function it_can_has_empty_banned_by(): void
/** @test */
public function it_can_has_current_user_as_banned_by(): void
{
- $bannedBy = factory(User::class)->create();
- $user = factory(User::class)->create([
+ $bannedBy = User::factory()->create();
+ $user = User::factory()->create([
'banned_at' => null,
]);
$this->actingAs($bannedBy);
@@ -184,7 +184,7 @@ public function it_can_has_current_user_as_banned_by(): void
/** @test */
public function it_can_ban_via_ban_relation_create(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
@@ -201,7 +201,7 @@ public function it_can_ban_via_ban_relation_create(): void
/** @test */
public function it_can_ban_with_comment(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
@@ -215,7 +215,7 @@ public function it_can_ban_with_comment(): void
/** @test */
public function it_can_ban_with_expiration_date(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
diff --git a/tests/Unit/Traits/HasBannedAtHelpersTest.php b/tests/Unit/Traits/HasBannedAtHelpersTest.php
index cfcf64d..bc69c15 100644
--- a/tests/Unit/Traits/HasBannedAtHelpersTest.php
+++ b/tests/Unit/Traits/HasBannedAtHelpersTest.php
@@ -22,7 +22,7 @@ final class HasBannedAtHelpersTest extends AbstractTestCase
/** @test */
public function it_can_set_banned_flag(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
@@ -34,7 +34,7 @@ public function it_can_set_banned_flag(): void
/** @test */
public function it_can_unset_banned_flag(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => Carbon::now(),
]);
@@ -46,7 +46,7 @@ public function it_can_unset_banned_flag(): void
/** @test */
public function it_can_check_if_entity_banned(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => Carbon::now(),
]);
@@ -56,7 +56,7 @@ public function it_can_check_if_entity_banned(): void
/** @test */
public function it_can_check_if_entity_not_banned(): void
{
- $user = factory(User::class)->create([
+ $user = User::factory()->create([
'banned_at' => null,
]);
diff --git a/tests/database/factories/BanFactory.php b/tests/database/factories/BanFactory.php
index abb3b13..c24ed4d 100644
--- a/tests/database/factories/BanFactory.php
+++ b/tests/database/factories/BanFactory.php
@@ -11,16 +11,26 @@
declare(strict_types=1);
+namespace Cog\Tests\Laravel\Ban\Database\Factories;
+
use Cog\Laravel\Ban\Models\Ban;
use Cog\Tests\Laravel\Ban\Stubs\Models\User;
-use Faker\Generator as Faker;
+use Illuminate\Database\Eloquent\Factories\Factory;
-/* @var \Illuminate\Database\Eloquent\Factory $factory */
-$factory->define(Ban::class, function (Faker $faker) {
- $bannable = factory(User::class)->create();
+final class BanFactory extends Factory
+{
+ protected $model = Ban::class;
- return [
- 'bannable_id' => $bannable->getKey(),
- 'bannable_type' => $bannable->getMorphClass(),
- ];
-});
+ /**
+ * Define the model's default state.
+ *
+ * @return array
+ */
+ public function definition(): array
+ {
+ return [
+ 'bannable_type' => (new User())->getMorphClass(),
+ 'bannable_id' => User::factory(),
+ ];
+ }
+}
diff --git a/tests/database/factories/UserFactory.php b/tests/database/factories/UserFactory.php
index cb38c8e..9164b74 100644
--- a/tests/database/factories/UserFactory.php
+++ b/tests/database/factories/UserFactory.php
@@ -11,20 +11,24 @@
declare(strict_types=1);
+namespace Cog\Tests\Laravel\Ban\Database\Factories;
+
use Cog\Tests\Laravel\Ban\Stubs\Models\User;
-use Cog\Tests\Laravel\Ban\Stubs\Models\UserWithBannedAtScopeApplied;
-use Faker\Generator as Faker;
+use Illuminate\Database\Eloquent\Factories\Factory;
-/* @var \Illuminate\Database\Eloquent\Factory $factory */
-$factory->define(User::class, function (Faker $faker) {
- return [
- 'name' => $faker->name,
- ];
-});
+final class UserFactory extends Factory
+{
+ protected $model = User::class;
-/* @var \Illuminate\Database\Eloquent\Factory $factory */
-$factory->define(UserWithBannedAtScopeApplied::class, function (Faker $faker) {
- return [
- 'name' => $faker->name,
- ];
-});
+ /**
+ * Define the model's default state.
+ *
+ * @return array
+ */
+ public function definition(): array
+ {
+ return [
+ 'name' => $this->faker->name(),
+ ];
+ }
+}
diff --git a/tests/database/factories/UserWithBannedAtScopeAppliedFactory.php b/tests/database/factories/UserWithBannedAtScopeAppliedFactory.php
new file mode 100644
index 0000000..2ec21cc
--- /dev/null
+++ b/tests/database/factories/UserWithBannedAtScopeAppliedFactory.php
@@ -0,0 +1,34 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+declare(strict_types=1);
+
+namespace Cog\Tests\Laravel\Ban\Database\Factories;
+
+use Cog\Tests\Laravel\Ban\Stubs\Models\UserWithBannedAtScopeApplied;
+use Illuminate\Database\Eloquent\Factories\Factory;
+
+final class UserWithBannedAtScopeAppliedFactory extends Factory
+{
+ protected $model = UserWithBannedAtScopeApplied::class;
+
+ /**
+ * Define the model's default state.
+ *
+ * @return array
+ */
+ public function definition(): array
+ {
+ return [
+ 'name' => $this->faker->name(),
+ ];
+ }
+}
diff --git a/tests/database/migrations/2016_09_02_173301_create_user_table.php b/tests/database/migrations/2016_09_02_173301_create_user_table.php
index d1db3df..ae31d8d 100644
--- a/tests/database/migrations/2016_09_02_173301_create_user_table.php
+++ b/tests/database/migrations/2016_09_02_173301_create_user_table.php
@@ -15,13 +15,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
-class CreateUserTable extends Migration
+return new class extends Migration
{
- /**
- * Run the migrations.
- *
- * @return void
- */
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
@@ -32,13 +27,8 @@ public function up(): void
});
}
- /**
- * Reverse the migrations.
- *
- * @return void
- */
public function down(): void
{
Schema::dropIfExists('users');
}
-}
+};