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

Added confirmation and correction possibility to the insert command #8

Merged
merged 2 commits into from
Mar 31, 2021
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
30 changes: 18 additions & 12 deletions src/Console/LarexInsertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,24 @@ public function handle(): int
//initialize data
$data = collect([]);

//get group
$data->put('group', $this->anticipate('Enter the group', $availableGroups));

//get key
$data->put('key', $this->anticipate('Enter the key', $availableKeys));

foreach ($languages as $i => $language) {
$count = $i + 1;
$value = $this->ask("[{$count}/{$languages->count()}] Enter the value for [{$language}] language");

$data->put($language, $value);
}
// iterate until user confirm the inserted data
do {
//get group
$data->put('group', $this->anticipate('Enter the group', $availableGroups, $data->get('group')));

//get key
$data->put('key', $this->anticipate('Enter the key', $availableKeys, $data->get('key')));

foreach ($languages as $i => $language) {
$count = $i + 1;
$value = $this->ask(
"[{$count}/{$languages->count()}] Enter the value for [{$language}] language",
$data->get($language)
);

$data->put($language, $value);
}
} while ($this->askWithCompletion('Are you sure?', ['yes', 'no'], 'yes') !== 'yes');

//append to csv
$csv->push($data->values()->toArray());
Expand Down
48 changes: 48 additions & 0 deletions tests/LarexInsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function test_insert_command(): void
->expectsQuestion('Enter the key', 'uncle')
->expectsQuestion('[1/2] Enter the value for [en] language', 'Uncle')
->expectsQuestion('[2/2] Enter the value for [it] language', 'Zio')
->expectsQuestion('Are you sure?', 'yes')
->expectsOutput('Item added successfully.')
->run();

Expand All @@ -38,6 +39,7 @@ public function test_insert_command_with_export(): void
->expectsQuestion('Enter the key', 'uncle')
->expectsQuestion('[1/2] Enter the value for [en] language', 'Uncle')
->expectsQuestion('[2/2] Enter the value for [it] language', 'Zio')
->expectsQuestion('Are you sure?', 'yes')
->expectsOutput('Item added successfully.')
->expectsOutput('')
->expectsOutput("Processing the '" . config('larex.csv.path') . "' file...")
Expand All @@ -47,4 +49,50 @@ public function test_insert_command_with_export(): void

self::assertEquals(0, $result);
}

public function test_insert_command_with_correction() :void
{
$this->initFromStub('insert/input');

$result = $this->artisan('larex:insert')
->expectsQuestion('Enter the group', 'app')
->expectsQuestion('Enter the key', 'dad')
->expectsQuestion('[1/2] Enter the value for [en] language', 'Dad')
->expectsQuestion('[2/2] Enter the value for [it] language', 'Papa')
->expectsQuestion('Are you sure?', 'no')
->expectsQuestion('Enter the group', 'app')
->expectsQuestion('Enter the key', 'dad')
->expectsQuestion('[1/2] Enter the value for [en] language', 'Dad')
->expectsQuestion('[2/2] Enter the value for [it] language', 'Papà')
->expectsQuestion('Are you sure?', 'yes')
->expectsOutput('Item added successfully.')
->run();

self::assertEquals(0, $result);
}

public function test_insert_command_with_correction_and_export() :void
{
$this->initFromStub('insert/input');

$result = $this->artisan('larex:insert --export')
->expectsQuestion('Enter the group', 'app')
->expectsQuestion('Enter the key', 'dad')
->expectsQuestion('[1/2] Enter the value for [en] language', 'Dad')
->expectsQuestion('[2/2] Enter the value for [it] language', 'Papa')
->expectsQuestion('Are you sure?', 'no')
->expectsQuestion('Enter the group', 'app')
->expectsQuestion('Enter the key', 'dad')
->expectsQuestion('[1/2] Enter the value for [en] language', 'Dad')
->expectsQuestion('[2/2] Enter the value for [it] language', 'Papà')
->expectsQuestion('Are you sure?', 'yes')
->expectsOutput('Item added successfully.')
->expectsOutput('')
->expectsOutput("Processing the '" . config('larex.csv.path') . "' file...")
->expectsOutput('resources/lang/en/app.php created successfully.')
->expectsOutput('resources/lang/it/app.php created successfully.')
->run();

self::assertEquals(0, $result);
}
}