mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:34:03 +00:00
Queue tests
This commit is contained in:
parent
00bb0d06b3
commit
86a98b2bc8
13 changed files with 216 additions and 124 deletions
|
|
@ -42,7 +42,6 @@
|
||||||
"Stancl\\Tenancy\\TenancyServiceProvider"
|
"Stancl\\Tenancy\\TenancyServiceProvider"
|
||||||
],
|
],
|
||||||
"aliases": {
|
"aliases": {
|
||||||
"Tenant": "Stancl\\Tenancy\\Facades\\Tenant",
|
|
||||||
"Tenancy": "Stancl\\Tenancy\\Facades\\Tenancy",
|
"Tenancy": "Stancl\\Tenancy\\Facades\\Tenancy",
|
||||||
"GlobalCache": "Stancl\\Tenancy\\Facades\\GlobalCache"
|
"GlobalCache": "Stancl\\Tenancy\\Facades\\GlobalCache"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,15 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||||
|
|
||||||
trait GeneratesIds
|
trait GeneratesIds
|
||||||
{
|
{
|
||||||
public static function bootGeneratesIds()
|
public static function bootGeneratesIds()
|
||||||
{
|
{
|
||||||
static::creating(function (self $model) {
|
static::creating(function (self $model) {
|
||||||
if (! $model->id && config('tenancy.id_generator')) {
|
if (! $model->id && app()->bound(UniqueIdentifierGenerator::class)) {
|
||||||
$model->id = app(config('tenancy.id_generator'))->generate($model);
|
$model->id = app(UniqueIdentifierGenerator::class)->generate($model);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Facades;
|
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Facade;
|
|
||||||
use Stancl\Tenancy\Tenant as TenantObject;
|
|
||||||
|
|
||||||
class Tenant extends Facade
|
|
||||||
{
|
|
||||||
protected static function getFacadeAccessor()
|
|
||||||
{
|
|
||||||
return TenantObject::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function create($domains, array $data = []): TenantObject
|
|
||||||
{
|
|
||||||
return TenantObject::create($domains, $data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -63,4 +63,9 @@ class Tenancy
|
||||||
|
|
||||||
return new $class;
|
return new $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function find($id): ?Tenant
|
||||||
|
{
|
||||||
|
return $this->model()->find($id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,28 +5,63 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\TenancyBootstrappers;
|
namespace Stancl\Tenancy\TenancyBootstrappers;
|
||||||
|
|
||||||
use Illuminate\Config\Repository;
|
use Illuminate\Config\Repository;
|
||||||
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
use Illuminate\Queue\Events\JobProcessing;
|
||||||
use Illuminate\Queue\QueueManager;
|
use Illuminate\Queue\QueueManager;
|
||||||
use Illuminate\Support\Testing\Fakes\QueueFake;
|
use Illuminate\Support\Testing\Fakes\QueueFake;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
// todo rewrite this
|
|
||||||
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
/** @var bool Has tenancy been started. */
|
public $tenancyInitialized = false;
|
||||||
public $started = false;
|
|
||||||
|
|
||||||
/** @var Repository */
|
/** @var Repository */
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
public function __construct(Repository $config, QueueManager $queue)
|
/** @var QueueManager */
|
||||||
|
protected $queue;
|
||||||
|
|
||||||
|
/** @var Dispatcher */
|
||||||
|
protected $event;
|
||||||
|
|
||||||
|
public function __construct(Repository $config, QueueManager $queue, Dispatcher $event)
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
$this->queue = $queue;
|
||||||
|
$this->event = $event;
|
||||||
|
|
||||||
|
$this->setUpJobListener();
|
||||||
|
$this->setUpPayloadGenerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUpJobListener()
|
||||||
|
{
|
||||||
|
$this->event->listen(JobProcessing::class, function ($event) {
|
||||||
|
$tenantId = $event->job->payload()['tenant_id'] ?? null;
|
||||||
|
|
||||||
|
// The job is not tenant-aware
|
||||||
|
if (!$tenantId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
|
||||||
|
if (tenancy()->initialized && tenant('id') === $tenantId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tenancy was either not initialized, or initialized for a different tenant.
|
||||||
|
// Therefore, we initialize it for the correct tenant.
|
||||||
|
tenancy()->initialize(tenancy()->find($tenantId));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUpPayloadGenerator()
|
||||||
|
{
|
||||||
$bootstrapper = &$this;
|
$bootstrapper = &$this;
|
||||||
|
|
||||||
if (! $queue instanceof QueueFake) {
|
if (! $this->queue instanceof QueueFake) {
|
||||||
$queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
|
$this->queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
|
||||||
return $bootstrapper->getPayload($connection);
|
return $bootstrapper->getPayload($connection);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -34,17 +69,17 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
public function start(Tenant $tenant)
|
public function start(Tenant $tenant)
|
||||||
{
|
{
|
||||||
$this->started = true;
|
$this->tenancyInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function end()
|
public function end()
|
||||||
{
|
{
|
||||||
$this->started = false;
|
$this->tenancyInitialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPayload(string $connection)
|
public function getPayload(string $connection)
|
||||||
{
|
{
|
||||||
if (! $this->started) {
|
if (! $this->tenancyInitialized) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,8 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy;
|
namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use Illuminate\Cache\CacheManager;
|
use Illuminate\Cache\CacheManager;
|
||||||
use Illuminate\Contracts\Http\Kernel;
|
use Illuminate\Queue\Events\JobProcessing;
|
||||||
use Illuminate\Support\Facades\Route;
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver;
|
|
||||||
use Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper;
|
use Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
|
|
@ -23,10 +21,7 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
|
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
|
||||||
|
|
||||||
$this->app->bind(Contracts\StorageDriver::class, function ($app) {
|
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.id_generator']);
|
||||||
return $app->make(DatabaseStorageDriver::class);
|
|
||||||
});
|
|
||||||
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']);
|
|
||||||
$this->app->singleton(DatabaseManager::class);
|
$this->app->singleton(DatabaseManager::class);
|
||||||
$this->app->singleton(Tenancy::class);
|
$this->app->singleton(Tenancy::class);
|
||||||
$this->app->bind(Tenant::class, function ($app) {
|
$this->app->bind(Tenant::class, function ($app) {
|
||||||
|
|
@ -80,22 +75,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
__DIR__ . '/../assets/migrations/' => database_path('migrations'),
|
__DIR__ . '/../assets/migrations/' => database_path('migrations'),
|
||||||
], 'migrations');
|
], 'migrations');
|
||||||
|
|
||||||
foreach ($this->app['config']['tenancy.global_middleware'] as $middleware) {
|
|
||||||
$this->app->make(Kernel::class)->prependMiddleware($middleware);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Since tenancy is initialized in the global middleware stack, this
|
|
||||||
* middleware group acts mostly as a 'flag' for the PreventAccess
|
|
||||||
* middleware to decide whether the request should be aborted.
|
|
||||||
*/
|
|
||||||
Route::middlewareGroup('tenancy', [
|
|
||||||
/* Prevent access from tenant domains to central routes and vice versa. */
|
|
||||||
Middleware\PreventAccessFromTenantDomains::class,
|
|
||||||
]);
|
|
||||||
|
|
||||||
Route::middlewareGroup('universal', []);
|
|
||||||
|
|
||||||
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
||||||
|
|
||||||
$this->app->singleton('globalUrl', function ($app) {
|
$this->app->singleton('globalUrl', function ($app) {
|
||||||
|
|
@ -108,24 +87,5 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
return $instance;
|
return $instance;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Queue tenancy
|
|
||||||
$this->app['events']->listen(\Illuminate\Queue\Events\JobProcessing::class, function ($event) {
|
|
||||||
$tenantId = $event->job->payload()['tenant_id'] ?? null;
|
|
||||||
|
|
||||||
// The job is not tenant-aware
|
|
||||||
if (! $tenantId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
|
|
||||||
if (tenancy()->initialized && tenant('id') === $tenantId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tenancy was either not initialized, or initialized for a different tenant.
|
|
||||||
// Therefore, we initialize it for the correct tenant.
|
|
||||||
tenancy()->initById($tenantId);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1
tests/Etc/tmp/jobpipelinetest.json
Normal file
1
tests/Etc/tmp/jobpipelinetest.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"foo":"bar"}
|
||||||
1
tests/Etc/tmp/queuetest.json
Normal file
1
tests/Etc/tmp/queuetest.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"tenant_id":"The current tenant id is: acme"}
|
||||||
|
|
@ -4,15 +4,12 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Redis;
|
||||||
use Illuminate\Testing\Assert as PHPUnit;
|
use Illuminate\Testing\Assert as PHPUnit;
|
||||||
use Illuminate\Testing\TestResponse;
|
use Illuminate\Testing\TestResponse;
|
||||||
use Stancl\Tenancy\Tenant;
|
|
||||||
|
|
||||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
{
|
{
|
||||||
public $autoCreateTenant = false;
|
|
||||||
public $autoInitTenancy = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup the test environment.
|
* Setup the test environment.
|
||||||
*
|
*
|
||||||
|
|
@ -22,7 +19,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
// Redis::connection('cache')->flushdb();
|
Redis::connection('default')->flushdb();
|
||||||
|
Redis::connection('cache')->flushdb();
|
||||||
|
|
||||||
file_put_contents(database_path('central.sqlite'), '');
|
file_put_contents(database_path('central.sqlite'), '');
|
||||||
$this->artisan('migrate:fresh', [
|
$this->artisan('migrate:fresh', [
|
||||||
|
|
@ -31,14 +29,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
'--realpath' => true,
|
'--realpath' => true,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($this->autoCreateTenant) {
|
|
||||||
$this->createTenant();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->autoInitTenancy) {
|
|
||||||
$this->initTenancy();
|
|
||||||
}
|
|
||||||
|
|
||||||
TestResponse::macro('assertContent', function ($content) {
|
TestResponse::macro('assertContent', function ($content) {
|
||||||
/** @var TestResponse $this */
|
/** @var TestResponse $this */
|
||||||
|
|
||||||
|
|
@ -48,16 +38,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createTenant($domains = ['test.localhost'])
|
|
||||||
{
|
|
||||||
Tenant::new()->withDomains($domains)->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function initTenancy($domain = 'test.localhost')
|
|
||||||
{
|
|
||||||
return tenancy()->init($domain);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define environment setup.
|
* Define environment setup.
|
||||||
*
|
*
|
||||||
|
|
@ -75,25 +55,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||||
'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
||||||
'database.redis.options.prefix' => 'foo',
|
'database.redis.options.prefix' => 'foo',
|
||||||
'database.redis.tenancy' => [
|
|
||||||
'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'),
|
|
||||||
'password' => env('TENANCY_TEST_REDIS_PASSWORD', null),
|
|
||||||
'port' => env('TENANCY_TEST_REDIS_PORT', 6379),
|
|
||||||
// Use the #14 Redis database unless specified otherwise.
|
|
||||||
// Make sure you don't store anything in this db!
|
|
||||||
'database' => env('TENANCY_TEST_REDIS_DB', 14),
|
|
||||||
'prefix' => 'abc', // unrelated to tenancy, but this doesn't seem to have an effect? try to replicate in a fresh laravel installation
|
|
||||||
],
|
|
||||||
'database.connections.central' => [
|
'database.connections.central' => [
|
||||||
'driver' => 'sqlite',
|
'driver' => 'sqlite',
|
||||||
'database' => database_path('central.sqlite'),
|
'database' => database_path('central.sqlite'),
|
||||||
// 'database' => ':memory:',
|
// 'database' => ':memory:',
|
||||||
],
|
],
|
||||||
'tenancy.database' => [
|
|
||||||
'template_connection' => 'central',
|
|
||||||
'prefix' => 'tenant',
|
|
||||||
'suffix' => '.sqlite',
|
|
||||||
],
|
|
||||||
'database.connections.sqlite.database' => ':memory:',
|
'database.connections.sqlite.database' => ':memory:',
|
||||||
'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'),
|
'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'),
|
||||||
'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'),
|
'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'),
|
||||||
|
|
@ -132,7 +98,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
protected function getPackageAliases($app)
|
protected function getPackageAliases($app)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'Tenant' => \Stancl\Tenancy\Facades\Tenant::class,
|
|
||||||
'Tenancy' => \Stancl\Tenancy\Facades\Tenancy::class,
|
'Tenancy' => \Stancl\Tenancy\Facades\Tenancy::class,
|
||||||
'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCache::class,
|
'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCache::class,
|
||||||
];
|
];
|
||||||
|
|
@ -165,11 +130,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', (int) (ceil($length / strlen($x))))), 1, $length);
|
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', (int) (ceil($length / strlen($x))))), 1, $length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isContainerized()
|
|
||||||
{
|
|
||||||
return env('CONTINUOUS_INTEGRATION') || env('DOCKER');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function assertArrayIsSubset($subset, $array, string $message = ''): void
|
public function assertArrayIsSubset($subset, $array, string $message = ''): void
|
||||||
{
|
{
|
||||||
parent::assertTrue(array_intersect($subset, $array) == $subset, $message);
|
parent::assertTrue(array_intersect($subset, $array) == $subset, $message);
|
||||||
|
|
|
||||||
|
|
@ -178,9 +178,5 @@ class BootstrapperTest extends TestCase
|
||||||
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
$this->assertFalse(Storage::disk('public')->exists('abc'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
// for queues see QueueTest
|
||||||
public function queue_data_is_separated()
|
|
||||||
{
|
|
||||||
// todo
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ class JobPipelineTest extends TestCase
|
||||||
|
|
||||||
config(['queue.default' => 'redis']);
|
config(['queue.default' => 'redis']);
|
||||||
|
|
||||||
file_put_contents(__DIR__ . '/../Etc/tmp/jobpipelinetest.json', '{}');
|
|
||||||
$this->valuestore = Valuestore::make(__DIR__ . '/../Etc/tmp/jobpipelinetest.json')->flush();
|
$this->valuestore = Valuestore::make(__DIR__ . '/../Etc/tmp/jobpipelinetest.json')->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,6 +63,22 @@ class JobPipelineTest extends TestCase
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function job_pipelines_run_when_queued()
|
||||||
|
{
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
FooJob::class,
|
||||||
|
])->send(function () {
|
||||||
|
return $this->valuestore;
|
||||||
|
})->shouldBeQueued(true)->toListener());
|
||||||
|
|
||||||
|
$this->assertFalse($this->valuestore->has('foo'));
|
||||||
|
Tenant::create();
|
||||||
|
$this->artisan('queue:work --once');
|
||||||
|
|
||||||
|
$this->assertSame('bar', $this->valuestore->get('foo'));
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function job_pipeline_executes_jobs_and_passes_the_object_sequentially()
|
public function job_pipeline_executes_jobs_and_passes_the_object_sequentially()
|
||||||
{
|
{
|
||||||
|
|
@ -159,6 +174,7 @@ class JobWithMultipleArguments
|
||||||
|
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
// we dont queue this job so no need to use valuestore here
|
||||||
app()->instance('test_args', [$this->first, $this->second]);
|
app()->instance('test_args', [$this->first, $this->second]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
136
tests/v3/QueueTest.php
Normal file
136
tests/v3/QueueTest.php
Normal file
|
|
@ -0,0 +1,136 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\v3;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\Events\JobProcessing;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Spatie\Valuestore\Valuestore;
|
||||||
|
use Stancl\Tenancy\Database\Models\Tenant;
|
||||||
|
use Stancl\Tenancy\Events\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
|
use Stancl\Tenancy\TenancyBootstrappers\QueueTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
|
||||||
|
class QueueTest extends TestCase
|
||||||
|
{
|
||||||
|
public $mockConsoleOutput = false;
|
||||||
|
|
||||||
|
/** @var Valuestore */
|
||||||
|
protected $valuestore;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [
|
||||||
|
QueueTenancyBootstrapper::class,
|
||||||
|
],
|
||||||
|
'queue.default' => 'redis',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
|
||||||
|
$this->valuestore = Valuestore::make(__DIR__ . '/../Etc/tmp/queuetest.json')->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function tenant_id_is_passed_to_tenant_queues()
|
||||||
|
{
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
Event::fake();
|
||||||
|
|
||||||
|
dispatch(new TestJob($this->valuestore));
|
||||||
|
|
||||||
|
Event::assertDispatched(JobProcessing::class, function ($event) {
|
||||||
|
return $event->job->payload()['tenant_id'] === tenant('id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function tenant_id_is_not_passed_to_central_queues()
|
||||||
|
{
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
Event::fake();
|
||||||
|
|
||||||
|
config(['queue.connections.central' => [
|
||||||
|
'driver' => 'sync',
|
||||||
|
'central' => true,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
dispatch(new TestJob($this->valuestore))->onConnection('central');
|
||||||
|
|
||||||
|
Event::assertDispatched(JobProcessing::class, function ($event) {
|
||||||
|
return ! isset($event->job->payload()['tenant_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function tenancy_is_initialized_inside_queues()
|
||||||
|
{
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
dispatch(new TestJob($this->valuestore));
|
||||||
|
|
||||||
|
$this->assertFalse($this->valuestore->has('tenant_id'));
|
||||||
|
$this->artisan('queue:work --once');
|
||||||
|
|
||||||
|
$this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function the_tenant_used_by_the_job_doesnt_change_when_the_current_tenant_changes()
|
||||||
|
{
|
||||||
|
$tenant1 = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
dispatch(new TestJob($this->valuestore));
|
||||||
|
|
||||||
|
$tenant2 = Tenant::create([
|
||||||
|
'id' => 'foobar',
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
|
$this->assertFalse($this->valuestore->has('tenant_id'));
|
||||||
|
$this->artisan('queue:work --once');
|
||||||
|
|
||||||
|
$this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestJob implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/** @var Valuestore */
|
||||||
|
protected $valuestore;
|
||||||
|
|
||||||
|
public function __construct(Valuestore $valuestore)
|
||||||
|
{
|
||||||
|
$this->valuestore = $valuestore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->valuestore->put('tenant_id', "The current tenant id is: " . tenant('id'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -130,6 +130,8 @@ class TenantModelTest extends TestCase
|
||||||
|
|
||||||
$this->assertTrue(tenant() instanceof AnotherTenant);
|
$this->assertTrue(tenant() instanceof AnotherTenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo test that tenant can be created even in another DB context - that the central trait works
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyTenant extends Tenant
|
class MyTenant extends Tenant
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue