Skip to content

Commit

Permalink
update tests and doc blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
macocci7 committed May 28, 2024
1 parent f87c77c commit 5e7a58a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
11 changes: 5 additions & 6 deletions playground/fileselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
require __DIR__.'/../vendor/autoload.php';

$model = fileselector(
label: 'What model should the policy apply to?',
placeholder: 'E.g. User',
validate: fn ($value) => match (true) {
strlen($value) === 0 => 'Please enter a model name.',
label: 'Select a file to import.',
placeholder: 'E.g. ./vendor/autoload.php',
validate: fn (string $value) => match (true) {
!is_readable($value) === 0 => 'Cannot read the file.',
default => null,
},
hint: 'The model name should be singular.',
hint: 'Input the file path.',
extensions: [
'.md',
'.json',
'.php',
],
Expand Down
9 changes: 7 additions & 2 deletions src/FileSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected function glob(string $path)
* @param string $path
* @return string[]
*/
protected function entries(string $path)
protected function entries(string $path): array
{
return collect(iterator_to_array($this->glob($path)))
->reject(fn (string $entry) => match (true) {
Expand All @@ -185,7 +185,12 @@ protected function entries(string $path)
->all();
}

private function isRejectable(string $entry)
/**
* Ditermines whether the entry should be rejected
*
* @param string $entry
*/
private function isRejectable(string $entry): bool
{
if (is_dir($entry)) {
return false;
Expand Down
25 changes: 19 additions & 6 deletions tests/Feature/FileSelectorTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use Laravel\Prompts\Exceptions\NonInteractiveValidationException;
use Laravel\Prompts\FileSelector;
use Laravel\Prompts\Key;
use Laravel\Prompts\Exceptions\NonInteractiveValidationException;
use Laravel\Prompts\Prompt;
use Laravel\Prompts\FileSelector;

use function Laravel\Prompts\fileselector;

Expand All @@ -16,23 +16,23 @@
});

it('completes the input using the tab key', function () {
Prompt::fake(['v', 'e', 'n', 'd', 'o', 'r', Key::DOWN, Key::TAB, Key::ENTER]);
Prompt::fake(['v', Key::DOWN, Key::TAB, Key::ENTER]);

$result = fileselector('Select a file.');

expect($result)->toBe('./vendor/');
});

it('completes the input using the arrow keys', function () {
Prompt::fake(['s', 'r', 'c', '/', 'C', Key::DOWN, Key::DOWN, Key::UP, Key::ENTER]);
Prompt::fake(['.', 'g', Key::DOWN, Key::DOWN, Key::UP, Key::ENTER]);

$result = fileselector('Select a file.');

expect($result)->toBe('src/Concerns/');
expect($result)->toBe('./.git/');
});

it('supports the home key while navigating options', function () {
Prompt::fake([Key::DOWN, Key::DOWN, Key::HOME[0], Key::ENTER]);
Prompt::fake([Key::DOWN, Key::DOWN, Key::DOWN, Key::HOME[0], Key::ENTER]);

$result = fileselector('Select a file.');

Expand Down Expand Up @@ -134,3 +134,16 @@

expect($result)->toBe('./vendor/autoload.php');
});

it('filter dir entries with specified extensions', function () {
Prompt::fake(['c', 'o', 'm', 'p', 'o', 's', 'e', 'r', Key::UP, Key::ENTER]);

$result = fileselector(
label: 'Select a file.',
extensions: [
'.json',
],
);

expect($result)->toBe('./composer.json');
});

0 comments on commit 5e7a58a

Please sign in to comment.