diff --git a/src/Concerns/HasTenantOptions.php b/src/Concerns/HasTenantOptions.php index 1324fa24..f8a763a7 100644 --- a/src/Concerns/HasTenantOptions.php +++ b/src/Concerns/HasTenantOptions.php @@ -13,37 +13,14 @@ use Symfony\Component\Console\Input\InputOption; */ trait HasTenantOptions { - /** Value indicating an option wasn't passed */ - protected $optionNotPassedValue = 'not passed'; - protected function getOptions() { return array_merge([ ['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()); } - 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 { return tenancy() @@ -52,7 +29,7 @@ trait HasTenantOptions $query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants')); }) ->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(); } diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 260f5309..2e982db5 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -239,64 +239,7 @@ test('link command works with a specified tenant', function() { $this->assertDirectoryDoesNotExist(public_path("public-$tenantKey")); }); -test('passing with pending option as false makes tenant commands not 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=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() { +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() { config(['tenancy.pending.include_in_queries' => false]); $tenants = collect([ @@ -319,6 +262,44 @@ test('not passing the with pending option makes tenant commands exclude the pend $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 () { $tenant = Tenant::create(); $id = $tenant->getTenantKey();