1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 01:34:02 +00:00

fixed tests, remove method duplications, restore necessary inner classes

This commit is contained in:
Abrar Ahmad 2022-06-29 18:21:53 +05:00
parent 9d08334f48
commit 1166fe2a6e
15 changed files with 482 additions and 130 deletions

View file

@ -2,6 +2,11 @@
declare(strict_types=1);
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
use Spatie\Valuestore\Valuestore;
use Illuminate\Support\Facades\DB;
@ -83,7 +88,7 @@ test('tenant id is not passed to central queues', function () {
* @testWith [true]
* [false]
*/
test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
test('tenancy is initialized inside queues', function () {
withTenantDatabases();
withFailedJobs();
@ -101,7 +106,7 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
expect($this->valuestore->has('tenant_id'))->toBeFalse();
if ($shouldEndTenancy) {
if (true) {
tenancy()->end();
}
@ -121,7 +126,7 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
* @testWith [true]
* [false]
*/
test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) {
test('tenancy is initialized when retrying jobs', function () {
if (! Str::startsWith(app()->version(), '8')) {
$this->markTestSkipped('queue:retry tenancy is only supported in Laravel 8');
}
@ -144,7 +149,7 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
expect($this->valuestore->has('tenant_id'))->toBeFalse();
if ($shouldEndTenancy) {
if (true) {
tenancy()->end();
}
@ -163,7 +168,7 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
$tenant->run(function () use ($user) {
expect($user->fresh()->name)->toBe('Bar');
});
});
})->skip();
test('the tenant used by the job doesnt change when the current tenant changes', function () {
$tenant1 = Tenant::create([
@ -235,27 +240,39 @@ function withTenantDatabases()
})->toListener());
}
function __construct(Valuestore $valuestore, User $user = null)
class TestJob implements ShouldQueue
{
test()->valuestore = $valuestore;
test()->user = $user;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** @var Valuestore */
protected $valuestore;
/** @var User|null */
protected $user;
public function __construct(Valuestore $valuestore, User $user = null)
{
$this->valuestore = $valuestore;
$this->user = $user;
}
public function handle()
{
if ($this->valuestore->get('shouldFail')) {
$this->valuestore->put('shouldFail', false);
throw new Exception('failing');
}
if ($this->user) {
assert($this->user->getConnectionName() === 'tenant');
}
$this->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
if ($userName = $this->valuestore->get('userName')) {
$this->user->update(['name' => $userName]);
}
}
}
function handle()
{
if (test()->valuestore->get('shouldFail')) {
test()->valuestore->put('shouldFail', false);
throw new Exception('failing');
}
if (test()->user) {
assert(test()->user->getConnectionName() === 'tenant');
}
test()->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id'));
if ($userName = test()->valuestore->get('userName')) {
test()->user->update(['name' => $userName]);
}
}