mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:24:04 +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)
|
||||
{
|
||||
$this->app['config']['database.default'] = $connection;
|
||||
$this->database->purge();
|
||||
$this->database->reconnect($connection);
|
||||
$this->database->setDefaultConnection($connection);
|
||||
|
|
|
|||
|
|
@ -32,13 +32,13 @@ class TenantManager
|
|||
protected $storage;
|
||||
|
||||
/** @var DatabaseManager */
|
||||
protected $database;
|
||||
public $database;
|
||||
|
||||
/** @var callable[][] Event listeners */
|
||||
protected $listeners = [];
|
||||
/** @var callable[][] */
|
||||
protected $eventListeners = [];
|
||||
|
||||
/** @var bool Has tenancy been initialized. */
|
||||
public $initialized;
|
||||
public $initialized = false;
|
||||
|
||||
public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
|
||||
{
|
||||
|
|
@ -153,8 +153,8 @@ class TenantManager
|
|||
|
||||
public function initializeTenancy(Tenant $tenant): self
|
||||
{
|
||||
$this->bootstrapTenancy($tenant);
|
||||
$this->setTenant($tenant);
|
||||
$this->bootstrapTenancy($tenant);
|
||||
$this->initialized = true;
|
||||
|
||||
return $this;
|
||||
|
|
@ -245,7 +245,7 @@ class TenantManager
|
|||
*/
|
||||
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
|
||||
|
|
@ -276,7 +276,7 @@ class TenantManager
|
|||
*/
|
||||
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) ?? []);
|
||||
|
||||
return $prevented;
|
||||
|
|
|
|||
|
|
@ -9,13 +9,15 @@ 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->tenant['id'] === $id) {
|
||||
if ($tenantManager->getTenant('id') === $id) {
|
||||
config(['tenancy.foo' => 'bar']);
|
||||
}
|
||||
});
|
||||
|
|
@ -31,7 +33,7 @@ class TenantManagerEventsTest extends TestCase
|
|||
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
||||
|
||||
Tenancy::eventListener('bootstrapped', function ($tenantManager) use ($id) {
|
||||
if ($tenantManager->tenant['id'] === $id) {
|
||||
if ($tenantManager->getTenant('id') === $id) {
|
||||
config(['tenancy.foo' => 'bar']);
|
||||
}
|
||||
});
|
||||
|
|
@ -47,7 +49,7 @@ class TenantManagerEventsTest extends TestCase
|
|||
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
|
||||
|
||||
Tenancy::eventListener('ending', function ($tenantManager) use ($id) {
|
||||
if ($tenantManager->tenant['id'] === $id) {
|
||||
if ($tenantManager->getTenant('id') === $id) {
|
||||
config(['tenancy.foo' => 'bar']);
|
||||
}
|
||||
});
|
||||
|
|
@ -62,12 +64,10 @@ class TenantManagerEventsTest extends TestCase
|
|||
/** @test */
|
||||
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) {
|
||||
if ($tenantManager->tenant['id'] === $id) {
|
||||
config(['tenancy.foo' => 'bar']);
|
||||
}
|
||||
Tenancy::eventListener('ended', function ($tenantManager) {
|
||||
config(['tenancy.foo' => 'bar']);
|
||||
});
|
||||
|
||||
$this->assertSame(null, config('tenancy.foo'));
|
||||
|
|
@ -77,21 +77,6 @@ class TenantManagerEventsTest extends TestCase
|
|||
$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 */
|
||||
public function database_can_be_reconnected_using_event_hooks()
|
||||
{
|
||||
|
|
@ -103,8 +88,8 @@ class TenantManagerEventsTest extends TestCase
|
|||
$id = Tenant::create('abc.localhost')['id'];
|
||||
|
||||
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
|
||||
if ($tenancy->tenant['id'] === $id) {
|
||||
$tenancy->database->useConnection('tenantabc');
|
||||
if ($tenancy->getTenant()['id'] === $id) {
|
||||
$tenancy->database->switchConnection('tenantabc');
|
||||
|
||||
return ['database'];
|
||||
}
|
||||
|
|
@ -126,8 +111,8 @@ class TenantManagerEventsTest extends TestCase
|
|||
$id = Tenant::create('abc.localhost')['id'];
|
||||
|
||||
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
|
||||
if ($tenancy->tenant['id'] === $id) {
|
||||
$tenancy->database->useConnection('tenantabc');
|
||||
if ($tenancy->getTenant()['id'] === $id) {
|
||||
$tenancy->database->switchConnection('tenantabc');
|
||||
// return ['database'];
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue