1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 19:14:03 +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,72 +2,53 @@
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Commands\ClearPendingTenants;
use Stancl\Tenancy\Commands\CreatePendingTenants;
use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantPulled;
use Stancl\Tenancy\Events\CreatingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantCreated;
use Stancl\Tenancy\Events\PendingTenantPulled;
use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Tests\Etc\Tenant;
class PendingTenantsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}
/** @test */
public function a_tenant_is_correctly_identified_as_pending()
{
test('a tenant is correctly identified as pending', function (){
Tenant::createPending();
$this->assertCount(1, Tenant::onlyPending()->get());
expect(Tenant::onlyPending()->count())->toBe(1);
Tenant::onlyPending()->first()->update([
'pending_since' => null
]);
$this->assertCount(0, Tenant::onlyPending()->get());
}
expect(Tenant::onlyPending()->count())->toBe(0);
});
/** @test */
public function pending_trait_imports_query_scopes()
{
test('pending trait imports query scopes', function () {
Tenant::createPending();
Tenant::create();
Tenant::create();
$this->assertCount(1, Tenant::onlyPending()->get());
expect(Tenant::onlyPending()->count())->toBe(1)
->and(Tenant::withPending(true)->count())->toBe(3)
->and(Tenant::withPending(false)->count())->toBe(2)
->and(Tenant::withoutPending()->count())->toBe(2);
$this->assertCount(3, Tenant::withPending(true)->get());
});
$this->assertCount(2, Tenant::withPending(false)->get());
$this->assertCount(2, Tenant::withoutPending()->get());
}
/** @test */
public function pending_tenants_are_created_and_deleted_from_the_commands()
{
test('pending tenants are created and deleted from the commands', function () {
config(['tenancy.pending.count' => 4]);
Artisan::call(CreatePendingTenants::class);
$this->assertCount(4, Tenant::onlyPending()->get());
expect(Tenant::onlyPending()->count())->toBe(4);
Artisan::call(ClearPendingTenants::class);
$this->assertCount(0, Tenant::onlyPending()->get());
}
expect(Tenant::onlyPending()->count())->toBe(0);
});
/** @test */
public function clear_pending_tenants_command_only_delete_pending_tenants_older_than()
{
test('clear pending tenants command only delete pending tenants older than', function () {
config(['tenancy.pending.count' => 2]);
Artisan::call(CreatePendingTenants::class);
@ -78,19 +59,15 @@ class PendingTenantsTest extends TestCase
Artisan::call('tenants:pending-clear --older-than-days=2');
$this->assertCount(1, Tenant::onlyPending()->get());
}
expect(Tenant::onlyPending()->count())->toBe(1);
});
/** @test */
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')
test('clear pending tenants command cannot run with both time constraints', function () {
pest()->artisan('tenants:pending-clear --older-than-days=2 --older-than-hours=2')
->assertFailed();
}
});
/** @test */
public function clear_pending_tenants_command_all_option_overrides_config()
{
test('clear pending tenants command all option overrides config', function () {
Tenant::createPending();
Tenant::createPending();
@ -104,59 +81,49 @@ class PendingTenantsTest extends TestCase
'--all' => true
]);
$this->assertCount(0, Tenant::onlyPending()->get());
}
expect(Tenant::onlyPending()->count())->toBe(0);
});
/** @test */
public function tenancy_can_check_for_rpending_tenants()
{
test('tenancy can check for rpending tenants', function () {
Tenant::query()->delete();
$this->assertFalse(Tenant::onlyPending()->exists());
expect(Tenant::onlyPending()->exists())->toBeFalse();
Tenant::createPending();
$this->assertTrue(Tenant::onlyPending()->exists());
}
expect(Tenant::onlyPending()->exists())->toBeTrue();
});
/** @test */
public function tenancy_can_pull_a_pending_tenant()
{
$this->assertNull(Tenant::pullPendingTenant());
test('tenancy can pull a pending tenant', function () {
expect(Tenant::pullPendingTenant())->toBeNull();
Tenant::createPending();
$this->assertInstanceOf(Tenant::class, Tenant::pullPendingTenant(true));
}
expect(Tenant::pullPendingTenant(true))->toBeInstanceOf(Tenant::class);
});
/** @test */
public function tenancy_can_create_if_none_are_pending()
{
$this->assertCount(0, Tenant::all());
test('tenancy can create if none are pending', function () {
expect(Tenant::all()->count())->toBe(0);
Tenant::pullPendingTenant(true);
$this->assertCount(1, Tenant::all());
}
expect(Tenant::all()->count())->toBe(1);
});
/** @test */
public function pending_tenants_global_scope_config_can_include_or_exclude()
{
test('pending tenants global scope config can include or exclude', function () {
Tenant::createPending();
config(['tenancy.pending.include_in_queries' => false]);
$this->assertCount(0, Tenant::all());
expect(Tenant::all()->count())->toBe(0);
config(['tenancy.pending.include_in_queries' => true]);
$this->assertCount(1, Tenant::all());
expect(Tenant::all()->count())->toBe(1);
Tenant::all();
}
});
/** @test */
public function pending_events_are_triggerred()
{
test('pending events are triggerred', function () {
Event::fake([
CreatingPendingTenant::class,
PendingTenantCreated::class,
@ -173,5 +140,4 @@ class PendingTenantsTest extends TestCase
Event::assertDispatched(PullingPendingTenant::class);
Event::assertDispatched(PendingTenantPulled::class);
}
}
});