Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.1.0] Central queue connections #181

Merged
merged 4 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/TenancyBootstrappers/QueueTenancyBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Stancl\Tenancy\TenancyBootstrappers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Config\Repository;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Testing\Fakes\QueueFake;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Tenant;
Expand All @@ -14,19 +15,18 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
/** @var bool Has tenancy been started. */
public $started = false;

/** @var Application */
protected $app;
/** @var Repository */
protected $config;

public function __construct(Application $app)
public function __construct(Repository $config, QueueManager $queue)
{
$this->app = $app;
$this->config = $config;

$bootstrapper = &$this;

$queue = $this->app['queue'];
if (! $queue instanceof QueueFake) {
$queue->createPayloadUsing(function () use (&$bootstrapper) {
return $bootstrapper->getPayload();
$queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
return $bootstrapper->getPayload($connection);
});
}
}
Expand All @@ -41,12 +41,16 @@ public function end()
$this->started = false;
}

public function getPayload()
public function getPayload(string $connection)
{
if (! $this->started) {
return [];
}

if ($this->config["queue.connections.$connection.central"]) {
return [];
}

$id = tenant('id');

return [
Expand Down
13 changes: 13 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function tenancy_is_initialized_inside_queues()
return $event->job->payload()['tenant_id'] === tenant('id');
});
}

/** @test */
public function tenancy_is_not_initialized_in_non_tenant_queues()
{
$this->loadLaravelMigrations(['--database' => 'tenant']);
Event::fake();

dispatch(new TestJob())->onConnection('central');

Event::assertDispatched(JobProcessing::class, function ($event) {
return ! isset($event->job->payload()['tenant_id']);
});
}
}

class TestJob implements ShouldQueue
Expand Down
8 changes: 4 additions & 4 deletions tests/TenantStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Stancl\Tenancy\Tests;

use Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver;
use Stancl\Tenancy\StorageDrivers\Database\TenantModel;
use Stancl\Tenancy\StorageDrivers\RedisStorageDriver;
use Stancl\Tenancy\Tenant;

class TenantStorageTest extends TestCase
Expand Down Expand Up @@ -84,10 +82,12 @@ public function associative_arrays_can_be_stored()
/** @test */
public function correct_storage_driver_is_used()
{
if (config('tenancy.storage_driver') == DatabaseStorageDriver::class) {
if (config('tenancy.storage_driver') == 'db') {
$this->assertSame('DatabaseStorageDriver', class_basename(tenancy()->storage));
} elseif (config('tenancy.storage_driver') == RedisStorageDriver::class) {
} elseif (config('tenancy.storage_driver') == 'redis') {
$this->assertSame('RedisStorageDriver', class_basename(tenancy()->storage));
} else {
dd(class_basename(config('tenancy.storage_driver')));
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ protected function getEnvironmentSetUp($app)
'tenancy.migrations_directory' => database_path('../migrations'),
'tenancy.storage_drivers.db.connection' => 'central',
'tenancy.bootstrappers.redis' => \Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class,
'queue.connections.central' => [
'driver' => 'sync',
'central' => true,
],
]);

$app->singleton(\Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class);
Expand Down