mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:54:03 +00:00
Fix event tests
This commit is contained in:
parent
a9c37d1535
commit
7e645a3312
3 changed files with 20 additions and 34 deletions
|
|
@ -82,6 +82,7 @@ class DatabaseManager
|
||||||
|
|
||||||
public function switchConnection($connection)
|
public function switchConnection($connection)
|
||||||
{
|
{
|
||||||
|
$this->app['config']['database.default'] = $connection;
|
||||||
$this->database->purge();
|
$this->database->purge();
|
||||||
$this->database->reconnect($connection);
|
$this->database->reconnect($connection);
|
||||||
$this->database->setDefaultConnection($connection);
|
$this->database->setDefaultConnection($connection);
|
||||||
|
|
|
||||||
|
|
@ -32,13 +32,13 @@ class TenantManager
|
||||||
protected $storage;
|
protected $storage;
|
||||||
|
|
||||||
/** @var DatabaseManager */
|
/** @var DatabaseManager */
|
||||||
protected $database;
|
public $database;
|
||||||
|
|
||||||
/** @var callable[][] Event listeners */
|
/** @var callable[][] */
|
||||||
protected $listeners = [];
|
protected $eventListeners = [];
|
||||||
|
|
||||||
/** @var bool Has tenancy been initialized. */
|
/** @var bool Has tenancy been initialized. */
|
||||||
public $initialized;
|
public $initialized = false;
|
||||||
|
|
||||||
public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
|
public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
|
||||||
{
|
{
|
||||||
|
|
@ -153,8 +153,8 @@ class TenantManager
|
||||||
|
|
||||||
public function initializeTenancy(Tenant $tenant): self
|
public function initializeTenancy(Tenant $tenant): self
|
||||||
{
|
{
|
||||||
$this->bootstrapTenancy($tenant);
|
|
||||||
$this->setTenant($tenant);
|
$this->setTenant($tenant);
|
||||||
|
$this->bootstrapTenancy($tenant);
|
||||||
$this->initialized = true;
|
$this->initialized = true;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -245,7 +245,7 @@ class TenantManager
|
||||||
*/
|
*/
|
||||||
public function tenancyBootstrappers($except = []): array
|
public function tenancyBootstrappers($except = []): array
|
||||||
{
|
{
|
||||||
return array_diff_key($this->app['config']['tenancy.bootstrappers'], $except);
|
return array_diff_key($this->app['config']['tenancy.bootstrappers'], array_flip($except));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shouldMigrateAfterCreation(): bool
|
public function shouldMigrateAfterCreation(): bool
|
||||||
|
|
@ -276,7 +276,7 @@ class TenantManager
|
||||||
*/
|
*/
|
||||||
protected function event(string $name): array
|
protected function event(string $name): array
|
||||||
{
|
{
|
||||||
return array_reduce($this->eventCalbacks[$name] ?? [], function ($prevented, $listener) {
|
return array_reduce($this->eventListeners[$name] ?? [], function ($prevented, $listener) {
|
||||||
$prevented = array_merge($prevented, $listener($this) ?? []);
|
$prevented = array_merge($prevented, $listener($this) ?? []);
|
||||||
|
|
||||||
return $prevented;
|
return $prevented;
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,15 @@ use Tenancy;
|
||||||
|
|
||||||
class TenantManagerEventsTest extends TestCase
|
class TenantManagerEventsTest extends TestCase
|
||||||
{
|
{
|
||||||
|
public $autoInitTenancy = false;
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function bootstrapping_event_works()
|
public function bootstrapping_event_works()
|
||||||
{
|
{
|
||||||
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
||||||
|
|
||||||
Tenancy::eventListener('bootstrapping', function ($tenantManager) use ($id) {
|
Tenancy::eventListener('bootstrapping', function ($tenantManager) use ($id) {
|
||||||
if ($tenantManager->tenant['id'] === $id) {
|
if ($tenantManager->getTenant('id') === $id) {
|
||||||
config(['tenancy.foo' => 'bar']);
|
config(['tenancy.foo' => 'bar']);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -31,7 +33,7 @@ class TenantManagerEventsTest extends TestCase
|
||||||
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
||||||
|
|
||||||
Tenancy::eventListener('bootstrapped', function ($tenantManager) use ($id) {
|
Tenancy::eventListener('bootstrapped', function ($tenantManager) use ($id) {
|
||||||
if ($tenantManager->tenant['id'] === $id) {
|
if ($tenantManager->getTenant('id') === $id) {
|
||||||
config(['tenancy.foo' => 'bar']);
|
config(['tenancy.foo' => 'bar']);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -47,7 +49,7 @@ class TenantManagerEventsTest extends TestCase
|
||||||
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
||||||
|
|
||||||
Tenancy::eventListener('ending', function ($tenantManager) use ($id) {
|
Tenancy::eventListener('ending', function ($tenantManager) use ($id) {
|
||||||
if ($tenantManager->tenant['id'] === $id) {
|
if ($tenantManager->getTenant('id') === $id) {
|
||||||
config(['tenancy.foo' => 'bar']);
|
config(['tenancy.foo' => 'bar']);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -62,12 +64,10 @@ class TenantManagerEventsTest extends TestCase
|
||||||
/** @test */
|
/** @test */
|
||||||
public function ended_event_works()
|
public function ended_event_works()
|
||||||
{
|
{
|
||||||
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
||||||
|
|
||||||
Tenancy::eventListener('ended', function ($tenantManager) use ($id) {
|
Tenancy::eventListener('ended', function ($tenantManager) {
|
||||||
if ($tenantManager->tenant['id'] === $id) {
|
config(['tenancy.foo' => 'bar']);
|
||||||
config(['tenancy.foo' => 'bar']);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertSame(null, config('tenancy.foo'));
|
$this->assertSame(null, config('tenancy.foo'));
|
||||||
|
|
@ -77,21 +77,6 @@ class TenantManagerEventsTest extends TestCase
|
||||||
$this->assertSame('bar', config('tenancy.foo'));
|
$this->assertSame('bar', config('tenancy.foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function event_returns_a_collection()
|
|
||||||
{
|
|
||||||
// Note: The event() method should not be called by your code.
|
|
||||||
tenancy()->bootstrapping(function ($tenancy) {
|
|
||||||
return ['database'];
|
|
||||||
});
|
|
||||||
tenancy()->bootstrapping(function ($tenancy) {
|
|
||||||
return ['redis', 'cache'];
|
|
||||||
});
|
|
||||||
|
|
||||||
$prevents = tenancy()->event('bootstrapping');
|
|
||||||
$this->assertEquals(collect(['database', 'redis', 'cache']), $prevents);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function database_can_be_reconnected_using_event_hooks()
|
public function database_can_be_reconnected_using_event_hooks()
|
||||||
{
|
{
|
||||||
|
|
@ -103,8 +88,8 @@ class TenantManagerEventsTest extends TestCase
|
||||||
$id = Tenant::create('abc.localhost')['id'];
|
$id = Tenant::create('abc.localhost')['id'];
|
||||||
|
|
||||||
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
|
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
|
||||||
if ($tenancy->tenant['id'] === $id) {
|
if ($tenancy->getTenant()['id'] === $id) {
|
||||||
$tenancy->database->useConnection('tenantabc');
|
$tenancy->database->switchConnection('tenantabc');
|
||||||
|
|
||||||
return ['database'];
|
return ['database'];
|
||||||
}
|
}
|
||||||
|
|
@ -126,8 +111,8 @@ class TenantManagerEventsTest extends TestCase
|
||||||
$id = Tenant::create('abc.localhost')['id'];
|
$id = Tenant::create('abc.localhost')['id'];
|
||||||
|
|
||||||
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
|
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
|
||||||
if ($tenancy->tenant['id'] === $id) {
|
if ($tenancy->getTenant()['id'] === $id) {
|
||||||
$tenancy->database->useConnection('tenantabc');
|
$tenancy->database->switchConnection('tenantabc');
|
||||||
// return ['database'];
|
// return ['database'];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue