1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 20:14:04 +00:00

Make with-pending VALUE_NONE

This commit is contained in:
lukinovec 2022-10-05 13:35:23 +02:00
parent 72f742e53d
commit af689f816a
2 changed files with 41 additions and 83 deletions

View file

@ -13,37 +13,14 @@ use Symfony\Component\Console\Input\InputOption;
*/ */
trait HasTenantOptions trait HasTenantOptions
{ {
/** Value indicating an option wasn't passed */
protected $optionNotPassedValue = 'not passed';
protected function getOptions() protected function getOptions()
{ {
return array_merge([ return array_merge([
['tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, '', null], ['tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, '', null],
['with-pending', null, InputOption::VALUE_OPTIONAL, 'include pending tenants in query', $this->optionNotPassedValue], ['with-pending', null, InputOption::VALUE_NONE, 'include pending tenants in query'],
], parent::getOptions()); ], parent::getOptions());
} }
protected function getWithPendingOption(): bool
{
$optionPassedWithoutArgument = is_null($this->option('with-pending'));
$optionPassedWithArgument = $this->option('with-pending') !== $this->optionNotPassedValue;
// E.g. 'tenants:run --with-pending'
if ($optionPassedWithoutArgument) {
return true;
}
// E.g. 'tenants:run --with-pending=false'
// If the passed value can't get converted to a bool (e.g. --with-pending=foo), default to false
if ($optionPassedWithArgument) {
return filter_var($this->option('with-pending'), FILTER_VALIDATE_BOOLEAN);
}
// Option not passed, e.g. tenants:run
return config('tenancy.pending.include_in_queries');
}
protected function getTenants(): LazyCollection protected function getTenants(): LazyCollection
{ {
return tenancy() return tenancy()
@ -52,7 +29,7 @@ trait HasTenantOptions
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants')); $query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
}) })
->when(tenancy()->model()::hasGlobalScope(PendingScope::class), function ($query) { ->when(tenancy()->model()::hasGlobalScope(PendingScope::class), function ($query) {
$query->withPending($this->getWithPendingOption()); $query->withPending(config('tenancy.pending.include_in_queries') ?: $this->option('with-pending'));
}) })
->cursor(); ->cursor();
} }

View file

@ -239,64 +239,7 @@ test('link command works with a specified tenant', function() {
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey")); $this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
}); });
test('passing with pending option as false makes tenant commands not include pending tenants', function() { test('commands do not run for pending tenants if tenancy.pending.include_in_queries is false and the with pending option does not get passed', function() {
$tenants = collect([
Tenant::create(),
Tenant::create(),
Tenant::createPending(),
Tenant::createPending(),
]);
pest()->artisan('tenants:migrate --with-pending');
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz' --with-pending=false");
$pendingTenants = $tenants->filter->pending();
$readyTenants = $tenants->reject->pending();
$pendingTenants->each(fn($tenant) => $artisan->doesntExpectOutputToContain("Tenant: {$tenant->getTenantKey()}"));
$readyTenants->each(fn($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
$artisan->assertExitCode(0);
});
test('passing with pending option as true makes tenant commands include pending tenants', function() {
$tenants = collect([
Tenant::create(),
Tenant::create(),
Tenant::createPending(),
Tenant::createPending(),
]);
pest()->artisan('tenants:migrate --with-pending');
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz' --with-pending");
$tenants->each(fn($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
$artisan->assertExitCode(0);
});
test('not passing the with pending option makes tenant commands include the pending tenants if tenancy.pending.include_in_queries is true', function() {
config(['tenancy.pending.include_in_queries' => true]);
$tenants = collect([
Tenant::create(),
Tenant::create(),
Tenant::createPending(),
Tenant::createPending(),
]);
pest()->artisan('tenants:migrate --with-pending');
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz'");
$tenants->each(fn($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
$artisan->assertExitCode(0);
});
test('not passing the with pending option makes tenant commands exclude the pending tenants if tenancy.pending.include_in_queries is false', function() {
config(['tenancy.pending.include_in_queries' => false]); config(['tenancy.pending.include_in_queries' => false]);
$tenants = collect([ $tenants = collect([
@ -319,6 +262,44 @@ test('not passing the with pending option makes tenant commands exclude the pend
$artisan->assertExitCode(0); $artisan->assertExitCode(0);
}); });
test('commands run for pending tenants too if tenancy.pending.include_in_queries is true', function() {
config(['tenancy.pending.include_in_queries' => true]);
$tenants = collect([
Tenant::create(),
Tenant::create(),
Tenant::createPending(),
Tenant::createPending(),
]);
pest()->artisan('tenants:migrate --with-pending');
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz'");
$tenants->each(fn($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
$artisan->assertExitCode(0);
});
test('commands run for pending tenants too if the with pending option is passed', function() {
config(['tenancy.pending.include_in_queries' => false]);
$tenants = collect([
Tenant::create(),
Tenant::create(),
Tenant::createPending(),
Tenant::createPending(),
]);
pest()->artisan('tenants:migrate --with-pending');
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz' --with-pending");
$tenants->each(fn($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
$artisan->assertExitCode(0);
});
test('run command works when sub command asks questions and accepts arguments', function () { test('run command works when sub command asks questions and accepts arguments', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$id = $tenant->getTenantKey(); $id = $tenant->getTenantKey();