From 39c1e65172e5b8881a856a226443527360e71588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?michael=20lundb=C3=B8l?= Date: Wed, 6 May 2020 21:59:16 +0200 Subject: [PATCH] Add a hook that should run before trying to fetch tenants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: michael lundbøl --- src/Traits/TenantAwareCommand.php | 15 ++++++++ tests/Etc/AddUserConditionally.php | 47 +++++++++++++++++++++++++ tests/Etc/ConsoleKernel.php | 1 + tests/Traits/TenantAwareCommandTest.php | 40 +++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 tests/Etc/AddUserConditionally.php diff --git a/src/Traits/TenantAwareCommand.php b/src/Traits/TenantAwareCommand.php index fb11df9f..964e8010 100644 --- a/src/Traits/TenantAwareCommand.php +++ b/src/Traits/TenantAwareCommand.php @@ -13,6 +13,12 @@ trait TenantAwareCommand /** @return int */ protected function execute(InputInterface $input, OutputInterface $output) { + $callback = $this->runBeforeRunningTenantsHook(); + + if ($callback !== 0) { + return $callback; + } + $tenants = $this->getTenants(); $exitCode = 0; @@ -35,4 +41,13 @@ trait TenantAwareCommand * @return Tenant[] */ abstract protected function getTenants(): array; + + private function runBeforeRunningTenantsHook() + { + if (! method_exists($this, 'beforeRunningTenants')) { + return; + } + + return (int) $this->laravel->call([$this, 'beforeRunningTenants']); + } } diff --git a/tests/Etc/AddUserConditionally.php b/tests/Etc/AddUserConditionally.php new file mode 100644 index 00000000..95b46fa7 --- /dev/null +++ b/tests/Etc/AddUserConditionally.php @@ -0,0 +1,47 @@ +option('stop')) { + $this->error('You stopped the command conditionally'); + return 1; + } + + return 0; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + User::create([ + 'name' => Str::random(10), + 'email' => Str::random(10) . '@gmail.com', + 'email_verified_at' => now(), + 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'remember_token' => Str::random(10), + ]); + } +} diff --git a/tests/Etc/ConsoleKernel.php b/tests/Etc/ConsoleKernel.php index 1bc66365..61caba7b 100644 --- a/tests/Etc/ConsoleKernel.php +++ b/tests/Etc/ConsoleKernel.php @@ -16,5 +16,6 @@ class ConsoleKernel extends Kernel protected $commands = [ ExampleCommand::class, AddUserCommand::class, + AddUserConditionally::class ]; } diff --git a/tests/Traits/TenantAwareCommandTest.php b/tests/Traits/TenantAwareCommandTest.php index fcc3d0e2..41728271 100644 --- a/tests/Traits/TenantAwareCommandTest.php +++ b/tests/Traits/TenantAwareCommandTest.php @@ -32,4 +32,44 @@ class TenantAwareCommandTest extends TestCase $this->assertNotEmpty(\DB::table('users')->get()); tenancy()->end(); } + + /** @test */ + public function commands_can_optionally_specify_a_hook_that_should_be_ran_before_running_the_command_within_a_tenant() + { + $tenant1 = Tenant::new()->save(); + $tenant2 = Tenant::new()->save(); + + \Artisan::call('tenants:migrate', [ + '--tenants' => [$tenant1['id'], $tenant2['id']], + ]); + + $this->withoutMockingConsoleOutput(); + $response = $this->artisan('user:add_conditionally', [ + '--tenants' => [$tenant1['id'], $tenant2['id']], + '--stop' => true + ]); + + $this->assertEquals(1, $response); + + tenancy()->initializeTenancy($tenant1); + $this->assertEmpty(\DB::table('users')->get()); + tenancy()->end(); + + tenancy()->initializeTenancy($tenant2); + $this->assertEmpty(\DB::table('users')->get()); + tenancy()->end(); + + $this->artisan('user:add_conditionally', [ + '--tenants' => [$tenant1['id'], $tenant2['id']], + '--stop' => false + ]); + + tenancy()->initializeTenancy($tenant1); + $this->assertNotEmpty(\DB::table('users')->get()); + tenancy()->end(); + + tenancy()->initializeTenancy($tenant2); + $this->assertNotEmpty(\DB::table('users')->get()); + tenancy()->end(); + } }