Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the terminal enable list view for the Prompts combo box #328

Open
wants to merge 5 commits into
base: tm/upgrade-laravel-zero
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions app/Commands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use function Laravel\Prompts\search;

class EnableCommand extends Command
{
Expand Down Expand Up @@ -47,7 +48,6 @@ public function handle(Environment $environment, Services $services): void
}

$option = $this->selectService();

if (! $option) {
return;
}
Expand Down Expand Up @@ -130,18 +130,21 @@ private function selectService(): ?string

private function defaultMenu(): ?string
{
$option = $this->menu(self::MENU_TITLE)->setTitleSeparator('=');

foreach ($this->enableableServicesByCategory() as $category => $services) {
$separator = str_repeat('-', 1 + Str::length($category));

$option->addStaticItem("{$category}:")
->addStaticItem($separator)
->addOptions($this->menuItemsForServices($services))
->addLineBreak('', 1);
}
$servicesList = collect($this->enableableServicesByCategory())->flatMap(function ($services, $category) {
return collect($this->menuItemsForServices($services))->mapWithKeys(function ($row, $key) use ($category) {
return [$key => "{$category}: {$row}"];
})->toArray();
})->toArray();

return $option->open();
return search(
label: self::MENU_TITLE,
options: fn (string $value) => strlen($value) > 0
? collect($servicesList)->filter(function ($row) use ($value) {
return str($row)->lower()->contains(str($value)->lower());
})->toArray()
: $servicesList,
scroll: 10
);
}

private function windowsMenu($category = null): ?string
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"ext-json": "*",
"ext-pcntl": "*",
"ext-posix": "*",
"guzzlehttp/psr7": "^1.7"
"guzzlehttp/psr7": "^1.7",
"laravel/prompts": "^0.1.24"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.5",
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/DisableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function it_can_disable_a_service_from_menu()
array_push($disableableServices, '<info>Exit</>');

$this->artisan('disable')
->expectsChoice('Takeout containers to disable', $postgressName, $disableableServices)
->expectsChoice('Takeout containers to disable', $postgressName, array_values($disableableServices))
->assertExitCode(0);
} else {
$menuMock = $this->mock(Menu::class, function ($mock) use ($postgressId) {
Expand Down
50 changes: 24 additions & 26 deletions tests/Feature/EnableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use App\Services\MeiliSearch;
use App\Services\PostgreSql;
use App\Shell\Docker;
use Illuminate\Console\Command;
use NunoMaduro\LaravelConsoleMenu\Menu;
use PHPUnit\Framework\Assert;
use Tests\TestCase;

class EnableCommandTest extends TestCase
Expand All @@ -33,14 +30,6 @@ function it_can_enable_a_service_from_menu()
$postgres = 'postgresql' => $fqcn = 'App\Services\PostgreSql',
];

$menuItems = [
'<fg=white;bg=blue;options=bold> DATABASE </>',
'PostgreSQL',
'<fg=white;bg=blue;options=bold> SEARCH </>',
'MeiliSearch',
'<info>Exit</>',
];

$this->mock(Docker::class, function ($mock) {
$mock->shouldReceive('isInstalled')->andReturn(true);
$mock->shouldReceive('isDockerServiceRunning')->andReturn(true);
Expand All @@ -56,28 +45,37 @@ function it_can_enable_a_service_from_menu()
});

if ($this->isWindows()) {
$menuItems = [
'<fg=white;bg=blue;options=bold> DATABASE </>',
'PostgreSQL',
'<fg=white;bg=blue;options=bold> SEARCH </>',
'MeiliSearch',
'<info>Exit</>',
];

$this->artisan('enable')
->expectsChoice('Takeout containers to enable', 'PostgreSQL', $menuItems)
->assertExitCode(0);
} else {
$menuMock = $this->mock(Menu::class, function ($mock) use ($postgres) {
$mock->shouldReceive('setTitleSeparator')->andReturnSelf();
$mock->shouldReceive('addStaticItem')->andReturnSelf()->times(4);
$mock->shouldReceive('addOptions')->andReturnSelf();
$mock->shouldReceive('addLineBreak')->andReturnSelf();
$mock->shouldReceive('open')->andReturn($postgres)->once();
});
$menuItems = [
'Database: PostgreSQL',
'Search: MeiliSearch',
'meilisearch',
$postgres,
];

Command::macro(
'menu',
function (string $title) use ($menuMock) {
Assert::assertEquals('Takeout containers to enable', $title);
$this->artisan('enable')
->expectsChoice('Takeout containers to enable', '', $menuItems)
->assertExitCode(0);

return $menuMock;
}
);
$menuItems = [
'Database: PostgreSQL',
$postgres,
];

$this->artisan('enable');
$this->artisan('enable')
->expectsChoice('Takeout containers to enable', $postgres, $menuItems)
->assertExitCode(0);
}
}

Expand Down
Loading