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

Add new method promptByMultipleKeys() in CLI class #6302

Merged

Conversation

rivalarya
Copy link

@rivalarya rivalarya commented Jul 27, 2022

Description
I make this method because method promptByKey() just can read one value.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Sorry, something went wrong.

- make a test for the method
- write the method in user guide
@kenjis kenjis added 4.3 enhancement PRs that improve existing functionalities labels Jul 27, 2022
@kenjis kenjis changed the title Add new method in CLI class Add new method promptByKey() in CLI class Jul 28, 2022
@kenjis kenjis changed the title Add new method promptByKey() in CLI class Add new method promptByMultipleKey() in CLI class Jul 28, 2022
@rivalarya rivalarya changed the title Add new method promptByMultipleKey() in CLI class Add new method promptByMultipleKeys() in CLI class Jul 29, 2022
Copy link
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

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

If this is the same with promptByKey() except for support for multiple keys. Please extract the common code in them instead of duplicating. This will make maintenance easier.

My initial comments below:

Comment on lines 315 to 319
if (is_string($text)) {
$text = [$text];
} elseif (! is_array($text)) {
throw new InvalidArgumentException('$text can only be string');
}
Copy link
Member

Choose a reason for hiding this comment

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

This part is confusing.

  • The PHPDoc says $text is a string.
  • The method parameter says it is untyped.
  • The conditional says $text must be a string|array
  • The throwable says $text can only be a string

throw new InvalidArgumentException('$text can only be string');
}

if (is_array($options) && $options) {
Copy link
Member

Choose a reason for hiding this comment

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

is_array check is always true because of the type declaration above

Comment on lines 339 to 341
} else {
throw new InvalidArgumentException('$options can only be array');
}
Copy link
Member

Choose a reason for hiding this comment

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

Based on the previous if, if $options is an array but empty, the throwable will say $options can only be an array which is misleading.

CLI::write(CLI::color($name, 'green') . CLI::wrap($description, 125, $keyMaxLength + 4));
}
static::fwrite(STDOUT, (trim((string) ((int) $text)) ? ' ' : '') . $extraOutput . ': ');
$input = trim(static::input()) ?: $default;
Copy link
Member

Choose a reason for hiding this comment

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

This will hang for 6 hours in the GH actions because the unit test is waiting for input. If you can in a way extract the non-testable part(s) in another method so that it can be mocked in tests.

Copy link
Member

Choose a reason for hiding this comment

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

Now we cannot write test for it. See #6076
It is a problem.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm. This is the problem with using static methods. It is not mockable by PHPUnit.

Copy link
Member

Choose a reason for hiding this comment

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

@paulbalandan It seems difficult to ask the contributor to add test code.
Do we accept this PR without test code?

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes. What I did before in promptByKey PR is to check locally the changes then suggest changes, if any, since prompt is not testable.

- delete unit test for promptByMultipleKeys because codeigniter4#6076
- change method name in docs and changelog
@kenjis kenjis added the tests needed Pull requests that need tests label Aug 3, 2022
@kenjis
Copy link
Member

kenjis commented Aug 5, 2022

Sorry, this branch has conflicts. Can you rebase and solve the conflicts?

And now we can write test for CLI::input(). See #6335
Could you try to add a test if possible?

@kenjis kenjis added the stale Pull requests with conflicts label Aug 5, 2022
@rivalarya
Copy link
Author

Sorry, this branch has conflicts. Can you rebase and solve the conflicts?

And now we can write test for CLI::input(). See #6335 Could you try to add a test if possible?

okay. i will write the test

@rivalarya rivalarya force-pushed the add-method-promptByMultipleKey-in-CLI branch from 687e80f to d886c9d Compare August 5, 2022 09:46
@kenjis kenjis removed stale Pull requests with conflicts tests needed Pull requests that need tests labels Aug 5, 2022
@kenjis
Copy link
Member

kenjis commented Aug 10, 2022

promptByKey():

$ php public/index.php 

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

[0, 1, 2]: x
The temp field must be one of: 0, 1, 2.

[0, 1, 2]: 

promptByMultipleKeys():

$ php public/index.php 


Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton
 [0, 1, 2]
x
Please select correctly

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton
 [0, 1, 2]

@kenjis
Copy link
Member

kenjis commented Aug 10, 2022

The output format is a bit different from promptByKey().

  1. Can you make the same?
  2. I did not know how to input multiple values.

For example:

$ php public/index.php 

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2]: x
Please select correctly.

You can specify multiple values separated by commas.
[0, 1, 2]:

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

See my comments.

@rivalarya
Copy link
Author

The output format is a bit different from promptByKey().

  1. Can you make the same?
  2. I did not know how to input multiple values.

For example:

$ php public/index.php 

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2]: x
Please select correctly.

You can specify multiple values separated by commas.
[0, 1, 2]:
  1. So, the output should only return the value of the selected key?
  2. There is an example in the documentation. see here

@kenjis
Copy link
Member

kenjis commented Aug 10, 2022

So, the output should only return the value of the selected key?

I think key value pair is okay.

There is an example in the documentation.

End users don't know whether promptByKey() or promptByMultipleKeys() is used.

@rivalarya
Copy link
Author

Please select correctly.

You can specify multiple values separated by commas.
[0, 1, 2]:

In promptByKey, when a user fails in validation input, the return is:

The temp field must be one of: 0, 1, 2, 3.

[0, 1, 2, 3]:

But in promptByMultipleKeys, when a user fails in validation input, the return is:

Please select correctly.

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

[0, 1, 2]:

Is it fine? or should I replace the return in promptByMultipleKeys?

@kenjis
Copy link
Member

kenjis commented Aug 10, 2022

My opinion is the following:

$ php public/index.php 

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2]: x
Please select correctly.

You can specify multiple values separated by commas.
[0, 1, 2]:

@kenjis
Copy link
Member

kenjis commented Aug 14, 2022

It seems the input validation is not correct.

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2] : ^,2

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies                                                                                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (2) [
    0 => string (12) "Playing game"
    2 => string (9) "Badminton"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]
Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2] : ,

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies                                                                                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
    0 => string (12) "Playing game"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]
Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2] : |^

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies                                                                                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
    0 => string (12) "Playing game"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

Please fix the input validation.

@kenjis
Copy link
Member

kenjis commented Aug 18, 2022

You have two while (true). It seems they have the different validation rules.
They should be combined into one.

Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2] : 1,2,5
Please select correctly.

You can specify multiple values separated by commas.
[0, 1, 2] : 2.5

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies                                                                                                                         │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
    2 => string (9) "Badminton"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]
Select your hobbies:
  [0]  Playing game
  [1]  Sleep
  [2]  Badminton

You can specify multiple values separated by commas.
[0, 1, 2] : 1,2,5
Please select correctly.

You can specify multiple values separated by commas.
[0, 1, 2] : ^

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ $hobbies                                                                                                                         │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
array (1) [
    0 => string (12) "Playing game"
]
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Called from .../app/Controllers/Home.php:13 [dd()]

-  combine two validation to one while loop
@kenjis
Copy link
Member

kenjis commented Aug 18, 2022

@rifalarya-2 I put two inline comments. That's all.

@rivalarya
Copy link
Author

@rifalarya-2 I put two inline comments. That's all.

Thank you

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you.

@kenjis kenjis merged commit 116f463 into codeigniter4:4.3 Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants