mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:24:04 +00:00
[4.x] Add batch tenancy queue bootstrapper (#874)
* exclude master from CI * Add batch tenancy queue bootstrapper * add test case * skip tests for old versions * variable docblocks * use Laravel's connection getter and setter * convert test to pest * bottom space * singleton regis in TestCase * Update src/Bootstrappers/BatchTenancyBootstrapper.php Co-authored-by: Samuel Štancl <samuel@archte.ch> * convert batch class resolution to property level * enabled BatchTenancyBootstrapper by default * typehint DatabaseBatchRepository * refactore name * DI DB manager * typehint * Update config.php * use initialize() twice without end()ing tenancy to assert that previousConnection logic works correctly Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com> Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
8e3b74f9d1
commit
b78320b882
4 changed files with 102 additions and 9 deletions
|
|
@ -32,6 +32,7 @@ return [
|
||||||
Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
|
||||||
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
|
||||||
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
|
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
|
||||||
|
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
|
||||||
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
||||||
41
src/Bootstrappers/BatchTenancyBootstrapper.php
Normal file
41
src/Bootstrappers/BatchTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
|
use Illuminate\Bus\DatabaseBatchRepository;
|
||||||
|
use Illuminate\Database\Connection;
|
||||||
|
use Illuminate\Database\DatabaseManager;
|
||||||
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
|
class BatchTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The previous database connection instance.
|
||||||
|
*/
|
||||||
|
protected ?Connection $previousConnection = null;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected DatabaseBatchRepository $batchRepository,
|
||||||
|
protected DatabaseManager $databaseManager
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function bootstrap(Tenant $tenant)
|
||||||
|
{
|
||||||
|
// Update batch repository connection to use the tenant connection
|
||||||
|
$this->previousConnection = $this->batchRepository->getConnection();
|
||||||
|
$this->batchRepository->setConnection($this->databaseManager->connection('tenant'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function revert()
|
||||||
|
{
|
||||||
|
if ($this->previousConnection) {
|
||||||
|
// Replace batch repository connection with the previously replaced one
|
||||||
|
$this->batchRepository->setConnection($this->previousConnection);
|
||||||
|
$this->previousConnection = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
tests/BatchTest.php
Normal file
44
tests/BatchTest.php
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Bus\BatchRepository;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
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();
|
||||||
|
$tenant2 = Tenant::create();
|
||||||
|
|
||||||
|
expect(getBatchRepositoryConnectionName())->toBe('central');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
expect(getBatchRepositoryConnectionName())->toBe('tenant');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
return app(BatchRepository::class)->getConnection()->getName();
|
||||||
|
}
|
||||||
|
|
@ -4,8 +4,15 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Dotenv\Dotenv;
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Facades\GlobalCache;
|
||||||
|
use Stancl\Tenancy\Facades\Tenancy;
|
||||||
|
use Stancl\Tenancy\TenancyServiceProvider;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
|
|
@ -42,13 +49,13 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
/**
|
/**
|
||||||
* Define environment setup.
|
* Define environment setup.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Foundation\Application $app
|
* @param Application $app
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function getEnvironmentSetUp($app)
|
protected function getEnvironmentSetUp($app)
|
||||||
{
|
{
|
||||||
if (file_exists(__DIR__ . '/../.env')) {
|
if (file_exists(__DIR__ . '/../.env')) {
|
||||||
\Dotenv\Dotenv::createImmutable(__DIR__ . '/..')->load();
|
Dotenv::createImmutable(__DIR__ . '/..')->load();
|
||||||
}
|
}
|
||||||
|
|
||||||
$app['config']->set([
|
$app['config']->set([
|
||||||
|
|
@ -96,7 +103,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
'--realpath' => true,
|
'--realpath' => true,
|
||||||
'--force' => true,
|
'--force' => true,
|
||||||
],
|
],
|
||||||
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // todo0 change this to []? two tests in TenantDatabaseManagerTest are failing with that
|
'tenancy.bootstrappers.redis' => RedisTenancyBootstrapper::class, // todo0 change this to []? two tests in TenantDatabaseManagerTest are failing with that
|
||||||
'queue.connections.central' => [
|
'queue.connections.central' => [
|
||||||
'driver' => 'sync',
|
'driver' => 'sync',
|
||||||
'central' => true,
|
'central' => true,
|
||||||
|
|
@ -105,28 +112,28 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
'tenancy.tenant_model' => Tenant::class, // Use test tenant w/ DBs & domains
|
'tenancy.tenant_model' => Tenant::class, // Use test tenant w/ DBs & domains
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$app->singleton(\Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class);
|
$app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPackageProviders($app)
|
protected function getPackageProviders($app)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
\Stancl\Tenancy\TenancyServiceProvider::class,
|
TenancyServiceProvider::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPackageAliases($app)
|
protected function getPackageAliases($app)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'Tenancy' => \Stancl\Tenancy\Facades\Tenancy::class,
|
'Tenancy' => Tenancy::class,
|
||||||
'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCache::class,
|
'GlobalCache' => GlobalCache::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve application HTTP Kernel implementation.
|
* Resolve application HTTP Kernel implementation.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Foundation\Application $app
|
* @param Application $app
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function resolveApplicationHttpKernel($app)
|
protected function resolveApplicationHttpKernel($app)
|
||||||
|
|
@ -137,7 +144,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
/**
|
/**
|
||||||
* Resolve application Console Kernel implementation.
|
* Resolve application Console Kernel implementation.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Foundation\Application $app
|
* @param Application $app
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function resolveApplicationConsoleKernel($app)
|
protected function resolveApplicationConsoleKernel($app)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue