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

Adopt expectation API

This commit is contained in:
Shift 2022-06-28 20:56:06 +00:00
parent 5637018b0d
commit cef9529d6a
No known key found for this signature in database
GPG key ID: 5A96F038425C5A1C
22 changed files with 278 additions and 278 deletions

View file

@ -28,7 +28,7 @@ test('context is switched when tenancy is initialized', function () {
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame('acme', app('tenancy_initialized_for_tenant')); expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
}); });
test('context is reverted when tenancy is ended', function () { test('context is reverted when tenancy is ended', function () {
@ -36,7 +36,7 @@ test('context is reverted when tenancy is ended', function () {
tenancy()->end(); tenancy()->end();
$this->assertSame(true, app('tenancy_ended')); expect(app('tenancy_ended'))->toBe(true);
}); });
test('context is switched when tenancy is reinitialized', function () { test('context is switched when tenancy is reinitialized', function () {
@ -50,7 +50,7 @@ test('context is switched when tenancy is reinitialized', function () {
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame('acme', app('tenancy_initialized_for_tenant')); expect(app('tenancy_initialized_for_tenant'))->toBe('acme');
$tenant2 = Tenant::create([ $tenant2 = Tenant::create([
'id' => 'foobar', 'id' => 'foobar',
@ -58,17 +58,17 @@ test('context is switched when tenancy is reinitialized', function () {
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
$this->assertSame('foobar', app('tenancy_initialized_for_tenant')); expect(app('tenancy_initialized_for_tenant'))->toBe('foobar');
}); });
test('central helper runs callbacks in the central state', function () { test('central helper runs callbacks in the central state', function () {
tenancy()->initialize($tenant = Tenant::create()); tenancy()->initialize($tenant = Tenant::create());
tenancy()->central(function () { tenancy()->central(function () {
$this->assertSame(null, tenant()); expect(tenant())->toBe(null);
}); });
$this->assertSame($tenant, tenant()); expect(tenant())->toBe($tenant);
}); });
test('central helper returns the value from the callback', function () { test('central helper returns the value from the callback', function () {
@ -86,19 +86,19 @@ test('central helper reverts back to tenant context', function () {
// //
}); });
$this->assertSame($tenant, tenant()); expect(tenant())->toBe($tenant);
}); });
test('central helper doesnt change tenancy state when called in central context', function () { test('central helper doesnt change tenancy state when called in central context', function () {
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this->assertNull(tenant()); expect(tenant())->toBeNull();
tenancy()->central(function () { tenancy()->central(function () {
// //
}); });
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this->assertNull(tenant()); expect(tenant())->toBeNull();
}); });
// Helpers // Helpers

View file

@ -50,21 +50,21 @@ test('database data is separated', function () {
// Create Foo user // Create Foo user
DB::table('users')->insert(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']); DB::table('users')->insert(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
$this->assertCount(1, DB::table('users')->get()); expect(DB::table('users')->get())->toHaveCount(1);
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
// Assert Foo user is not in this DB // Assert Foo user is not in this DB
$this->assertCount(0, DB::table('users')->get()); expect(DB::table('users')->get())->toHaveCount(0);
// Create Bar user // Create Bar user
DB::table('users')->insert(['name' => 'Bar', 'email' => 'bar@bar.com', 'password' => 'secret']); DB::table('users')->insert(['name' => 'Bar', 'email' => 'bar@bar.com', 'password' => 'secret']);
$this->assertCount(1, DB::table('users')->get()); expect(DB::table('users')->get())->toHaveCount(1);
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
// Assert Bar user is not in this DB // Assert Bar user is not in this DB
$this->assertCount(1, DB::table('users')->get()); expect(DB::table('users')->get())->toHaveCount(1);
$this->assertSame('Foo', DB::table('users')->first()->name); expect(DB::table('users')->first()->name)->toBe('Foo');
}); });
test('cache data is separated', function () { test('cache data is separated', function () {
@ -79,33 +79,33 @@ test('cache data is separated', function () {
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
cache()->set('foo', 'central'); cache()->set('foo', 'central');
$this->assertSame('central', Cache::get('foo')); expect(Cache::get('foo'))->toBe('central');
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
// Assert central cache doesn't leak to tenant context // Assert central cache doesn't leak to tenant context
$this->assertFalse(Cache::has('foo')); expect(Cache::has('foo'))->toBeFalse();
cache()->set('foo', 'bar'); cache()->set('foo', 'bar');
$this->assertSame('bar', Cache::get('foo')); expect(Cache::get('foo'))->toBe('bar');
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
// Assert one tenant's data doesn't leak to another tenant // Assert one tenant's data doesn't leak to another tenant
$this->assertFalse(Cache::has('foo')); expect(Cache::has('foo'))->toBeFalse();
cache()->set('foo', 'xyz'); cache()->set('foo', 'xyz');
$this->assertSame('xyz', Cache::get('foo')); expect(Cache::get('foo'))->toBe('xyz');
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
// Asset data didn't leak to original tenant // Asset data didn't leak to original tenant
$this->assertSame('bar', Cache::get('foo')); expect(Cache::get('foo'))->toBe('bar');
tenancy()->end(); tenancy()->end();
// Asset central is still the same // Asset central is still the same
$this->assertSame('central', Cache::get('foo')); expect(Cache::get('foo'))->toBe('central');
}); });
test('redis data is separated', function () { test('redis data is separated', function () {
@ -118,23 +118,23 @@ test('redis data is separated', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
Redis::set('foo', 'bar'); Redis::set('foo', 'bar');
$this->assertSame('bar', Redis::get('foo')); expect(Redis::get('foo'))->toBe('bar');
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
$this->assertSame(null, Redis::get('foo')); expect(Redis::get('foo'))->toBe(null);
Redis::set('foo', 'xyz'); Redis::set('foo', 'xyz');
Redis::set('abc', 'def'); Redis::set('abc', 'def');
$this->assertSame('xyz', Redis::get('foo')); expect(Redis::get('foo'))->toBe('xyz');
$this->assertSame('def', Redis::get('abc')); expect(Redis::get('abc'))->toBe('def');
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
$this->assertSame('bar', Redis::get('foo')); expect(Redis::get('foo'))->toBe('bar');
$this->assertSame(null, Redis::get('abc')); expect(Redis::get('abc'))->toBe(null);
$tenant3 = Tenant::create(); $tenant3 = Tenant::create();
tenancy()->initialize($tenant3); tenancy()->initialize($tenant3);
$this->assertSame(null, Redis::get('foo')); expect(Redis::get('foo'))->toBe(null);
$this->assertSame(null, Redis::get('abc')); expect(Redis::get('abc'))->toBe(null);
}); });
test('filesystem data is separated', function () { test('filesystem data is separated', function () {
@ -154,34 +154,34 @@ test('filesystem data is separated', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
Storage::disk('public')->put('foo', 'bar'); Storage::disk('public')->put('foo', 'bar');
$this->assertSame('bar', Storage::disk('public')->get('foo')); expect(Storage::disk('public')->get('foo'))->toBe('bar');
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
$this->assertFalse(Storage::disk('public')->exists('foo')); expect(Storage::disk('public')->exists('foo'))->toBeFalse();
Storage::disk('public')->put('foo', 'xyz'); Storage::disk('public')->put('foo', 'xyz');
Storage::disk('public')->put('abc', 'def'); Storage::disk('public')->put('abc', 'def');
$this->assertSame('xyz', Storage::disk('public')->get('foo')); expect(Storage::disk('public')->get('foo'))->toBe('xyz');
$this->assertSame('def', Storage::disk('public')->get('abc')); expect(Storage::disk('public')->get('abc'))->toBe('def');
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
$this->assertSame('bar', Storage::disk('public')->get('foo')); expect(Storage::disk('public')->get('foo'))->toBe('bar');
$this->assertFalse(Storage::disk('public')->exists('abc')); expect(Storage::disk('public')->exists('abc'))->toBeFalse();
$tenant3 = Tenant::create(); $tenant3 = Tenant::create();
tenancy()->initialize($tenant3); tenancy()->initialize($tenant3);
$this->assertFalse(Storage::disk('public')->exists('foo')); expect(Storage::disk('public')->exists('foo'))->toBeFalse();
$this->assertFalse(Storage::disk('public')->exists('abc')); expect(Storage::disk('public')->exists('abc'))->toBeFalse();
$expected_storage_path = $old_storage_path . '/tenant' . tenant('id'); // /tenant = suffix base $expected_storage_path = $old_storage_path . '/tenant' . tenant('id'); // /tenant = suffix base
// Check that disk prefixes respect the root_override logic // Check that disk prefixes respect the root_override logic
$this->assertSame($expected_storage_path . '/app/', getDiskPrefix('local')); expect(getDiskPrefix('local'))->toBe($expected_storage_path . '/app/');
$this->assertSame($expected_storage_path . '/app/public/', getDiskPrefix('public')); expect(getDiskPrefix('public'))->toBe($expected_storage_path . '/app/public/');
$this->assertSame('tenant' . tenant('id') . '/', getDiskPrefix('s3'), '/'); $this->assertSame('tenant' . tenant('id') . '/', getDiskPrefix('s3'), '/');
// Check suffixing logic // Check suffixing logic
$new_storage_path = storage_path(); $new_storage_path = storage_path();
$this->assertEquals($expected_storage_path, $new_storage_path); expect($new_storage_path)->toEqual($expected_storage_path);
}); });
// Helpers // Helpers

View file

@ -28,14 +28,14 @@ test('tags are merged when array is passed', function () {
tenancy()->initialize(Tenant::create()); tenancy()->initialize(Tenant::create());
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar']; $expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo', 'bar'];
$this->assertEquals($expected, cache()->tags(['foo', 'bar'])->getTags()->getNames()); expect(cache()->tags(['foo', 'bar'])->getTags()->getNames())->toEqual($expected);
}); });
test('tags are merged when string is passed', function () { test('tags are merged when string is passed', function () {
tenancy()->initialize(Tenant::create()); tenancy()->initialize(Tenant::create());
$expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo']; $expected = [config('tenancy.cache.tag_base') . tenant('id'), 'foo'];
$this->assertEquals($expected, cache()->tags('foo')->getTags()->getNames()); expect(cache()->tags('foo')->getTags()->getNames())->toEqual($expected);
}); });
test('exception is thrown when zero arguments are passed to tags method', function () { test('exception is thrown when zero arguments are passed to tags method', function () {
@ -57,7 +57,7 @@ test('tags separate cache well enough', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
cache()->put('foo', 'bar', 1); cache()->put('foo', 'bar', 1);
$this->assertSame('bar', cache()->get('foo')); expect(cache()->get('foo'))->toBe('bar');
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
@ -65,7 +65,7 @@ test('tags separate cache well enough', function () {
$this->assertNotSame('bar', cache()->get('foo')); $this->assertNotSame('bar', cache()->get('foo'));
cache()->put('foo', 'xyz', 1); cache()->put('foo', 'xyz', 1);
$this->assertSame('xyz', cache()->get('foo')); expect(cache()->get('foo'))->toBe('xyz');
}); });
test('invoking the cache helper works', function () { test('invoking the cache helper works', function () {
@ -73,7 +73,7 @@ test('invoking the cache helper works', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 1); cache(['foo' => 'bar'], 1);
$this->assertSame('bar', cache('foo')); expect(cache('foo'))->toBe('bar');
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
@ -81,7 +81,7 @@ test('invoking the cache helper works', function () {
$this->assertNotSame('bar', cache('foo')); $this->assertNotSame('bar', cache('foo'));
cache(['foo' => 'xyz'], 1); cache(['foo' => 'xyz'], 1);
$this->assertSame('xyz', cache('foo')); expect(cache('foo'))->toBe('xyz');
}); });
test('cache is persisted', function () { test('cache is persisted', function () {
@ -89,12 +89,12 @@ test('cache is persisted', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 10); cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo')); expect(cache('foo'))->toBe('bar');
tenancy()->end(); tenancy()->end();
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
$this->assertSame('bar', cache('foo')); expect(cache('foo'))->toBe('bar');
}); });
test('cache is persisted when reidentification is used', function () { test('cache is persisted when reidentification is used', function () {
@ -103,11 +103,11 @@ test('cache is persisted when reidentification is used', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 10); cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo')); expect(cache('foo'))->toBe('bar');
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
tenancy()->end(); tenancy()->end();
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
$this->assertSame('bar', cache('foo')); expect(cache('foo'))->toBe('bar');
}); });

View file

@ -18,8 +18,8 @@ test('tenants can be resolved using the cached resolver', function () {
'domain' => 'acme', 'domain' => 'acme',
]); ]);
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
}); });
test('the underlying resolver is not touched when using the cached resolver', function () { test('the underlying resolver is not touched when using the cached resolver', function () {
@ -32,17 +32,17 @@ test('the underlying resolver is not touched when using the cached resolver', fu
DomainTenantResolver::$shouldCache = false; DomainTenantResolver::$shouldCache = false;
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
DB::flushQueryLog(); DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
$this->assertNotEmpty(DB::getQueryLog()); // not empty $this->assertNotEmpty(DB::getQueryLog()); // not empty
DomainTenantResolver::$shouldCache = true; DomainTenantResolver::$shouldCache = true;
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
DB::flushQueryLog(); DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
$this->assertEmpty(DB::getQueryLog()); // empty expect(DB::getQueryLog())->toBeEmpty(); // empty
}); });
test('cache is invalidated when the tenant is updated', function () { test('cache is invalidated when the tenant is updated', function () {
@ -55,17 +55,17 @@ test('cache is invalidated when the tenant is updated', function () {
DomainTenantResolver::$shouldCache = true; DomainTenantResolver::$shouldCache = true;
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
DB::flushQueryLog(); DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
$this->assertEmpty(DB::getQueryLog()); // empty expect(DB::getQueryLog())->toBeEmpty(); // empty
$tenant->update([ $tenant->update([
'foo' => 'bar', 'foo' => 'bar',
]); ]);
DB::flushQueryLog(); DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
$this->assertNotEmpty(DB::getQueryLog()); // not empty $this->assertNotEmpty(DB::getQueryLog()); // not empty
}); });
@ -79,20 +79,20 @@ test('cache is invalidated when a tenants domain is changed', function () {
DomainTenantResolver::$shouldCache = true; DomainTenantResolver::$shouldCache = true;
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
DB::flushQueryLog(); DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
$this->assertEmpty(DB::getQueryLog()); // empty expect(DB::getQueryLog())->toBeEmpty(); // empty
$tenant->createDomain([ $tenant->createDomain([
'domain' => 'bar', 'domain' => 'bar',
]); ]);
DB::flushQueryLog(); DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('acme'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('acme')))->toBeTrue();
$this->assertNotEmpty(DB::getQueryLog()); // not empty $this->assertNotEmpty(DB::getQueryLog()); // not empty
DB::flushQueryLog(); DB::flushQueryLog();
$this->assertTrue($tenant->is(app(DomainTenantResolver::class)->resolve('bar'))); expect($tenant->is(app(DomainTenantResolver::class)->resolve('bar')))->toBeTrue();
$this->assertNotEmpty(DB::getQueryLog()); // not empty $this->assertNotEmpty(DB::getQueryLog()); // not empty
}); });

View file

@ -32,14 +32,14 @@ test('tenant can be identified by subdomain', function () {
'domain' => 'foo', 'domain' => 'foo',
]); ]);
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this $this
->get('http://foo.localhost/foo/abc/xyz') ->get('http://foo.localhost/foo/abc/xyz')
->assertSee('abc + xyz'); ->assertSee('abc + xyz');
$this->assertTrue(tenancy()->initialized); expect(tenancy()->initialized)->toBeTrue();
$this->assertSame('acme', tenant('id')); expect(tenant('id'))->toBe('acme');
}); });
test('tenant can be identified by domain', function () { test('tenant can be identified by domain', function () {
@ -53,12 +53,12 @@ test('tenant can be identified by domain', function () {
'domain' => 'foobar.localhost', 'domain' => 'foobar.localhost',
]); ]);
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this $this
->get('http://foobar.localhost/foo/abc/xyz') ->get('http://foobar.localhost/foo/abc/xyz')
->assertSee('abc + xyz'); ->assertSee('abc + xyz');
$this->assertTrue(tenancy()->initialized); expect(tenancy()->initialized)->toBeTrue();
$this->assertSame('acme', tenant('id')); expect(tenant('id'))->toBe('acme');
}); });

View file

@ -40,27 +40,27 @@ afterEach(function () {
}); });
test('migrate command doesnt change the db connection', function () { test('migrate command doesnt change the db connection', function () {
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
$old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); $old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
Artisan::call('tenants:migrate'); Artisan::call('tenants:migrate');
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName(); $new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
$this->assertEquals($old_connection_name, $new_connection_name); expect($new_connection_name)->toEqual($old_connection_name);
$this->assertNotEquals('tenant', $new_connection_name); $this->assertNotEquals('tenant', $new_connection_name);
}); });
test('migrate command works without options', function () { test('migrate command works without options', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
Artisan::call('tenants:migrate'); Artisan::call('tenants:migrate');
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertTrue(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeTrue();
}); });
test('migrate command works with tenants option', function () { test('migrate command works with tenants option', function () {
@ -69,30 +69,30 @@ test('migrate command works with tenants option', function () {
'--tenants' => [$tenant['id']], '--tenants' => [$tenant['id']],
]); ]);
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize(Tenant::create()); tenancy()->initialize(Tenant::create());
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertTrue(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeTrue();
}); });
test('migrate command loads schema state', function () { test('migrate command loads schema state', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$this->assertFalse(Schema::hasTable('schema_users')); expect(Schema::hasTable('schema_users'))->toBeFalse();
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
Artisan::call('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"'); Artisan::call('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"');
$this->assertFalse(Schema::hasTable('schema_users')); expect(Schema::hasTable('schema_users'))->toBeFalse();
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
// Check for both tables to see if missing migrations also get executed // Check for both tables to see if missing migrations also get executed
$this->assertTrue(Schema::hasTable('schema_users')); expect(Schema::hasTable('schema_users'))->toBeTrue();
$this->assertTrue(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeTrue();
}); });
test('dump command works', function () { test('dump command works', function () {
@ -102,19 +102,19 @@ test('dump command works', function () {
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
Artisan::call('tenants:dump --path="tests/Etc/tenant-schema-test.dump"'); Artisan::call('tenants:dump --path="tests/Etc/tenant-schema-test.dump"');
$this->assertFileExists('tests/Etc/tenant-schema-test.dump'); expect('tests/Etc/tenant-schema-test.dump')->toBeFile();
}); });
test('rollback command works', function () { test('rollback command works', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
Artisan::call('tenants:migrate'); Artisan::call('tenants:migrate');
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertTrue(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeTrue();
Artisan::call('tenants:rollback'); Artisan::call('tenants:rollback');
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
}); });
test('seed command works', function () { test('seed command works', function () {
@ -125,16 +125,16 @@ test('database connection is switched to default', function () {
$originalDBName = DB::connection()->getDatabaseName(); $originalDBName = DB::connection()->getDatabaseName();
Artisan::call('tenants:migrate'); Artisan::call('tenants:migrate');
$this->assertSame($originalDBName, DB::connection()->getDatabaseName()); expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]); Artisan::call('tenants:seed', ['--class' => ExampleSeeder::class]);
$this->assertSame($originalDBName, DB::connection()->getDatabaseName()); expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
Artisan::call('tenants:rollback'); Artisan::call('tenants:rollback');
$this->assertSame($originalDBName, DB::connection()->getDatabaseName()); expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
$this->run_commands_works(); $this->run_commands_works();
$this->assertSame($originalDBName, DB::connection()->getDatabaseName()); expect(DB::connection()->getDatabaseName())->toBe($originalDBName);
}); });
test('database connection is switched to default when tenancy has been initialized', function () { test('database connection is switched to default when tenancy has been initialized', function () {
@ -163,32 +163,32 @@ test('install command works', function () {
} }
$this->artisan('tenancy:install'); $this->artisan('tenancy:install');
$this->assertFileExists(base_path('routes/tenant.php')); expect(base_path('routes/tenant.php'))->toBeFile();
$this->assertFileExists(base_path('config/tenancy.php')); expect(base_path('config/tenancy.php'))->toBeFile();
$this->assertFileExists(app_path('Providers/TenancyServiceProvider.php')); expect(app_path('Providers/TenancyServiceProvider.php'))->toBeFile();
$this->assertFileExists(database_path('migrations/2019_09_15_000010_create_tenants_table.php')); expect(database_path('migrations/2019_09_15_000010_create_tenants_table.php'))->toBeFile();
$this->assertFileExists(database_path('migrations/2019_09_15_000020_create_domains_table.php')); expect(database_path('migrations/2019_09_15_000020_create_domains_table.php'))->toBeFile();
$this->assertDirectoryExists(database_path('migrations/tenant')); expect(database_path('migrations/tenant'))->toBeDirectory();
}); });
test('migrate fresh command works', function () { test('migrate fresh command works', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
Artisan::call('tenants:migrate-fresh'); Artisan::call('tenants:migrate-fresh');
$this->assertFalse(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeFalse();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertTrue(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeTrue();
$this->assertFalse(DB::table('users')->exists()); expect(DB::table('users')->exists())->toBeFalse();
DB::table('users')->insert(['name' => 'xxx', 'password' => bcrypt('password'), 'email' => 'foo@bar.xxx']); DB::table('users')->insert(['name' => 'xxx', 'password' => bcrypt('password'), 'email' => 'foo@bar.xxx']);
$this->assertTrue(DB::table('users')->exists()); expect(DB::table('users')->exists())->toBeTrue();
// test that db is wiped // test that db is wiped
Artisan::call('tenants:migrate-fresh'); Artisan::call('tenants:migrate-fresh');
$this->assertFalse(DB::table('users')->exists()); expect(DB::table('users')->exists())->toBeFalse();
}); });
test('run command with array of tenants works', function () { test('run command with array of tenants works', function () {

View file

@ -29,7 +29,7 @@ test('database can be created after tenant creation', function () {
$manager = app(MySQLDatabaseManager::class); $manager = app(MySQLDatabaseManager::class);
$manager->setConnection('mysql'); $manager->setConnection('mysql');
$this->assertTrue($manager->databaseExists($tenant->database()->getName())); expect($manager->databaseExists($tenant->database()->getName()))->toBeTrue();
}); });
test('database can be migrated after tenant creation', function () { test('database can be migrated after tenant creation', function () {
@ -43,7 +43,7 @@ test('database can be migrated after tenant creation', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenant->run(function () { $tenant->run(function () {
$this->assertTrue(Schema::hasTable('users')); expect(Schema::hasTable('users'))->toBeTrue();
}); });
}); });
@ -63,7 +63,7 @@ test('database can be seeded after tenant creation', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenant->run(function () { $tenant->run(function () {
$this->assertSame('Seeded User', User::first()->name); expect(User::first()->name)->toBe('Seeded User');
}); });
}); });
@ -84,7 +84,7 @@ test('custom job can be added to the pipeline', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenant->run(function () { $tenant->run(function () {
$this->assertSame('Foo', User::all()[1]->name); expect(User::all()[1]->name)->toBe('Foo');
}); });
}); });

View file

@ -40,11 +40,11 @@ test('users are created when permission controlled mysql manager is used', funct
/** @var ManagesDatabaseUsers $manager */ /** @var ManagesDatabaseUsers $manager */
$manager = $tenant->database()->manager(); $manager = $tenant->database()->manager();
$this->assertFalse($manager->userExists($tenant->database()->getUsername())); expect($manager->userExists($tenant->database()->getUsername()))->toBeFalse();
$tenant->save(); $tenant->save();
$this->assertTrue($manager->userExists($tenant->database()->getUsername())); expect($manager->userExists($tenant->database()->getUsername()))->toBeTrue();
}); });
test('a tenants database cannot be created when the user already exists', function () { test('a tenants database cannot be created when the user already exists', function () {
@ -55,8 +55,8 @@ test('a tenants database cannot be created when the user already exists', functi
/** @var ManagesDatabaseUsers $manager */ /** @var ManagesDatabaseUsers $manager */
$manager = $tenant->database()->manager(); $manager = $tenant->database()->manager();
$this->assertTrue($manager->userExists($tenant->database()->getUsername())); expect($manager->userExists($tenant->database()->getUsername()))->toBeTrue();
$this->assertTrue($manager->databaseExists($tenant->database()->getName())); expect($manager->databaseExists($tenant->database()->getName()))->toBeTrue();
$this->expectException(TenantDatabaseUserAlreadyExistsException::class); $this->expectException(TenantDatabaseUserAlreadyExistsException::class);
Event::fake([DatabaseCreated::class]); Event::fake([DatabaseCreated::class]);
@ -69,7 +69,7 @@ test('a tenants database cannot be created when the user already exists', functi
$manager2 = $tenant2->database()->manager(); $manager2 = $tenant2->database()->manager();
// database was not created because of DB transaction // database was not created because of DB transaction
$this->assertFalse($manager2->databaseExists($tenant2->database()->getName())); expect($manager2->databaseExists($tenant2->database()->getName()))->toBeFalse();
Event::assertNotDispatched(DatabaseCreated::class); Event::assertNotDispatched(DatabaseCreated::class);
}); });
@ -83,7 +83,7 @@ test('correct grants are given to users', function () {
]); ]);
$query = DB::connection('mysql')->select("SHOW GRANTS FOR `{$tenant->database()->getUsername()}`@`%`")[1]; $query = DB::connection('mysql')->select("SHOW GRANTS FOR `{$tenant->database()->getUsername()}`@`%`")[1];
$this->assertStringStartsWith('GRANT CREATE, ALTER, ALTER ROUTINE ON', $query->{"Grants for {$user}@%"}); // @mysql because that's the hostname within the docker network expect($query->{"Grants for {$user}@%"})->toStartWith('GRANT CREATE, ALTER, ALTER ROUTINE ON'); // @mysql because that's the hostname within the docker network
}); });
test('having existing databases without users and switching to permission controlled mysql manager doesnt break existing dbs', function () { test('having existing databases without users and switching to permission controlled mysql manager doesnt break existing dbs', function () {
@ -102,7 +102,7 @@ test('having existing databases without users and switching to permission contro
'id' => 'foo' . Str::random(10), 'id' => 'foo' . Str::random(10),
]); ]);
$this->assertTrue($tenant->database()->manager() instanceof MySQLDatabaseManager); expect($tenant->database()->manager() instanceof MySQLDatabaseManager)->toBeTrue();
tenancy()->initialize($tenant); // check if everything works tenancy()->initialize($tenant); // check if everything works
tenancy()->end(); tenancy()->end();
@ -111,6 +111,6 @@ test('having existing databases without users and switching to permission contro
tenancy()->initialize($tenant); // check if everything works tenancy()->initialize($tenant); // check if everything works
$this->assertTrue($tenant->database()->manager() instanceof PermissionControlledMySQLDatabaseManager); expect($tenant->database()->manager() instanceof PermissionControlledMySQLDatabaseManager)->toBeTrue();
$this->assertSame('root', config('database.connections.tenant.username')); expect(config('database.connections.tenant.username'))->toBe('root');
}); });

View file

@ -36,8 +36,8 @@ test('tenant can be identified using hostname', function () {
$resolvedTenant = app(DomainTenantResolver::class)->resolve('foo.localhost'); $resolvedTenant = app(DomainTenantResolver::class)->resolve('foo.localhost');
$this->assertSame($id, $resolvedTenant->id); expect($resolvedTenant->id)->toBe($id);
$this->assertSame(['foo.localhost'], $resolvedTenant->domains->pluck('domain')->toArray()); expect($resolvedTenant->domains->pluck('domain')->toArray())->toBe(['foo.localhost']);
}); });
test('a domain can belong to only one tenant', function () { test('a domain can belong to only one tenant', function () {
@ -70,14 +70,14 @@ test('tenant can be identified by domain', function () {
'domain' => 'foo.localhost', 'domain' => 'foo.localhost',
]); ]);
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this $this
->get('http://foo.localhost/foo/abc/xyz') ->get('http://foo.localhost/foo/abc/xyz')
->assertSee('abc + xyz'); ->assertSee('abc + xyz');
$this->assertTrue(tenancy()->initialized); expect(tenancy()->initialized)->toBeTrue();
$this->assertSame('acme', tenant('id')); expect(tenant('id'))->toBe('acme');
}); });
test('onfail logic can be customized', function () { test('onfail logic can be customized', function () {
@ -97,5 +97,5 @@ test('domains are always lowercase', function () {
'domain' => 'CAPITALS', 'domain' => 'CAPITALS',
]); ]);
$this->assertSame('capitals', Domain::first()->domain); expect(Domain::first()->domain)->toBe('capitals');
}); });

View file

@ -31,7 +31,7 @@ test('listeners can be synchronous', function () {
Queue::assertNothingPushed(); Queue::assertNothingPushed();
$this->assertSame('bar', app('foo')); expect(app('foo'))->toBe('bar');
}); });
test('listeners can be queued by setting a static property', function () { test('listeners can be queued by setting a static property', function () {
@ -46,7 +46,7 @@ test('listeners can be queued by setting a static property', function () {
return $job->class === FooListener::class; return $job->class === FooListener::class;
}); });
$this->assertFalse(app()->bound('foo')); expect(app()->bound('foo'))->toBeFalse();
}); });
test('ing events can be used to cancel tenant model actions', function () { test('ing events can be used to cancel tenant model actions', function () {
@ -54,8 +54,8 @@ test('ing events can be used to cancel tenant model actions', function () {
return false; return false;
}); });
$this->assertSame(false, Tenant::create()->exists); expect(Tenant::create()->exists)->toBe(false);
$this->assertSame(0, Tenant::count()); expect(Tenant::count())->toBe(0);
}); });
test('ing events can be used to cancel domain model actions', function () { test('ing events can be used to cancel domain model actions', function () {
@ -73,7 +73,7 @@ test('ing events can be used to cancel domain model actions', function () {
'domain' => 'foo', 'domain' => 'foo',
]); ]);
$this->assertSame('acme', $domain->refresh()->domain); expect($domain->refresh()->domain)->toBe('acme');
}); });
test('ing events can be used to cancel db creation', function () { test('ing events can be used to cancel db creation', function () {
@ -112,7 +112,7 @@ test('ing events can be used to cancel tenancy bootstrapping', function () {
tenancy()->initialize(Tenant::create()); tenancy()->initialize(Tenant::create());
$this->assertSame([DatabaseTenancyBootstrapper::class], array_map('get_class', tenancy()->getBootstrappers())); expect(array_map('get_class', tenancy()->getBootstrappers()))->toBe([DatabaseTenancyBootstrapper::class]);
}); });
test('individual job pipelines can terminate while leaving others running', function () { test('individual job pipelines can terminate while leaving others running', function () {
@ -179,7 +179,7 @@ test('database is not migrated if creation is disabled', function () {
'tenancy_db_name' => 'already_created', 'tenancy_db_name' => 'already_created',
]); ]);
$this->assertFalse($this->hasFailed()); expect($this->hasFailed())->toBeFalse();
}); });
// Helpers // Helpers

View file

@ -34,6 +34,6 @@ test('tenant route helper generates correct url', function () {
return 'Foo'; return 'Foo';
})->name('foo'); })->name('foo');
$this->assertSame('http://foo.localhost/abcdef/as/df', tenant_route('foo.localhost', 'foo', ['a' => 'as', 'b' => 'df'])); expect(tenant_route('foo.localhost', 'foo', ['a' => 'as', 'b' => 'df']))->toBe('http://foo.localhost/abcdef/as/df');
$this->assertSame('http://foo.localhost/abcdef', tenant_route('foo.localhost', 'foo', [])); expect(tenant_route('foo.localhost', 'foo', []))->toBe('http://foo.localhost/abcdef');
}); });

View file

@ -18,7 +18,7 @@ afterEach(function () {
}); });
test('config is merged and removed', function () { test('config is merged and removed', function () {
$this->assertSame(null, config('services.paypal')); expect(config('services.paypal'))->toBe(null);
config([ config([
'tenancy.features' => [TenantConfig::class], 'tenancy.features' => [TenantConfig::class],
'tenancy.bootstrappers' => [], 'tenancy.bootstrappers' => [],
@ -37,7 +37,7 @@ test('config is merged and removed', function () {
]); ]);
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame(['public' => 'foo', 'private' => 'bar'], config('services.paypal')); expect(config('services.paypal'))->toBe(['public' => 'foo', 'private' => 'bar']);
tenancy()->end(); tenancy()->end();
$this->assertSame([ $this->assertSame([
@ -47,7 +47,7 @@ test('config is merged and removed', function () {
}); });
test('the value can be set to multiple config keys', function () { test('the value can be set to multiple config keys', function () {
$this->assertSame(null, config('services.paypal')); expect(config('services.paypal'))->toBe(null);
config([ config([
'tenancy.features' => [TenantConfig::class], 'tenancy.features' => [TenantConfig::class],
'tenancy.bootstrappers' => [], 'tenancy.bootstrappers' => [],

View file

@ -23,31 +23,31 @@ beforeEach(function () {
}); });
test('global cache manager stores data in global cache', function () { test('global cache manager stores data in global cache', function () {
$this->assertSame(null, cache('foo')); expect(cache('foo'))->toBe(null);
GlobalCache::put(['foo' => 'bar'], 1); GlobalCache::put(['foo' => 'bar'], 1);
$this->assertSame('bar', GlobalCache::get('foo')); expect(GlobalCache::get('foo'))->toBe('bar');
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
$this->assertSame('bar', GlobalCache::get('foo')); expect(GlobalCache::get('foo'))->toBe('bar');
GlobalCache::put(['abc' => 'xyz'], 1); GlobalCache::put(['abc' => 'xyz'], 1);
cache(['def' => 'ghi'], 10); cache(['def' => 'ghi'], 10);
$this->assertSame('ghi', cache('def')); expect(cache('def'))->toBe('ghi');
tenancy()->end(); tenancy()->end();
$this->assertSame('xyz', GlobalCache::get('abc')); expect(GlobalCache::get('abc'))->toBe('xyz');
$this->assertSame('bar', GlobalCache::get('foo')); expect(GlobalCache::get('foo'))->toBe('bar');
$this->assertSame(null, cache('def')); expect(cache('def'))->toBe(null);
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
$this->assertSame('xyz', GlobalCache::get('abc')); expect(GlobalCache::get('abc'))->toBe('xyz');
$this->assertSame('bar', GlobalCache::get('foo')); expect(GlobalCache::get('foo'))->toBe('bar');
$this->assertSame(null, cache('def')); expect(cache('def'))->toBe(null);
cache(['def' => 'xxx'], 1); cache(['def' => 'xxx'], 1);
$this->assertSame('xxx', cache('def')); expect(cache('def'))->toBe('xxx');
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
$this->assertSame('ghi', cache('def')); expect(cache('def'))->toBe('ghi');
}); });

View file

@ -34,12 +34,12 @@ test('tenant can be identified by path', function () {
'id' => 'acme', 'id' => 'acme',
]); ]);
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this->get('/acme/foo/abc/xyz'); $this->get('/acme/foo/abc/xyz');
$this->assertTrue(tenancy()->initialized); expect(tenancy()->initialized)->toBeTrue();
$this->assertSame('acme', tenant('id')); expect(tenant('id'))->toBe('acme');
}); });
test('route actions dont get the tenant id', function () { test('route actions dont get the tenant id', function () {
@ -47,14 +47,14 @@ test('route actions dont get the tenant id', function () {
'id' => 'acme', 'id' => 'acme',
]); ]);
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this $this
->get('/acme/foo/abc/xyz') ->get('/acme/foo/abc/xyz')
->assertContent('abc + xyz'); ->assertContent('abc + xyz');
$this->assertTrue(tenancy()->initialized); expect(tenancy()->initialized)->toBeTrue();
$this->assertSame('acme', tenant('id')); expect(tenant('id'))->toBe('acme');
}); });
test('exception is thrown when tenant cannot be identified by path', function () { test('exception is thrown when tenant cannot be identified by path', function () {
@ -64,7 +64,7 @@ test('exception is thrown when tenant cannot be identified by path', function ()
->withoutExceptionHandling() ->withoutExceptionHandling()
->get('/acme/foo/abc/xyz'); ->get('/acme/foo/abc/xyz');
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
}); });
test('onfail logic can be customized', function () { test('onfail logic can be customized', function () {

View file

@ -104,7 +104,7 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
dispatch(new TestJob($this->valuestore, $user)); dispatch(new TestJob($this->valuestore, $user));
$this->assertFalse($this->valuestore->has('tenant_id')); expect($this->valuestore->has('tenant_id'))->toBeFalse();
if ($shouldEndTenancy) { if ($shouldEndTenancy) {
tenancy()->end(); tenancy()->end();
@ -112,12 +112,12 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
$this->artisan('queue:work --once'); $this->artisan('queue:work --once');
$this->assertSame(0, DB::connection('central')->table('failed_jobs')->count()); expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);
$this->assertSame('The current tenant id is: ' . $tenant->id, $this->valuestore->get('tenant_id')); expect($this->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id);
$tenant->run(function () use ($user) { $tenant->run(function () use ($user) {
$this->assertSame('Bar', $user->fresh()->name); expect($user->fresh()->name)->toBe('Bar');
}); });
}); });
@ -147,7 +147,7 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
dispatch(new TestJob($this->valuestore, $user)); dispatch(new TestJob($this->valuestore, $user));
$this->assertFalse($this->valuestore->has('tenant_id')); expect($this->valuestore->has('tenant_id'))->toBeFalse();
if ($shouldEndTenancy) { if ($shouldEndTenancy) {
tenancy()->end(); tenancy()->end();
@ -155,18 +155,18 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
$this->artisan('queue:work --once'); $this->artisan('queue:work --once');
$this->assertSame(1, DB::connection('central')->table('failed_jobs')->count()); expect(DB::connection('central')->table('failed_jobs')->count())->toBe(1);
$this->assertNull($this->valuestore->get('tenant_id')); // job failed expect($this->valuestore->get('tenant_id'))->toBeNull(); // job failed
$this->artisan('queue:retry all'); $this->artisan('queue:retry all');
$this->artisan('queue:work --once'); $this->artisan('queue:work --once');
$this->assertSame(0, DB::connection('central')->table('failed_jobs')->count()); expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);
$this->assertSame('The current tenant id is: ' . $tenant->id, $this->valuestore->get('tenant_id')); // job succeeded expect($this->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id); // job succeeded
$tenant->run(function () use ($user) { $tenant->run(function () use ($user) {
$this->assertSame('Bar', $user->fresh()->name); expect($user->fresh()->name)->toBe('Bar');
}); });
}); });
@ -185,10 +185,10 @@ test('the tenant used by the job doesnt change when the current tenant changes',
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
$this->assertFalse($this->valuestore->has('tenant_id')); expect($this->valuestore->has('tenant_id'))->toBeFalse();
$this->artisan('queue:work --once'); $this->artisan('queue:work --once');
$this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id')); expect($this->valuestore->get('tenant_id'))->toBe('The current tenant id is: acme');
}); });
// Helpers // Helpers

View file

@ -130,7 +130,7 @@ test('only the synced columns are updated in the central db', function () {
test('creating the resource in tenant database creates it in central database and creates the mapping', function () { test('creating the resource in tenant database creates it in central database and creates the mapping', function () {
// Assert no user in central DB // Assert no user in central DB
$this->assertCount(0, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(0);
$tenant = ResourceTenant::create(); $tenant = ResourceTenant::create();
migrateTenants(); migrateTenants();
@ -149,23 +149,23 @@ test('creating the resource in tenant database creates it in central database an
tenancy()->end(); tenancy()->end();
// Asset user was created // Asset user was created
$this->assertSame('acme', CentralUser::first()->global_id); expect(CentralUser::first()->global_id)->toBe('acme');
$this->assertSame('commenter', CentralUser::first()->role); expect(CentralUser::first()->role)->toBe('commenter');
// Assert mapping was created // Assert mapping was created
$this->assertCount(1, CentralUser::first()->tenants); expect(CentralUser::first()->tenants)->toHaveCount(1);
// Assert role change doesn't cascade // Assert role change doesn't cascade
CentralUser::first()->update(['role' => 'central superadmin']); CentralUser::first()->update(['role' => 'central superadmin']);
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame('commenter', ResourceUser::first()->role); expect(ResourceUser::first()->role)->toBe('commenter');
}); });
test('trying to update synced resources from central context using tenant models results in an exception', function () { test('trying to update synced resources from central context using tenant models results in an exception', function () {
$this->creating_the_resource_in_tenant_database_creates_it_in_central_database_and_creates_the_mapping(); $this->creating_the_resource_in_tenant_database_creates_it_in_central_database_and_creates_the_mapping();
tenancy()->end(); tenancy()->end();
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this->expectException(ModelNotSyncMasterException::class); $this->expectException(ModelNotSyncMasterException::class);
ResourceUser::first()->update(['role' => 'foobar']); ResourceUser::first()->update(['role' => 'foobar']);
@ -186,13 +186,13 @@ test('attaching a tenant to the central resource triggers a pull from the tenant
migrateTenants(); migrateTenants();
$tenant->run(function () { $tenant->run(function () {
$this->assertCount(0, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(0);
}); });
$centralUser->tenants()->attach('t1'); $centralUser->tenants()->attach('t1');
$tenant->run(function () { $tenant->run(function () {
$this->assertCount(1, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(1);
}); });
}); });
@ -211,7 +211,7 @@ test('attaching users to tenants d o e s n o t d o a n y t h i n g', function ()
migrateTenants(); migrateTenants();
$tenant->run(function () { $tenant->run(function () {
$this->assertCount(0, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(0);
}); });
// The child model is inaccessible in the Pivot Model, so we can't fire any events. // The child model is inaccessible in the Pivot Model, so we can't fire any events.
@ -219,7 +219,7 @@ test('attaching users to tenants d o e s n o t d o a n y t h i n g', function ()
$tenant->run(function () { $tenant->run(function () {
// Still zero // Still zero
$this->assertCount(0, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(0);
}); });
}); });
@ -251,17 +251,17 @@ test('resources are synced only to workspaces that have the resource', function
$t1->run(function () { $t1->run(function () {
// assert user exists // assert user exists
$this->assertCount(1, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(1);
}); });
$t2->run(function () { $t2->run(function () {
// assert user exists // assert user exists
$this->assertCount(1, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(1);
}); });
$t3->run(function () { $t3->run(function () {
// assert user does NOT exist // assert user does NOT exist
$this->assertCount(0, ResourceUser::all()); expect(ResourceUser::all())->toHaveCount(0);
}); });
}); });
@ -298,15 +298,15 @@ test('when a resource exists in other tenant dbs but is c r e a t e d in a tenan
}); });
$centralUser = CentralUser::first(); $centralUser = CentralUser::first();
$this->assertSame('John Foo', $centralUser->name); // name changed expect($centralUser->name)->toBe('John Foo'); // name changed
$this->assertSame('john@foo', $centralUser->email); // email changed expect($centralUser->email)->toBe('john@foo'); // email changed
$this->assertSame('commenter', $centralUser->role); // role didn't change expect($centralUser->role)->toBe('commenter'); // role didn't change
$t1->run(function () { $t1->run(function () {
$user = ResourceUser::first(); $user = ResourceUser::first();
$this->assertSame('John Foo', $user->name); // name changed expect($user->name)->toBe('John Foo'); // name changed
$this->assertSame('john@foo', $user->email); // email changed expect($user->email)->toBe('john@foo'); // email changed
$this->assertSame('commenter', $user->role); // role didn't change, i.e. is the same as from the original copy from central expect($user->role)->toBe('commenter'); // role didn't change, i.e. is the same as from the original copy from central
}); });
}); });
@ -342,23 +342,23 @@ test('the synced columns are updated in other tenant dbs where the resource exis
'role' => 'employee', // unsynced 'role' => 'employee', // unsynced
]); ]);
$this->assertSame('employee', ResourceUser::first()->role); expect(ResourceUser::first()->role)->toBe('employee');
}); });
// Check that change was cascaded to other tenants // Check that change was cascaded to other tenants
$t1->run($check = function () { $t1->run($check = function () {
$user = ResourceUser::first(); $user = ResourceUser::first();
$this->assertSame('John 3', $user->name); // synced expect($user->name)->toBe('John 3'); // synced
$this->assertSame('commenter', $user->role); // unsynced expect($user->role)->toBe('commenter'); // unsynced
}); });
$t2->run($check); $t2->run($check);
// Check that change bubbled up to central DB // Check that change bubbled up to central DB
$this->assertSame(1, CentralUser::count()); expect(CentralUser::count())->toBe(1);
$centralUser = CentralUser::first(); $centralUser = CentralUser::first();
$this->assertSame('John 3', $centralUser->name); // synced expect($centralUser->name)->toBe('John 3'); // synced
$this->assertSame('commenter', $centralUser->role); // unsynced expect($centralUser->role)->toBe('commenter'); // unsynced
}); });
test('global id is generated using id generator when its not supplied', function () { test('global id is generated using id generator when its not supplied', function () {
@ -389,7 +389,7 @@ test('when the resource doesnt exist in the tenant db non synced columns will ca
$centralUser->tenants()->attach('t1'); $centralUser->tenants()->attach('t1');
$t1->run(function () { $t1->run(function () {
$this->assertSame('employee', ResourceUser::first()->role); expect(ResourceUser::first()->role)->toBe('employee');
}); });
}); });
@ -409,7 +409,7 @@ test('when the resource doesnt exist in the central db non synced columns will b
]); ]);
}); });
$this->assertSame('employee', CentralUser::first()->role); expect(CentralUser::first()->role)->toBe('employee');
}); });
test('the listener can be queued', function () { test('the listener can be queued', function () {
@ -491,7 +491,7 @@ test('an event is fired for all touched resources', function () {
'role' => 'employee', // unsynced 'role' => 'employee', // unsynced
]); ]);
$this->assertSame('employee', ResourceUser::first()->role); expect(ResourceUser::first()->role)->toBe('employee');
}); });
Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) { Event::assertDispatched(SyncedResourceChangedInForeignDatabase::class, function (SyncedResourceChangedInForeignDatabase $event) {

View file

@ -46,13 +46,13 @@ test('primary models are scoped to the current tenant', function () {
$post = Post::create(['text' => 'Foo']); $post = Post::create(['text' => 'Foo']);
$this->assertSame('acme', $post->tenant_id); expect($post->tenant_id)->toBe('acme');
$this->assertSame('acme', $post->tenant->id); expect($post->tenant->id)->toBe('acme');
$post = Post::first(); $post = Post::first();
$this->assertSame('acme', $post->tenant_id); expect($post->tenant_id)->toBe('acme');
$this->assertSame('acme', $post->tenant->id); expect($post->tenant->id)->toBe('acme');
// ====================================== // ======================================
// foobar context // foobar context
@ -62,13 +62,13 @@ test('primary models are scoped to the current tenant', function () {
$post = Post::create(['text' => 'Bar']); $post = Post::create(['text' => 'Bar']);
$this->assertSame('foobar', $post->tenant_id); expect($post->tenant_id)->toBe('foobar');
$this->assertSame('foobar', $post->tenant->id); expect($post->tenant->id)->toBe('foobar');
$post = Post::first(); $post = Post::first();
$this->assertSame('foobar', $post->tenant_id); expect($post->tenant_id)->toBe('foobar');
$this->assertSame('foobar', $post->tenant->id); expect($post->tenant->id)->toBe('foobar');
// ====================================== // ======================================
// acme context again // acme context again
@ -76,11 +76,11 @@ test('primary models are scoped to the current tenant', function () {
tenancy()->initialize($acme); tenancy()->initialize($acme);
$post = Post::first(); $post = Post::first();
$this->assertSame('acme', $post->tenant_id); expect($post->tenant_id)->toBe('acme');
$this->assertSame('acme', $post->tenant->id); expect($post->tenant->id)->toBe('acme');
// Assert foobar models are inaccessible in acme context // Assert foobar models are inaccessible in acme context
$this->assertSame(1, Post::count()); expect(Post::count())->toBe(1);
}); });
test('primary models are not scoped in the central context', function () { test('primary models are not scoped in the central context', function () {
@ -88,7 +88,7 @@ test('primary models are not scoped in the central context', function () {
tenancy()->end(); tenancy()->end();
$this->assertSame(2, Post::count()); expect(Post::count())->toBe(2);
}); });
test('secondary models are scoped to the current tenant when accessed via primary model', function () { test('secondary models are scoped to the current tenant when accessed via primary model', function () {
@ -112,17 +112,17 @@ test('secondary models are scoped to the current tenant when accessed via primar
// ================ // ================
// acme context again // acme context again
tenancy()->initialize($acme); tenancy()->initialize($acme);
$this->assertSame(1, Post::count()); expect(Post::count())->toBe(1);
$this->assertSame(1, Post::first()->comments->count()); expect(Post::first()->comments->count())->toBe(1);
}); });
test('secondary models are n o t scoped to the current tenant when accessed directly', function () { test('secondary models are n o t scoped to the current tenant when accessed directly', function () {
$this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model(); $this->secondary_models_are_scoped_to_the_current_tenant_when_accessed_via_primary_model();
// We're in acme context // We're in acme context
$this->assertSame('acme', tenant('id')); expect(tenant('id'))->toBe('acme');
$this->assertSame(2, Comment::count()); expect(Comment::count())->toBe(2);
}); });
test('secondary models a r e scoped to the current tenant when accessed directly a n d p a r e n t r e l a t i o n s h i p t r a i t i s u s e d', function () { test('secondary models a r e scoped to the current tenant when accessed directly a n d p a r e n t r e l a t i o n s h i p t r a i t i s u s e d', function () {
@ -134,8 +134,8 @@ test('secondary models a r e scoped to the current tenant when accessed directly
$post = Post::create(['text' => 'Foo']); $post = Post::create(['text' => 'Foo']);
$post->scoped_comments()->create(['text' => 'Comment Text']); $post->scoped_comments()->create(['text' => 'Comment Text']);
$this->assertSame(1, Post::count()); expect(Post::count())->toBe(1);
$this->assertSame(1, ScopedComment::count()); expect(ScopedComment::count())->toBe(1);
}); });
$foobar = Tenant::create([ $foobar = Tenant::create([
@ -143,18 +143,18 @@ test('secondary models a r e scoped to the current tenant when accessed directly
]); ]);
$foobar->run(function () { $foobar->run(function () {
$this->assertSame(0, Post::count()); expect(Post::count())->toBe(0);
$this->assertSame(0, ScopedComment::count()); expect(ScopedComment::count())->toBe(0);
$post = Post::create(['text' => 'Bar']); $post = Post::create(['text' => 'Bar']);
$post->scoped_comments()->create(['text' => 'Comment Text 2']); $post->scoped_comments()->create(['text' => 'Comment Text 2']);
$this->assertSame(1, Post::count()); expect(Post::count())->toBe(1);
$this->assertSame(1, ScopedComment::count()); expect(ScopedComment::count())->toBe(1);
}); });
// Global context // Global context
$this->assertSame(2, ScopedComment::count()); expect(ScopedComment::count())->toBe(2);
}); });
test('secondary models are n o t scoped in the central context', function () { test('secondary models are n o t scoped in the central context', function () {
@ -162,7 +162,7 @@ test('secondary models are n o t scoped in the central context', function () {
tenancy()->end(); tenancy()->end();
$this->assertSame(2, Comment::count()); expect(Comment::count())->toBe(2);
}); });
test('global models are not scoped at all', function () { test('global models are not scoped at all', function () {
@ -179,13 +179,13 @@ test('global models are not scoped at all', function () {
]); ]);
$acme->run(function () { $acme->run(function () {
$this->assertSame(2, GlobalResource::count()); expect(GlobalResource::count())->toBe(2);
GlobalResource::create(['text' => 'Third']); GlobalResource::create(['text' => 'Third']);
GlobalResource::create(['text' => 'Fourth']); GlobalResource::create(['text' => 'Fourth']);
}); });
$this->assertSame(4, GlobalResource::count()); expect(GlobalResource::count())->toBe(4);
}); });
test('tenant id and relationship is auto added when creating primary resources in tenant context', function () { test('tenant id and relationship is auto added when creating primary resources in tenant context', function () {
@ -195,10 +195,10 @@ test('tenant id and relationship is auto added when creating primary resources i
$post = Post::create(['text' => 'Foo']); $post = Post::create(['text' => 'Foo']);
$this->assertSame('acme', $post->tenant_id); expect($post->tenant_id)->toBe('acme');
$this->assertTrue($post->relationLoaded('tenant')); expect($post->relationLoaded('tenant'))->toBeTrue();
$this->assertSame($acme, $post->tenant); expect($post->tenant)->toBe($acme);
$this->assertSame(tenant(), $post->tenant); expect($post->tenant)->toBe(tenant());
}); });
test('tenant id is not auto added when creating primary resources in central context', function () { test('tenant id is not auto added when creating primary resources in central context', function () {
@ -227,7 +227,7 @@ test('tenant id column name can be customized', function () {
$post = Post::create(['text' => 'Foo']); $post = Post::create(['text' => 'Foo']);
$this->assertSame('acme', $post->team_id); expect($post->team_id)->toBe('acme');
// ====================================== // ======================================
// foobar context // foobar context
@ -237,11 +237,11 @@ test('tenant id column name can be customized', function () {
$post = Post::create(['text' => 'Bar']); $post = Post::create(['text' => 'Bar']);
$this->assertSame('foobar', $post->team_id); expect($post->team_id)->toBe('foobar');
$post = Post::first(); $post = Post::first();
$this->assertSame('foobar', $post->team_id); expect($post->team_id)->toBe('foobar');
// ====================================== // ======================================
// acme context again // acme context again
@ -249,10 +249,10 @@ test('tenant id column name can be customized', function () {
tenancy()->initialize($acme); tenancy()->initialize($acme);
$post = Post::first(); $post = Post::first();
$this->assertSame('acme', $post->team_id); expect($post->team_id)->toBe('acme');
// Assert foobar models are inaccessible in acme context // Assert foobar models are inaccessible in acme context
$this->assertSame(1, Post::count()); expect(Post::count())->toBe(1);
}); });
test('the model returned by the tenant helper has unique and exists validation rules', function () { test('the model returned by the tenant helper has unique and exists validation rules', function () {
@ -287,8 +287,8 @@ test('the model returned by the tenant helper has unique and exists validation r
])->fails(); ])->fails();
// Assert that tenant()->unique() and tenant()->exists() are scoped // Assert that tenant()->unique() and tenant()->exists() are scoped
$this->assertTrue($uniqueFails); expect($uniqueFails)->toBeTrue();
$this->assertFalse($existsFails); expect($existsFails)->toBeFalse();
}); });
// Helpers // Helpers

View file

@ -34,14 +34,14 @@ test('tenant can be identified by subdomain', function () {
'domain' => 'foo', 'domain' => 'foo',
]); ]);
$this->assertFalse(tenancy()->initialized); expect(tenancy()->initialized)->toBeFalse();
$this $this
->get('http://foo.localhost/foo/abc/xyz') ->get('http://foo.localhost/foo/abc/xyz')
->assertSee('abc + xyz'); ->assertSee('abc + xyz');
$this->assertTrue(tenancy()->initialized); expect(tenancy()->initialized)->toBeTrue();
$this->assertSame('acme', tenant('id')); expect(tenant('id'))->toBe('acme');
}); });
test('onfail logic can be customized', function () { test('onfail logic can be customized', function () {

View file

@ -40,7 +40,7 @@ test('asset can be accessed using the url returned by the tenant asset helper',
// response()->file() returns BinaryFileResponse whose content is // response()->file() returns BinaryFileResponse whose content is
// inaccessible via getContent, so ->assertSee() can't be used // inaccessible via getContent, so ->assertSee() can't be used
$this->assertFileExists($path); expect($path)->toBeFile();
$response = $this->get(tenant_asset($filename), [ $response = $this->get(tenant_asset($filename), [
'X-Tenant' => $tenant->id, 'X-Tenant' => $tenant->id,
]); ]);
@ -51,7 +51,7 @@ test('asset can be accessed using the url returned by the tenant asset helper',
$content = fread($f, filesize($path)); $content = fread($f, filesize($path));
fclose($f); fclose($f);
$this->assertSame('bar', $content); expect($content)->toBe('bar');
}); });
test('asset helper returns a link to tenant asset controller when asset url is null', function () { test('asset helper returns a link to tenant asset controller when asset url is null', function () {
@ -60,7 +60,7 @@ test('asset helper returns a link to tenant asset controller when asset url is n
$tenant = Tenant::create(); $tenant = Tenant::create();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame(route('stancl.tenancy.asset', ['path' => 'foo']), asset('foo')); expect(asset('foo'))->toBe(route('stancl.tenancy.asset', ['path' => 'foo']));
}); });
test('asset helper returns a link to an external url when asset url is not null', function () { test('asset helper returns a link to an external url when asset url is not null', function () {
@ -69,17 +69,17 @@ test('asset helper returns a link to an external url when asset url is not null'
$tenant = Tenant::create(); $tenant = Tenant::create();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame("https://an-s3-bucket/tenant{$tenant->id}/foo", asset('foo')); expect(asset('foo'))->toBe("https://an-s3-bucket/tenant{$tenant->id}/foo");
}); });
test('global asset helper returns the same url regardless of tenancy initialization', function () { test('global asset helper returns the same url regardless of tenancy initialization', function () {
$original = global_asset('foobar'); $original = global_asset('foobar');
$this->assertSame(asset('foobar'), global_asset('foobar')); expect(global_asset('foobar'))->toBe(asset('foobar'));
$tenant = Tenant::create(); $tenant = Tenant::create();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame($original, global_asset('foobar')); expect(global_asset('foobar'))->toBe($original);
}); });
test('asset helper tenancy can be disabled', function () { test('asset helper tenancy can be disabled', function () {
@ -93,7 +93,7 @@ test('asset helper tenancy can be disabled', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertSame($original, asset('foo')); expect(asset('foo'))->toBe($original);
}); });
// Helpers // Helpers

View file

@ -40,20 +40,20 @@ test('databases can be created and deleted', function ($driver, $databaseManager
$manager = app($databaseManager); $manager = app($databaseManager);
$manager->setConnection($driver); $manager->setConnection($driver);
$this->assertFalse($manager->databaseExists($name)); expect($manager->databaseExists($name))->toBeFalse();
$tenant = Tenant::create([ $tenant = Tenant::create([
'tenancy_db_name' => $name, 'tenancy_db_name' => $name,
'tenancy_db_connection' => $driver, 'tenancy_db_connection' => $driver,
]); ]);
$this->assertTrue($manager->databaseExists($name)); expect($manager->databaseExists($name))->toBeTrue();
$manager->deleteDatabase($tenant); $manager->deleteDatabase($tenant);
$this->assertFalse($manager->databaseExists($name)); expect($manager->databaseExists($name))->toBeFalse();
})->with('database_manager_provider'); })->with('database_manager_provider');
test('dbs can be created when another driver is used for the central db', function () { test('dbs can be created when another driver is used for the central db', function () {
$this->assertSame('central', config('database.default')); expect(config('database.default'))->toBe('central');
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant; return $event->tenant;
@ -64,26 +64,26 @@ test('dbs can be created when another driver is used for the central db', functi
$mysqlmanager = app(MySQLDatabaseManager::class); $mysqlmanager = app(MySQLDatabaseManager::class);
$mysqlmanager->setConnection('mysql'); $mysqlmanager->setConnection('mysql');
$this->assertFalse($mysqlmanager->databaseExists($database)); expect($mysqlmanager->databaseExists($database))->toBeFalse();
Tenant::create([ Tenant::create([
'tenancy_db_name' => $database, 'tenancy_db_name' => $database,
'tenancy_db_connection' => 'mysql', 'tenancy_db_connection' => 'mysql',
]); ]);
$this->assertTrue($mysqlmanager->databaseExists($database)); expect($mysqlmanager->databaseExists($database))->toBeTrue();
$postgresManager = app(PostgreSQLDatabaseManager::class); $postgresManager = app(PostgreSQLDatabaseManager::class);
$postgresManager->setConnection('pgsql'); $postgresManager->setConnection('pgsql');
$database = 'db' . $this->randomString(); $database = 'db' . $this->randomString();
$this->assertFalse($postgresManager->databaseExists($database)); expect($postgresManager->databaseExists($database))->toBeFalse();
Tenant::create([ Tenant::create([
'tenancy_db_name' => $database, 'tenancy_db_name' => $database,
'tenancy_db_connection' => 'pgsql', 'tenancy_db_connection' => 'pgsql',
]); ]);
$this->assertTrue($postgresManager->databaseExists($database)); expect($postgresManager->databaseExists($database))->toBeTrue();
}); });
test('the tenant connection is fully removed', function () { test('the tenant connection is fully removed', function () {
@ -102,20 +102,20 @@ test('the tenant connection is fully removed', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$this->assertSame(['central'], array_keys(app('db')->getConnections())); expect(array_keys(app('db')->getConnections()))->toBe(['central']);
$this->assertArrayNotHasKey('tenant', config('database.connections')); $this->assertArrayNotHasKey('tenant', config('database.connections'));
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
createUsersTable(); createUsersTable();
$this->assertSame(['central', 'tenant'], array_keys(app('db')->getConnections())); expect(array_keys(app('db')->getConnections()))->toBe(['central', 'tenant']);
$this->assertArrayHasKey('tenant', config('database.connections')); $this->assertArrayHasKey('tenant', config('database.connections'));
tenancy()->end(); tenancy()->end();
$this->assertSame(['central'], array_keys(app('db')->getConnections())); expect(array_keys(app('db')->getConnections()))->toBe(['central']);
$this->assertNull(config('database.connections.tenant')); expect(config('database.connections.tenant'))->toBeNull();
}); });
test('db name is prefixed with db path when sqlite is used', function () { test('db name is prefixed with db path when sqlite is used', function () {
@ -132,7 +132,7 @@ test('db name is prefixed with db path when sqlite is used', function () {
]); ]);
app(DatabaseManager::class)->createTenantConnection($tenant); app(DatabaseManager::class)->createTenantConnection($tenant);
$this->assertSame(config('database.connections.tenant.database'), database_path('foodb')); expect(database_path('foodb'))->toBe(config('database.connections.tenant.database'));
}); });
test('schema manager uses schema to separate tenant dbs', function () { test('schema manager uses schema to separate tenant dbs', function () {
@ -160,8 +160,8 @@ test('schema manager uses schema to separate tenant dbs', function () {
config('database.connections.' . config('database.default') . '.search_path') : config('database.connections.' . config('database.default') . '.search_path') :
config('database.connections.' . config('database.default') . '.schema'); config('database.connections.' . config('database.default') . '.schema');
$this->assertSame($tenant->database()->getName(), $schemaConfig); expect($schemaConfig)->toBe($tenant->database()->getName());
$this->assertSame($originalDatabaseName, config(['database.connections.pgsql.database'])); expect(config(['database.connections.pgsql.database']))->toBe($originalDatabaseName);
}); });
test('a tenants database cannot be created when the database already exists', function () { test('a tenants database cannot be created when the database already exists', function () {
@ -175,7 +175,7 @@ test('a tenants database cannot be created when the database already exists', fu
]); ]);
$manager = $tenant->database()->manager(); $manager = $tenant->database()->manager();
$this->assertTrue($manager->databaseExists($tenant->database()->getName())); expect($manager->databaseExists($tenant->database()->getName()))->toBeTrue();
$this->expectException(TenantDatabaseAlreadyExistsException::class); $this->expectException(TenantDatabaseAlreadyExistsException::class);
$tenant2 = Tenant::create([ $tenant2 = Tenant::create([
@ -220,10 +220,10 @@ test('tenant database can be created on a foreign server', function () {
$manager = $tenant->database()->manager(); $manager = $tenant->database()->manager();
$manager->setConnection('mysql'); $manager->setConnection('mysql');
$this->assertFalse($manager->databaseExists($name)); expect($manager->databaseExists($name))->toBeFalse();
$manager->setConnection('mysql2'); $manager->setConnection('mysql2');
$this->assertTrue($manager->databaseExists($name)); expect($manager->databaseExists($name))->toBeTrue();
}); });
test('path used by sqlite manager can be customized', function () { test('path used by sqlite manager can be customized', function () {

View file

@ -36,11 +36,11 @@ test('current tenant can be resolved from service container using typehint', fun
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('id is generated when no id is supplied', function () { test('id is generated when no id is supplied', function () {
@ -66,8 +66,8 @@ test('autoincrement ids are supported', function () {
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
$this->assertSame(1, $tenant1->id); expect($tenant1->id)->toBe(1);
$this->assertSame(2, $tenant2->id); expect($tenant2->id)->toBe(2);
}); });
test('custom tenant model can be used', function () { test('custom tenant model can be used', function () {
@ -75,7 +75,7 @@ test('custom tenant model can be used', function () {
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertTrue(tenant() instanceof MyTenant); expect(tenant() instanceof MyTenant)->toBeTrue();
}); });
test('custom tenant model that doesnt extend vendor tenant model can be used', function () { test('custom tenant model that doesnt extend vendor tenant model can be used', function () {
@ -85,7 +85,7 @@ test('custom tenant model that doesnt extend vendor tenant model can be used', f
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$this->assertTrue(tenant() instanceof AnotherTenant); expect(tenant() instanceof AnotherTenant)->toBeTrue();
}); });
test('tenant can be created even when we are in another tenants context', function () { test('tenant can be created even when we are in another tenants context', function () {
@ -112,15 +112,15 @@ test('tenant can be created even when we are in another tenants context', functi
tenancy()->end(); tenancy()->end();
$this->assertSame(2, Tenant::count()); expect(Tenant::count())->toBe(2);
}); });
test('the model uses tenant collection', function () { test('the model uses tenant collection', function () {
Tenant::create(); Tenant::create();
Tenant::create(); Tenant::create();
$this->assertSame(2, Tenant::count()); expect(Tenant::count())->toBe(2);
$this->assertTrue(Tenant::all() instanceof TenantCollection); expect(Tenant::all() instanceof TenantCollection)->toBeTrue();
}); });
test('a command can be run on a collection of tenants', function () { test('a command can be run on a collection of tenants', function () {
@ -139,8 +139,8 @@ test('a command can be run on a collection of tenants', function () {
]); ]);
}); });
$this->assertSame('xyz', Tenant::find('t1')->foo); expect(Tenant::find('t1')->foo)->toBe('xyz');
$this->assertSame('xyz', Tenant::find('t2')->foo); expect(Tenant::find('t2')->foo)->toBe('xyz');
}); });
// Helpers // Helpers

View file

@ -171,7 +171,7 @@ test('tokens are deleted after use', function () {
->assertSuccessful() ->assertSuccessful()
->assertSee('You are logged in as Joe'); ->assertSee('You are logged in as Joe');
$this->assertNull(ImpersonationToken::find($token->token)); expect(ImpersonationToken::find($token->token))->toBeNull();
}); });
test('impersonation works with multiple models and guards', function () { test('impersonation works with multiple models and guards', function () {
@ -220,8 +220,8 @@ test('impersonation works with multiple models and guards', function () {
->assertSee('You are logged in as Joe'); ->assertSee('You are logged in as Joe');
Tenant::first()->run(function () { Tenant::first()->run(function () {
$this->assertSame('Joe', auth()->guard('another')->user()->name); expect(auth()->guard('another')->user()->name)->toBe('Joe');
$this->assertSame(null, auth()->guard('web')->user()); expect(auth()->guard('web')->user())->toBe(null);
}); });
}); });