Skip to content

Commit 3d0cb5d

Browse files
authored
Merge pull request #75 from RonasIT/55-admin-name-fix
[55] fix admin name question option
2 parents d287e9b + 3e8fd00 commit 3d0cb5d

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/Commands/InitCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,26 @@ protected function runMigrations(): void
428428

429429
protected function createAdminUser(string $kebabName, string $serviceKey = '', string $serviceName = ''): array
430430
{
431-
$adminEmail = when(empty($serviceKey), "admin@{$kebabName}.com", "admin.{$serviceKey}@{$kebabName}.com");
431+
$isServiceAdmin = (!empty($serviceKey) && !empty($serviceName));
432+
433+
$adminEmail = when($isServiceAdmin, "admin.{$serviceKey}@{$kebabName}.com", "admin@{$kebabName}.com");
432434
$defaultPassword = substr(md5(uniqid()), 0, 8);
433435

434-
$serviceLabel = when(!empty($serviceName), " for {$serviceName}");
436+
$serviceLabel = when($isServiceAdmin, " for {$serviceName}");
435437

436438
$adminCredentials = [
437439
'email' => $this->ask("Please enter admin email{$serviceLabel}", $adminEmail),
438440
'password' => $this->ask("Please enter admin password{$serviceLabel}", $defaultPassword),
439441
];
440442

443+
$adminName = when($isServiceAdmin, "{$serviceName} Admin", 'Admin');
444+
441445
if ($this->authType === AuthTypeEnum::None) {
442-
$adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", "{$serviceName} Admin");
446+
$adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", $adminName);
443447
$adminCredentials['role_id'] = $this->ask("Please enter admin role id{$serviceLabel}", RoleEnum::Admin->value);
444448
}
445449

446-
if (empty($serviceName)) {
450+
if (!$isServiceAdmin) {
447451
$this->adminCredentials = $adminCredentials;
448452
}
449453

tests/InitCommandTest.php

100644100755
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace RonasIT\ProjectInitializator\Tests;
44

5+
use Mockery;
6+
use ReflectionMethod;
7+
use ReflectionProperty;
8+
use RonasIT\ProjectInitializator\Commands\InitCommand;
9+
use RonasIT\ProjectInitializator\Enums\AuthTypeEnum;
510
use RonasIT\ProjectInitializator\Tests\Support\Traits\InitCommandMockTrait;
611

712
class InitCommandTest extends TestCase
@@ -926,4 +931,21 @@ public function testRunWithClerkAdditionalAdminsWithoutDefaultAdmin(): void
926931
->expectsConfirmation('Do you want to uninstall project-initializator package?')
927932
->assertExitCode(0);
928933
}
934+
935+
public function testDefaultAdminsCredentials(): void
936+
{
937+
$commandMock = Mockery::mock(InitCommand::class)->shouldAllowMockingProtectedMethods();
938+
939+
$commandMock->shouldReceive('ask')->andReturnUsing(fn ($question, $default) => $default);
940+
$commandMock->shouldReceive('publishAdminMigration')->andReturnNull();
941+
942+
$authTypeProperty = new ReflectionProperty(InitCommand::class, 'authType');
943+
$authTypeProperty->setAccessible(true);
944+
$authTypeProperty->setValue($commandMock, AuthTypeEnum::None);
945+
946+
$createAdminMethod = new ReflectionMethod(InitCommand::class, 'createAdminUser');
947+
$credentials = $createAdminMethod->invokeArgs($commandMock, ['my-app']);
948+
949+
$this->assertEquals('Admin', $credentials['name']);
950+
}
929951
}

0 commit comments

Comments
 (0)