1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 11:14:04 +00:00

[4.x] Migrate tests to Pest (#884)

* Add Pest dependencies

* Add base Pest file

* Convert test cases

* Remove non-compound imports

* Adopt expectation API

* Optimize uses

* Shift cleanup

* phpunit -> pest

* Fix tests in PR #884 PHPUnit to Pest Converter  (#885)

* fixed tests, remove method duplications, restore necessary inner classes

* Update CommandsTest.php

* temporary checks run on `shift-64622` on branch.

* fixed `TestSeeder` class not resolved

* fixed messed up names

* removed `uses` from individual files and add it in `Pest`

* extract tests to helpers

* use pest dataset

* Update AutomaticModeTest.php

* newline

* todo convention

* resolve reviews

* added `// todo@tests`

* remove shift branch from CI workflow

Co-authored-by: Samuel Štancl <samuel@archte.ch>

* check if I have write permission

* Convert newly added tests to Pest

Co-authored-by: Shift <shift@laravelshift.com>
Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com>
This commit is contained in:
Samuel Štancl 2022-07-22 19:26:59 +02:00 committed by GitHub
parent 69de181b7d
commit b47c5549ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 3010 additions and 3478 deletions

View file

@ -2,8 +2,6 @@
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Event;
@ -21,148 +19,127 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\UUIDGenerator;
class TenantModelTest extends TestCase
{
/** @test */
public function created_event_is_dispatched()
{
Event::fake([TenantCreated::class]);
test('created event is dispatched', function () {
Event::fake([TenantCreated::class]);
Event::assertNotDispatched(TenantCreated::class);
Event::assertNotDispatched(TenantCreated::class);
Tenant::create();
Tenant::create();
Event::assertDispatched(TenantCreated::class);
}
Event::assertDispatched(TenantCreated::class);
});
/** @test */
public function current_tenant_can_be_resolved_from_service_container_using_typehint()
{
$tenant = Tenant::create();
test('current tenant can be resolved from service container using typehint', function () {
$tenant = Tenant::create();
tenancy()->initialize($tenant);
tenancy()->initialize($tenant);
$this->assertSame($tenant->id, app(Contracts\Tenant::class)->id);
expect(app(Contracts\Tenant::class)->id)->toBe($tenant->id);
tenancy()->end();
tenancy()->end();
$this->assertSame(null, app(Contracts\Tenant::class));
}
expect(app(Contracts\Tenant::class))->toBe(null);
});
/** @test */
public function id_is_generated_when_no_id_is_supplied()
{
config(['tenancy.id_generator' => UUIDGenerator::class]);
test('id is generated when no id is supplied', function () {
config(['tenancy.id_generator' => UUIDGenerator::class]);
$this->mock(UUIDGenerator::class, function ($mock) {
return $mock->shouldReceive('generate')->once();
});
$this->mock(UUIDGenerator::class, function ($mock) {
return $mock->shouldReceive('generate')->once();
});
$tenant = Tenant::create();
$tenant = Tenant::create();
$this->assertNotNull($tenant->id);
}
$this->assertNotNull($tenant->id);
});
/** @test */
public function autoincrement_ids_are_supported()
{
Schema::drop('domains');
Schema::table('tenants', function (Blueprint $table) {
$table->bigIncrements('id')->change();
});
test('autoincrement ids are supported', function () {
Schema::drop('domains');
Schema::table('tenants', function (Blueprint $table) {
$table->bigIncrements('id')->change();
});
unset(app()[UniqueIdentifierGenerator::class]);
unset(app()[UniqueIdentifierGenerator::class]);
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
$this->assertSame(1, $tenant1->id);
$this->assertSame(2, $tenant2->id);
}
expect($tenant1->id)->toBe(1);
expect($tenant2->id)->toBe(2);
});
/** @test */
public function custom_tenant_model_can_be_used()
{
$tenant = MyTenant::create();
test('custom tenant model can be used', function () {
$tenant = MyTenant::create();
tenancy()->initialize($tenant);
tenancy()->initialize($tenant);
$this->assertTrue(tenant() instanceof MyTenant);
}
expect(tenant() instanceof MyTenant)->toBeTrue();
});
/** @test */
public function custom_tenant_model_that_doesnt_extend_vendor_Tenant_model_can_be_used()
{
$tenant = AnotherTenant::create([
'id' => 'acme',
test('custom tenant model that doesnt extend vendor tenant model can be used', function () {
$tenant = AnotherTenant::create([
'id' => 'acme',
]);
tenancy()->initialize($tenant);
expect(tenant() instanceof AnotherTenant)->toBeTrue();
});
test('tenant can be created even when we are in another tenants context', function () {
config(['tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function ($event) {
return $event->tenant;
})->toListener());
$tenant1 = Tenant::create([
'id' => 'foo',
'tenancy_db_name' => 'db' . Str::random(16),
]);
tenancy()->initialize($tenant1);
$tenant2 = Tenant::create([
'id' => 'bar',
'tenancy_db_name' => 'db' . Str::random(16),
]);
tenancy()->end();
expect(Tenant::count())->toBe(2);
});
test('the model uses tenant collection', function () {
Tenant::create();
Tenant::create();
expect(Tenant::count())->toBe(2);
expect(Tenant::all() instanceof TenantCollection)->toBeTrue();
});
test('a command can be run on a collection of tenants', function () {
Tenant::create([
'id' => 't1',
'foo' => 'bar',
]);
Tenant::create([
'id' => 't2',
'foo' => 'bar',
]);
Tenant::all()->runForEach(function ($tenant) {
$tenant->update([
'foo' => 'xyz',
]);
});
tenancy()->initialize($tenant);
$this->assertTrue(tenant() instanceof AnotherTenant);
}
/** @test */
public function tenant_can_be_created_even_when_we_are_in_another_tenants_context()
{
config(['tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function ($event) {
return $event->tenant;
})->toListener());
$tenant1 = Tenant::create([
'id' => 'foo',
'tenancy_db_name' => 'db' . Str::random(16),
]);
tenancy()->initialize($tenant1);
$tenant2 = Tenant::create([
'id' => 'bar',
'tenancy_db_name' => 'db' . Str::random(16),
]);
tenancy()->end();
$this->assertSame(2, Tenant::count());
}
/** @test */
public function the_model_uses_TenantCollection()
{
Tenant::create();
Tenant::create();
$this->assertSame(2, Tenant::count());
$this->assertTrue(Tenant::all() instanceof TenantCollection);
}
/** @test */
public function a_command_can_be_run_on_a_collection_of_tenants()
{
Tenant::create([
'id' => 't1',
'foo' => 'bar',
]);
Tenant::create([
'id' => 't2',
'foo' => 'bar',
]);
Tenant::all()->runForEach(function ($tenant) {
$tenant->update([
'foo' => 'xyz',
]);
});
$this->assertSame('xyz', Tenant::find('t1')->foo);
$this->assertSame('xyz', Tenant::find('t2')->foo);
}
}
expect(Tenant::find('t1')->foo)->toBe('xyz');
expect(Tenant::find('t2')->foo)->toBe('xyz');
});
class MyTenant extends Tenant
{