Skip to content

Commit

Permalink
Remove legacy factories (#91)
Browse files Browse the repository at this point in the history
Replace legacy factories with the new ones
  • Loading branch information
antonkomarev authored Feb 25, 2023
1 parent 5478657 commit f5ad6a6
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 113 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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": {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">tests/</directory>
<exclude>tests/database/factories/</exclude>
</testsuite>
</testsuites>
<php>
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Ban.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
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;
use Illuminate\Support\Carbon;

class Ban extends Model implements BanContract
{
use HasFactory;
use SoftDeletes;

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';
});
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Stubs/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion tests/Stubs/Models/UserWithBannedAtScopeApplied.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
14 changes: 5 additions & 9 deletions tests/Unit/Events/ModelWasBannedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}
Expand Down
12 changes: 4 additions & 8 deletions tests/Unit/Events/ModelWasUnbannedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down
28 changes: 14 additions & 14 deletions tests/Unit/Models/BanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);

Expand All @@ -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(),
]);
Expand All @@ -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(),
Expand All @@ -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);

Expand Down Expand Up @@ -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(),
]);
Expand All @@ -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(),
]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Observers/BanObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 10 additions & 11 deletions tests/Unit/Scopes/BannedAtScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand All @@ -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,
]);

Expand All @@ -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,
]);

Expand All @@ -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,
]);

Expand All @@ -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,
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Services/BanServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading

0 comments on commit f5ad6a6

Please sign in to comment.