|
17 | 17 | use CodeIgniter\Exceptions\RuntimeException; |
18 | 18 | use CodeIgniter\Superglobals; |
19 | 19 | use CodeIgniter\Test\CIUnitTestCase; |
| 20 | +use CodeIgniter\Test\Mock\MockInputOutput; |
20 | 21 | use CodeIgniter\Test\PhpStreamWrapper; |
21 | 22 | use CodeIgniter\Test\StreamFilterTrait; |
22 | 23 | use PHPUnit\Framework\Attributes\DataProvider; |
@@ -143,6 +144,129 @@ public function testPromptInputZero(): void |
143 | 144 | $this->assertSame('0', $output); |
144 | 145 | } |
145 | 146 |
|
| 147 | + public function testPromptPassesPromptTextToInputReader(): void |
| 148 | + { |
| 149 | + $io = new class () extends InputOutput { |
| 150 | + public ?string $receivedPrefix = null; |
| 151 | + |
| 152 | + public function input(?string $prefix = null): string |
| 153 | + { |
| 154 | + $this->receivedPrefix = $prefix; |
| 155 | + |
| 156 | + return 'red'; |
| 157 | + } |
| 158 | + }; |
| 159 | + CLI::setInputOutput($io); |
| 160 | + |
| 161 | + $output = CLI::prompt('What is your favorite color?'); |
| 162 | + |
| 163 | + CLI::resetInputOutput(); |
| 164 | + |
| 165 | + $this->assertSame('red', $output); |
| 166 | + $this->assertSame('What is your favorite color? : ', $io->receivedPrefix); |
| 167 | + } |
| 168 | + |
| 169 | + public function testPromptPassesDefaultOptionInPromptText(): void |
| 170 | + { |
| 171 | + $io = new class () extends InputOutput { |
| 172 | + public ?string $receivedPrefix = null; |
| 173 | + |
| 174 | + public function input(?string $prefix = null): string |
| 175 | + { |
| 176 | + $this->receivedPrefix = $prefix; |
| 177 | + |
| 178 | + return ''; |
| 179 | + } |
| 180 | + }; |
| 181 | + CLI::setInputOutput($io); |
| 182 | + |
| 183 | + $output = CLI::prompt('What is your favorite color?', 'red'); |
| 184 | + |
| 185 | + CLI::resetInputOutput(); |
| 186 | + |
| 187 | + $this->assertSame('red', $output); |
| 188 | + $this->assertSame( |
| 189 | + sprintf('What is your favorite color? [%s]: ', CLI::color('red', 'green')), |
| 190 | + $io->receivedPrefix, |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + public function testPromptByKeyPassesPromptTextToInputReader(): void |
| 195 | + { |
| 196 | + $io = new class () extends InputOutput { |
| 197 | + public ?string $receivedPrefix = null; |
| 198 | + |
| 199 | + public function input(?string $prefix = null): string |
| 200 | + { |
| 201 | + $this->receivedPrefix = $prefix; |
| 202 | + |
| 203 | + return '1'; |
| 204 | + } |
| 205 | + }; |
| 206 | + CLI::setInputOutput($io); |
| 207 | + |
| 208 | + $output = CLI::promptByKey('Select your hobbies:', ['Playing game', 'Sleep', 'Badminton']); |
| 209 | + |
| 210 | + CLI::resetInputOutput(); |
| 211 | + |
| 212 | + $this->assertSame('1', $output); |
| 213 | + $this->assertSame( |
| 214 | + PHP_EOL . sprintf('[%s, 1, 2]: ', CLI::color('0', 'green')), |
| 215 | + $io->receivedPrefix, |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + public function testPromptByMultipleKeysPassesPromptTextToInputReader(): void |
| 220 | + { |
| 221 | + $io = new class () extends InputOutput { |
| 222 | + public ?string $receivedPrefix = null; |
| 223 | + |
| 224 | + public function input(?string $prefix = null): string |
| 225 | + { |
| 226 | + $this->receivedPrefix = $prefix; |
| 227 | + |
| 228 | + return '0,1'; |
| 229 | + } |
| 230 | + }; |
| 231 | + CLI::setInputOutput($io); |
| 232 | + |
| 233 | + $output = CLI::promptByMultipleKeys('Select your hobbies:', ['Playing game', 'Sleep', 'Badminton']); |
| 234 | + |
| 235 | + CLI::resetInputOutput(); |
| 236 | + |
| 237 | + $this->assertSame([0 => 'Playing game', 1 => 'Sleep'], $output); |
| 238 | + $this->assertSame( |
| 239 | + 'You can specify multiple values separated by commas.' . PHP_EOL |
| 240 | + . sprintf('[%s, 1, 2] : ', CLI::color('0', 'green')), |
| 241 | + $io->receivedPrefix, |
| 242 | + ); |
| 243 | + } |
| 244 | + |
| 245 | + public function testInputWritesPrefixToStdout(): void |
| 246 | + { |
| 247 | + $io = new MockInputOutput(); |
| 248 | + $io->setInputs(['blue']); |
| 249 | + CLI::setInputOutput($io); |
| 250 | + |
| 251 | + $output = CLI::input('Name: '); |
| 252 | + |
| 253 | + CLI::resetInputOutput(); |
| 254 | + |
| 255 | + $this->assertSame('blue', $output); |
| 256 | + $this->assertSame('Name: blue' . PHP_EOL, $io->getOutput()); |
| 257 | + } |
| 258 | + |
| 259 | + public function testMarkAnsiNonPrintingWrapsEscapeSequences(): void |
| 260 | + { |
| 261 | + $wrap = $this->getPrivateMethodInvoker(new InputOutput(), 'markAnsiNonPrinting'); |
| 262 | + |
| 263 | + $this->assertSame( |
| 264 | + "What is your favorite color? [\x01\e[0;32m\x02red\x01\e[0m\x02]: ", |
| 265 | + $wrap(sprintf('What is your favorite color? [%s]: ', CLI::color('red', 'green'))), |
| 266 | + ); |
| 267 | + $this->assertSame('Name: ', $wrap('Name: ')); |
| 268 | + } |
| 269 | + |
146 | 270 | public function testPromptByKey(): void |
147 | 271 | { |
148 | 272 | PhpStreamWrapper::register(); |
|
0 commit comments