ChoiceQuestionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Question;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Question\ChoiceQuestion;
  13. class ChoiceQuestionTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider selectUseCases
  17. */
  18. public function testSelectUseCases($multiSelect, $answers, $expected, $message, $default = null)
  19. {
  20. $question = new ChoiceQuestion('A question', [
  21. 'First response',
  22. 'Second response',
  23. 'Third response',
  24. 'Fourth response',
  25. null,
  26. ], $default);
  27. $question->setMultiselect($multiSelect);
  28. foreach ($answers as $answer) {
  29. $validator = $question->getValidator();
  30. $actual = $validator($answer);
  31. $this->assertEquals($actual, $expected, $message);
  32. }
  33. }
  34. public static function selectUseCases()
  35. {
  36. return [
  37. [
  38. false,
  39. ['First response', 'First response ', ' First response', ' First response '],
  40. 'First response',
  41. 'When passed single answer on singleSelect, the defaultValidator must return this answer as a string',
  42. ],
  43. [
  44. true,
  45. ['First response', 'First response ', ' First response', ' First response '],
  46. ['First response'],
  47. 'When passed single answer on MultiSelect, the defaultValidator must return this answer as an array',
  48. ],
  49. [
  50. true,
  51. ['First response,Second response', ' First response , Second response '],
  52. ['First response', 'Second response'],
  53. 'When passed multiple answers on MultiSelect, the defaultValidator must return these answers as an array',
  54. ],
  55. [
  56. false,
  57. [null],
  58. null,
  59. 'When used null as default single answer on singleSelect, the defaultValidator must return this answer as null',
  60. ],
  61. [
  62. false,
  63. ['First response'],
  64. 'First response',
  65. 'When used a string as default single answer on singleSelect, the defaultValidator must return this answer as a string',
  66. 'First response',
  67. ],
  68. [
  69. false,
  70. [0],
  71. 'First response',
  72. 'When passed single answer using choice\'s key, the defaultValidator must return the choice value',
  73. ],
  74. [
  75. true,
  76. ['0, 2'],
  77. ['First response', 'Third response'],
  78. 'When passed multiple answers using choices\' key, the defaultValidator must return the choice values in an array',
  79. ],
  80. ];
  81. }
  82. public function testNonTrimmable()
  83. {
  84. $question = new ChoiceQuestion('A question', [
  85. 'First response ',
  86. ' Second response',
  87. ' Third response ',
  88. ]);
  89. $question->setTrimmable(false);
  90. $this->assertSame(' Third response ', $question->getValidator()(' Third response '));
  91. $question->setMultiselect(true);
  92. $this->assertSame(['First response ', ' Second response'], $question->getValidator()('First response , Second response'));
  93. }
  94. /**
  95. * @dataProvider selectAssociativeChoicesProvider
  96. */
  97. public function testSelectAssociativeChoices($providedAnswer, $expectedValue)
  98. {
  99. $question = new ChoiceQuestion('A question', [
  100. '0' => 'First choice',
  101. 'foo' => 'Foo',
  102. '99' => 'N°99',
  103. 'string object' => new StringChoice('String Object'),
  104. ]);
  105. $this->assertSame($expectedValue, $question->getValidator()($providedAnswer));
  106. }
  107. public static function selectAssociativeChoicesProvider()
  108. {
  109. return [
  110. 'select "0" choice by key' => ['0', '0'],
  111. 'select "0" choice by value' => ['First choice', '0'],
  112. 'select by key' => ['foo', 'foo'],
  113. 'select by value' => ['Foo', 'foo'],
  114. 'select by key, with numeric key' => ['99', '99'],
  115. 'select by value, with numeric key' => ['N°99', '99'],
  116. 'select by key, with string object value' => ['string object', 'string object'],
  117. 'select by value, with string object value' => ['String Object', 'string object'],
  118. ];
  119. }
  120. public function testSelectWithNonStringChoices()
  121. {
  122. $question = new ChoiceQuestion('A question', [
  123. $result1 = new StringChoice('foo'),
  124. $result2 = new StringChoice('bar'),
  125. $result3 = new StringChoice('baz'),
  126. ]);
  127. $this->assertSame($result1, $question->getValidator()('foo'), 'answer can be selected by its string value');
  128. $this->assertSame($result1, $question->getValidator()(0), 'answer can be selected by index');
  129. $question->setMultiselect(true);
  130. $this->assertSame([$result3, $result2], $question->getValidator()('baz, bar'));
  131. }
  132. }
  133. class StringChoice
  134. {
  135. private $string;
  136. public function __construct(string $string)
  137. {
  138. $this->string = $string;
  139. }
  140. public function __toString()
  141. {
  142. return $this->string;
  143. }
  144. }