1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 16:14:02 +00:00
tenancy/tests/TenantManagerEventsTest.php
2019-09-16 18:38:23 +02:00

124 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Stancl\Tenancy\Tenant;
use Tenancy;
class TenantManagerEventsTest extends TestCase
{
public $autoInitTenancy = false;
/** @test */
public function bootstrapping_event_works()
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('bootstrapping', function ($tenantManager) use ($id) {
if ($tenantManager->getTenant('id') === $id) {
config(['tenancy.foo' => 'bar']);
}
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function bootstrapped_event_works()
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('bootstrapped', function ($tenantManager) use ($id) {
if ($tenantManager->getTenant('id') === $id) {
config(['tenancy.foo' => 'bar']);
}
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function ending_event_works()
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('ending', function ($tenantManager) use ($id) {
if ($tenantManager->getTenant('id') === $id) {
config(['tenancy.foo' => 'bar']);
}
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame(null, config('tenancy.foo'));
tenancy()->endTenancy();
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function ended_event_works()
{
Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::eventListener('ended', function ($tenantManager) {
config(['tenancy.foo' => 'bar']);
});
$this->assertSame(null, config('tenancy.foo'));
tenancy()->init('foo.localhost');
$this->assertSame(null, config('tenancy.foo'));
tenancy()->endTenancy();
$this->assertSame('bar', config('tenancy.foo'));
}
/** @test */
public function database_can_be_reconnected_using_event_hooks()
{
config(['database.connections.tenantabc' => [
'driver' => 'sqlite',
'database' => database_path('some_special_database.sqlite'),
]]);
$id = Tenant::create('abc.localhost')['id'];
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
if ($tenancy->getTenant()['id'] === $id) {
$tenancy->database->switchConnection('tenantabc');
return ['database'];
}
});
$this->assertNotSame('tenantabc', \DB::connection()->getConfig()['name']);
tenancy()->init('abc.localhost');
$this->assertSame('tenantabc', \DB::connection()->getConfig()['name']);
}
/** @test */
public function database_cannot_be_reconnected_without_using_prevents()
{
config(['database.connections.tenantabc' => [
'driver' => 'sqlite',
'database' => database_path('some_special_database.sqlite'),
]]);
$id = Tenant::create('abc.localhost')['id'];
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
if ($tenancy->getTenant()['id'] === $id) {
$tenancy->database->switchConnection('tenantabc');
// return ['database'];
}
});
$this->assertNotSame('tenantabc', \DB::connection()->getConfig()['name']);
tenancy()->init('abc.localhost');
$this->assertSame('tenant', \DB::connection()->getConfig()['name']);
}
}