mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 00:04:03 +00:00
Convert test cases
This commit is contained in:
parent
405b91085e
commit
a8dcd0dfc9
27 changed files with 3059 additions and 3658 deletions
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Session\Middleware\StartSession;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
|
@ -13,69 +11,59 @@ use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
|||
use Stancl\Tenancy\Middleware\ScopeSessions;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
class ScopeSessionsTest extends TestCase
|
||||
{
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
uses(Stancl\Tenancy\Tests\TestCase::class);
|
||||
|
||||
Route::group([
|
||||
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
|
||||
], function () {
|
||||
Route::get('/foo', function () {
|
||||
return 'true';
|
||||
});
|
||||
beforeEach(function () {
|
||||
Route::group([
|
||||
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
|
||||
], function () {
|
||||
Route::get('/foo', function () {
|
||||
return 'true';
|
||||
});
|
||||
});
|
||||
|
||||
Event::listen(TenantCreated::class, function (TenantCreated $event) {
|
||||
$tenant = $event->tenant;
|
||||
Event::listen(TenantCreated::class, function (TenantCreated $event) {
|
||||
$tenant = $event->tenant;
|
||||
|
||||
/** @var Tenant $tenant */
|
||||
$tenant->domains()->create([
|
||||
'domain' => $tenant->id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tenant_id_is_auto_added_to_session_if_its_missing()
|
||||
{
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
/** @var Tenant $tenant */
|
||||
$tenant->domains()->create([
|
||||
'domain' => $tenant->id,
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSessionHas(ScopeSessions::$tenantIdKey, 'acme');
|
||||
}
|
||||
test('tenant id is auto added to session if its missing', function () {
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
/** @test */
|
||||
public function changing_tenant_id_in_session_will_abort_the_request()
|
||||
{
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSessionHas(ScopeSessions::$tenantIdKey, 'acme');
|
||||
});
|
||||
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSuccessful();
|
||||
test('changing tenant id in session will abort the request', function () {
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
session()->put(ScopeSessions::$tenantIdKey, 'foobar');
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertStatus(403);
|
||||
}
|
||||
session()->put(ScopeSessions::$tenantIdKey, 'foobar');
|
||||
|
||||
/** @test */
|
||||
public function an_exception_is_thrown_when_the_middleware_is_executed_before_tenancy_is_initialized()
|
||||
{
|
||||
Route::get('/bar', function () {
|
||||
return true;
|
||||
})->middleware([StartSession::class, ScopeSessions::class]);
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertStatus(403);
|
||||
});
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
test('an exception is thrown when the middleware is executed before tenancy is initialized', function () {
|
||||
Route::get('/bar', function () {
|
||||
return true;
|
||||
})->middleware([StartSession::class, ScopeSessions::class]);
|
||||
|
||||
$this->expectException(TenancyNotInitializedException::class);
|
||||
$this->withoutExceptionHandling()->get('http://acme.localhost/bar');
|
||||
}
|
||||
}
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
|
||||
$this->expectException(TenancyNotInitializedException::class);
|
||||
$this->withoutExceptionHandling()->get('http://acme.localhost/bar');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue