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

[WIP] add BatchTenancyBootstrapper and tests #9

Open
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ env:

on:
push:
branches: [ master ]
branches: [ master, 3.x]
pull_request:
branches: [ master ]
branches: [ master, 3.x] # todo@1 remove 3.x before merge

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,
],

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

declare(strict_types=1);

namespace Stancl\Tenancy\Bootstrappers;

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

class BatchTenancyBootstrapper implements TenancyBootstrapper
{
/**
* The database previous connection instance.
*
* @var \Illuminate\Database\Connection
*/
protected $previousConnection = null;

public function bootstrap(Tenant $tenant)
{
$batchRepository = app(BatchRepository::class);

if ($batchRepository instanceof DatabaseBatchRepository) {
// Access the resolved batch repository instance and update its connection to use the tenant connection
$this->previousConnection = $batchRepository->getConnection();
$batchRepository->setConnection(DB::connection('tenant'));
}
}

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);
$batchRepository->setConnection($this->previousConnection);
$this->previousConnection = null;
}
}
}
44 changes: 44 additions & 0 deletions tests/BatchTest.php
Original file line number Diff line number Diff line change
@@ -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 () {
$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('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()
{
return app(BatchRepository::class)->getConnection()->getName();
}