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

[4.x] Add batch tenancy queue bootstrapper #874

Merged
merged 20 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ env:

on:
push:
branches: [ 3.x, 2.x, master ]
branches: [ 3.x, 2.x ]
pull_request:
branches: [ 3.x, 2.x, master ]
branches: [ 3.x, 2.x ]

jobs:
tests:
Expand Down
1 change: 1 addition & 0 deletions assets/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
// Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved
],

/**
Expand Down
56 changes: 56 additions & 0 deletions src/Bootstrappers/BatchTenancyBootstrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Stancl\Tenancy\Bootstrappers;

use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Support\Facades\DB;
use ReflectionClass;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;

class BatchTenancyBootstrapper implements TenancyBootstrapper
{
/**
* The database previous connection instance.
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved
*
* @var \Illuminate\Database\Connection
*/
protected $previousConnection = null;
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved

public function bootstrap(Tenant $tenant)
{
$batchRepository = app(BatchRepository::class);
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved

if ($batchRepository instanceof DatabaseBatchRepository) {
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Access the resolved batch repository instance and update its connection to use the tenant connection
*/
$batchRepositoryReflection = new ReflectionClass($batchRepository);
$connectionProperty = $batchRepositoryReflection->getProperty('connection');
$connectionProperty->setAccessible(true);
$connection = $connectionProperty->getValue($batchRepository);

$this->previousConnection = $connection;

$connectionProperty->setValue($batchRepository, DB::connection('tenant'));
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved
}
}

public function revert()
{
if ($this->previousConnection) {
/**
* Access the resolved batch repository instance and replace its connection with the previously replaced one
*/
$batchRepository = app(BatchRepository::class);

$batchRepositoryReflection = new ReflectionClass($batchRepository);
$connectionProperty = $batchRepositoryReflection->getProperty('connection');
$connectionProperty->setAccessible(true);

$connectionProperty->setValue($batchRepository, $this->previousConnection);
$this->previousConnection = null;
}
}
}
71 changes: 71 additions & 0 deletions tests/BatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types = 1);

namespace Stancl\Tenancy\Tests;

use Illuminate\Bus\BatchRepository;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use ReflectionClass;
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;

class BatchTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

$this->app->singleton(BatchTenancyBootstrapper::class);
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved

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();
}

}