1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 13:34:04 +00:00

phpunit to pest

This commit is contained in:
Abrar Ahmad 2022-08-03 15:28:35 +05:00
parent 3990aa6eb6
commit e4442cf6cb

View file

@ -2,176 +2,142 @@
declare(strict_types=1); declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Commands\ClearPendingTenants; use Stancl\Tenancy\Commands\ClearPendingTenants;
use Stancl\Tenancy\Commands\CreatePendingTenants; use Stancl\Tenancy\Commands\CreatePendingTenants;
use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantPulled;
use Stancl\Tenancy\Events\CreatingPendingTenant; use Stancl\Tenancy\Events\CreatingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantCreated; use Stancl\Tenancy\Events\PendingTenantCreated;
use Stancl\Tenancy\Events\PendingTenantPulled;
use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
class PendingTenantsTest extends TestCase test('a tenant is correctly identified as pending', function (){
{ Tenant::createPending();
public function setUp(): void
{
parent::setUp();
}
/** @test */ expect(Tenant::onlyPending()->count())->toBe(1);
public function a_tenant_is_correctly_identified_as_pending()
{
Tenant::createPending();
$this->assertCount(1, Tenant::onlyPending()->get()); Tenant::onlyPending()->first()->update([
'pending_since' => null
]);
Tenant::onlyPending()->first()->update([ expect(Tenant::onlyPending()->count())->toBe(0);
'pending_since' => null });
]);
$this->assertCount(0, Tenant::onlyPending()->get()); test('pending trait imports query scopes', function () {
} Tenant::createPending();
Tenant::create();
Tenant::create();
/** @test */ expect(Tenant::onlyPending()->count())->toBe(1)
public function pending_trait_imports_query_scopes() ->and(Tenant::withPending(true)->count())->toBe(3)
{ ->and(Tenant::withPending(false)->count())->toBe(2)
Tenant::createPending(); ->and(Tenant::withoutPending()->count())->toBe(2);
Tenant::create();
Tenant::create();
$this->assertCount(1, Tenant::onlyPending()->get()); });
$this->assertCount(3, Tenant::withPending(true)->get()); test('pending tenants are created and deleted from the commands', function () {
config(['tenancy.pending.count' => 4]);
$this->assertCount(2, Tenant::withPending(false)->get()); Artisan::call(CreatePendingTenants::class);
$this->assertCount(2, Tenant::withoutPending()->get()); expect(Tenant::onlyPending()->count())->toBe(4);
}
/** @test */ Artisan::call(ClearPendingTenants::class);
public function pending_tenants_are_created_and_deleted_from_the_commands()
{
config(['tenancy.pending.count' => 4]);
Artisan::call(CreatePendingTenants::class); expect(Tenant::onlyPending()->count())->toBe(0);
});
$this->assertCount(4, Tenant::onlyPending()->get()); test('clear pending tenants command only delete pending tenants older than', function () {
config(['tenancy.pending.count' => 2]);
Artisan::call(ClearPendingTenants::class); Artisan::call(CreatePendingTenants::class);
$this->assertCount(0, Tenant::onlyPending()->get()); tenancy()->model()->query()->onlyPending()->first()->update([
} 'pending_since' => now()->subDays(5)->timestamp
]);
/** @test */ Artisan::call('tenants:pending-clear --older-than-days=2');
public function clear_pending_tenants_command_only_delete_pending_tenants_older_than()
{
config(['tenancy.pending.count' => 2]);
Artisan::call(CreatePendingTenants::class); expect(Tenant::onlyPending()->count())->toBe(1);
});
tenancy()->model()->query()->onlyPending()->first()->update([ test('clear pending tenants command cannot run with both time constraints', function () {
'pending_since' => now()->subDays(5)->timestamp pest()->artisan('tenants:pending-clear --older-than-days=2 --older-than-hours=2')
]); ->assertFailed();
});
Artisan::call('tenants:pending-clear --older-than-days=2'); test('clear pending tenants command all option overrides config', function () {
Tenant::createPending();
Tenant::createPending();
$this->assertCount(1, Tenant::onlyPending()->get()); tenancy()->model()->query()->onlyPending()->first()->update([
} 'pending_since' => now()->subDays(10)
]);
/** @test */ config(['tenancy.pending.older_than_days' => 4]);
public function clear_pending_tenants_command_cannot_run_with_both_time_constraints()
{
$this->artisan('tenants:pending-clear --older-than-days=2 --older-than-hours=2')
->assertFailed();
}
/** @test */ Artisan::call(ClearPendingTenants::class, [
public function clear_pending_tenants_command_all_option_overrides_config() '--all' => true
{ ]);
Tenant::createPending();
Tenant::createPending();
tenancy()->model()->query()->onlyPending()->first()->update([ expect(Tenant::onlyPending()->count())->toBe(0);
'pending_since' => now()->subDays(10) });
]);
config(['tenancy.pending.older_than_days' => 4]); test('tenancy can check for rpending tenants', function () {
Tenant::query()->delete();
Artisan::call(ClearPendingTenants::class, [ expect(Tenant::onlyPending()->exists())->toBeFalse();
'--all' => true
]);
$this->assertCount(0, Tenant::onlyPending()->get()); Tenant::createPending();
}
/** @test */ expect(Tenant::onlyPending()->exists())->toBeTrue();
public function tenancy_can_check_for_rpending_tenants() });
{
Tenant::query()->delete();
$this->assertFalse(Tenant::onlyPending()->exists()); test('tenancy can pull a pending tenant', function () {
expect(Tenant::pullPendingTenant())->toBeNull();
Tenant::createPending(); Tenant::createPending();
$this->assertTrue(Tenant::onlyPending()->exists()); expect(Tenant::pullPendingTenant(true))->toBeInstanceOf(Tenant::class);
} });
/** @test */ test('tenancy can create if none are pending', function () {
public function tenancy_can_pull_a_pending_tenant() expect(Tenant::all()->count())->toBe(0);
{
$this->assertNull(Tenant::pullPendingTenant());
Tenant::createPending(); Tenant::pullPendingTenant(true);
$this->assertInstanceOf(Tenant::class, Tenant::pullPendingTenant(true)); expect(Tenant::all()->count())->toBe(1);
} });
/** @test */ test('pending tenants global scope config can include or exclude', function () {
public function tenancy_can_create_if_none_are_pending() Tenant::createPending();
{
$this->assertCount(0, Tenant::all());
Tenant::pullPendingTenant(true); config(['tenancy.pending.include_in_queries' => false]);
$this->assertCount(1, Tenant::all()); expect(Tenant::all()->count())->toBe(0);
}
/** @test */ config(['tenancy.pending.include_in_queries' => true]);
public function pending_tenants_global_scope_config_can_include_or_exclude()
{
Tenant::createPending();
config(['tenancy.pending.include_in_queries' => false]); expect(Tenant::all()->count())->toBe(1);
Tenant::all();
});
$this->assertCount(0, Tenant::all()); test('pending events are triggerred', function () {
Event::fake([
CreatingPendingTenant::class,
PendingTenantCreated::class,
PullingPendingTenant::class,
PendingTenantPulled::class,
]);
config(['tenancy.pending.include_in_queries' => true]); Tenant::createPending();
$this->assertCount(1, Tenant::all()); Event::assertDispatched(CreatingPendingTenant::class);
Tenant::all(); Event::assertDispatched(PendingTenantCreated::class);
}
/** @test */ Tenant::pullPendingTenant();
public function pending_events_are_triggerred()
{
Event::fake([
CreatingPendingTenant::class,
PendingTenantCreated::class,
PullingPendingTenant::class,
PendingTenantPulled::class,
]);
Tenant::createPending(); Event::assertDispatched(PullingPendingTenant::class);
Event::assertDispatched(PendingTenantPulled::class);
Event::assertDispatched(CreatingPendingTenant::class); });
Event::assertDispatched(PendingTenantCreated::class);
Tenant::pullPendingTenant();
Event::assertDispatched(PullingPendingTenant::class);
Event::assertDispatched(PendingTenantPulled::class);
}
}