diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index ec30e24..222c940 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -428,22 +428,26 @@ protected function runMigrations(): void protected function createAdminUser(string $kebabName, string $serviceKey = '', string $serviceName = ''): array { - $adminEmail = when(empty($serviceKey), "admin@{$kebabName}.com", "admin.{$serviceKey}@{$kebabName}.com"); + $isServiceAdmin = (!empty($serviceKey) && !empty($serviceName)); + + $adminEmail = when($isServiceAdmin, "admin.{$serviceKey}@{$kebabName}.com", "admin@{$kebabName}.com"); $defaultPassword = substr(md5(uniqid()), 0, 8); - $serviceLabel = when(!empty($serviceName), " for {$serviceName}"); + $serviceLabel = when($isServiceAdmin, " for {$serviceName}"); $adminCredentials = [ 'email' => $this->ask("Please enter admin email{$serviceLabel}", $adminEmail), 'password' => $this->ask("Please enter admin password{$serviceLabel}", $defaultPassword), ]; + $adminName = when($isServiceAdmin, "{$serviceName} Admin", 'Admin'); + if ($this->authType === AuthTypeEnum::None) { - $adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", "{$serviceName} Admin"); + $adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", $adminName); $adminCredentials['role_id'] = $this->ask("Please enter admin role id{$serviceLabel}", RoleEnum::Admin->value); } - if (empty($serviceName)) { + if (!$isServiceAdmin) { $this->adminCredentials = $adminCredentials; } diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php old mode 100644 new mode 100755 index 4316a0e..286a293 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -2,6 +2,11 @@ namespace RonasIT\ProjectInitializator\Tests; +use Mockery; +use ReflectionMethod; +use ReflectionProperty; +use RonasIT\ProjectInitializator\Commands\InitCommand; +use RonasIT\ProjectInitializator\Enums\AuthTypeEnum; use RonasIT\ProjectInitializator\Tests\Support\Traits\InitCommandMockTrait; class InitCommandTest extends TestCase @@ -926,4 +931,21 @@ public function testRunWithClerkAdditionalAdminsWithoutDefaultAdmin(): void ->expectsConfirmation('Do you want to uninstall project-initializator package?') ->assertExitCode(0); } + + public function testDefaultAdminsCredentials(): void + { + $commandMock = Mockery::mock(InitCommand::class)->shouldAllowMockingProtectedMethods(); + + $commandMock->shouldReceive('ask')->andReturnUsing(fn ($question, $default) => $default); + $commandMock->shouldReceive('publishAdminMigration')->andReturnNull(); + + $authTypeProperty = new ReflectionProperty(InitCommand::class, 'authType'); + $authTypeProperty->setAccessible(true); + $authTypeProperty->setValue($commandMock, AuthTypeEnum::None); + + $createAdminMethod = new ReflectionMethod(InitCommand::class, 'createAdminUser'); + $credentials = $createAdminMethod->invokeArgs($commandMock, ['my-app']); + + $this->assertEquals('Admin', $credentials['name']); + } }