Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The when() helper on this line is missing the third parameter (default value). When $isServiceAdmin is false, this will return null instead of an empty string. This could result in the service label being null rather than empty, which may cause type inconsistencies or unexpected output.

Consider changing to:

$serviceLabel = when($isServiceAdmin, " for {$serviceName}", '');
Suggested change
$serviceLabel = when($isServiceAdmin, " for {$serviceName}");
$serviceLabel = when($isServiceAdmin, " for {$serviceName}", '');

Copilot uses AI. Check for mistakes.

$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;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/InitCommandTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']);
}
}