diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index e81617ab..46143cef 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -1,11 +1,12 @@ previousConnection) { - // Access the resolved batch repository instance and replace its connection with the previously replaced one + // Access the resolved batch repository instance and replace its connection with the previously replaced one $batchRepository = app(BatchRepository::class); $batchRepository->setConnection($this->previousConnection); $this->previousConnection = null; } } -} \ No newline at end of file +} diff --git a/tests/BatchTest.php b/tests/BatchTest.php index 66ebaec5..06f41401 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -1,13 +1,7 @@ app->singleton(BatchTenancyBootstrapper::class); + + config([ + 'tenancy.bootstrappers' => [ + DatabaseTenancyBootstrapper::class, + BatchTenancyBootstrapper::class, + ], + ]); + + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); +}); + +test('batch repository is set to tenant connection and reverted', function () { + $tenant = Tenant::create(); + + expect(getBatchRepositoryConnectionName())->toBe('central'); + + tenancy()->initialize($tenant); + + expect(getBatchRepositoryConnectionName())->toBe('tenant'); + + tenancy()->end(); + + expect(getBatchRepositoryConnectionName())->toBe('central'); +})->skip(fn() => version_compare(app()->version(), '8.0', '<'), 'Job batches are only supported in Laravel 8+'); + +function getBatchRepositoryConnectionName() { - public function setUp(): void - { - parent::setUp(); - - $this->app->singleton(BatchTenancyBootstrapper::class); - - config([ - 'tenancy.bootstrappers' => [ - DatabaseTenancyBootstrapper::class, - BatchTenancyBootstrapper::class, - ], - ]); - - Event::listen(TenancyInitialized::class, BootstrapTenancy::class); - Event::listen(TenancyEnded::class, RevertToCentralContext::class); - } - - /** @test */ - public function batch_repository_is_set_to_tenant_connection_and_reverted() - { - if (! version_compare(app()->version(), '8.0', '>=')) { - $this->markTestSkipped('Job batches are only supported in Laravel 8+'); - } - - $tenant = Tenant::create(); - - $this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected initial connection to be central'); - - tenancy()->initialize($tenant); - - $this->assertEquals('tenant', $this->getBatchRepositoryConnectionName(), 'Expected tenant connection to be tenant'); - - tenancy()->end(); - - $this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected the reverted connection to be central'); - } - - - private function getBatchRepositoryConnectionName(): string - { - $batchRepository = app(BatchRepository::class); - - $batchRepositoryReflection = new ReflectionClass($batchRepository); - $connectionProperty = $batchRepositoryReflection->getProperty('connection'); - $connectionProperty->setAccessible(true); - $connection = $connectionProperty->getValue($batchRepository); - - return $connection->getName(); - } - -} + return app(BatchRepository::class)->getConnection()->getName(); +} \ No newline at end of file