mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 21:54:03 +00:00
* 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>
104 lines
2.7 KiB
PHP
104 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
|
use Stancl\Tenancy\Database\Models;
|
|
use Stancl\Tenancy\Database\Models\Domain;
|
|
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
|
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
|
|
|
beforeEach(function () {
|
|
Route::group([
|
|
'middleware' => InitializeTenancyByDomain::class,
|
|
], function () {
|
|
Route::get('/foo/{a}/{b}', function ($a, $b) {
|
|
return "$a + $b";
|
|
});
|
|
});
|
|
|
|
config(['tenancy.tenant_model' => DomainTenant::class]);
|
|
});
|
|
|
|
test('tenant can be identified using hostname', function () {
|
|
$tenant = DomainTenant::create();
|
|
|
|
$id = $tenant->id;
|
|
|
|
$tenant->domains()->create([
|
|
'domain' => 'foo.localhost',
|
|
]);
|
|
|
|
$resolvedTenant = app(DomainTenantResolver::class)->resolve('foo.localhost');
|
|
|
|
expect($resolvedTenant->id)->toBe($id);
|
|
expect($resolvedTenant->domains->pluck('domain')->toArray())->toBe(['foo.localhost']);
|
|
});
|
|
|
|
test('a domain can belong to only one tenant', function () {
|
|
$tenant = DomainTenant::create();
|
|
|
|
$tenant->domains()->create([
|
|
'domain' => 'foo.localhost',
|
|
]);
|
|
|
|
$tenant2 = DomainTenant::create();
|
|
|
|
$this->expectException(DomainOccupiedByOtherTenantException::class);
|
|
$tenant2->domains()->create([
|
|
'domain' => 'foo.localhost',
|
|
]);
|
|
});
|
|
|
|
test('an exception is thrown if tenant cannot be identified', function () {
|
|
$this->expectException(TenantCouldNotBeIdentifiedOnDomainException::class);
|
|
|
|
app(DomainTenantResolver::class)->resolve('foo.localhost');
|
|
});
|
|
|
|
test('tenant can be identified by domain', function () {
|
|
$tenant = DomainTenant::create([
|
|
'id' => 'acme',
|
|
]);
|
|
|
|
$tenant->domains()->create([
|
|
'domain' => 'foo.localhost',
|
|
]);
|
|
|
|
expect(tenancy()->initialized)->toBeFalse();
|
|
|
|
$this
|
|
->get('http://foo.localhost/foo/abc/xyz')
|
|
->assertSee('abc + xyz');
|
|
|
|
expect(tenancy()->initialized)->toBeTrue();
|
|
expect(tenant('id'))->toBe('acme');
|
|
});
|
|
|
|
test('onfail logic can be customized', function () {
|
|
InitializeTenancyByDomain::$onFail = function () {
|
|
return 'foo';
|
|
};
|
|
|
|
$this
|
|
->get('http://foo.localhost/foo/abc/xyz')
|
|
->assertSee('foo');
|
|
});
|
|
|
|
test('domains are always lowercase', function () {
|
|
$tenant = DomainTenant::create();
|
|
|
|
$tenant->domains()->create([
|
|
'domain' => 'CAPITALS',
|
|
]);
|
|
|
|
expect(Domain::first()->domain)->toBe('capitals');
|
|
});
|
|
|
|
class DomainTenant extends Models\Tenant
|
|
{
|
|
use HasDomains;
|
|
}
|