@@ -394,6 +394,357 @@ public function testChoiceOutputFormattingQuestionForUtf8Keys()
394
394
$ dialog ->ask ($ this ->createStreamableInputInterfaceMock ($ this ->getInputStream ("\n" )), $ output , $ question );
395
395
}
396
396
397
+ /**
398
+ * @group legacy
399
+ */
400
+ public function testLegacyAskChoice ()
401
+ {
402
+ $ questionHelper = new QuestionHelper ();
403
+
404
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
405
+ $ questionHelper ->setHelperSet ($ helperSet );
406
+
407
+ $ heroes = array ('Superman ' , 'Batman ' , 'Spiderman ' );
408
+
409
+ $ questionHelper ->setInputStream ($ this ->getInputStream ("\n1 \n 1 \nFabien \n1 \nFabien \n1 \n0,2 \n 0 , 2 \n\n\n" ));
410
+
411
+ $ question = new ChoiceQuestion ('What is your favorite superhero? ' , $ heroes , '2 ' );
412
+ $ question ->setMaxAttempts (1 );
413
+ // first answer is an empty answer, we're supposed to receive the default value
414
+ $ this ->assertEquals ('Spiderman ' , $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
415
+
416
+ $ question = new ChoiceQuestion ('What is your favorite superhero? ' , $ heroes );
417
+ $ question ->setMaxAttempts (1 );
418
+ $ this ->assertEquals ('Batman ' , $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
419
+ $ this ->assertEquals ('Batman ' , $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
420
+
421
+ $ question = new ChoiceQuestion ('What is your favorite superhero? ' , $ heroes );
422
+ $ question ->setErrorMessage ('Input "%s" is not a superhero! ' );
423
+ $ question ->setMaxAttempts (2 );
424
+ $ this ->assertEquals ('Batman ' , $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ output = $ this ->createOutputInterface (), $ question ));
425
+
426
+ rewind ($ output ->getStream ());
427
+ $ stream = stream_get_contents ($ output ->getStream ());
428
+ $ this ->assertContains ('Input "Fabien" is not a superhero! ' , $ stream );
429
+
430
+ try {
431
+ $ question = new ChoiceQuestion ('What is your favorite superhero? ' , $ heroes , '1 ' );
432
+ $ question ->setMaxAttempts (1 );
433
+ $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ output = $ this ->createOutputInterface (), $ question );
434
+ $ this ->fail ();
435
+ } catch (\InvalidArgumentException $ e ) {
436
+ $ this ->assertEquals ('Value "Fabien" is invalid ' , $ e ->getMessage ());
437
+ }
438
+
439
+ $ question = new ChoiceQuestion ('What is your favorite superhero? ' , $ heroes , null );
440
+ $ question ->setMaxAttempts (1 );
441
+ $ question ->setMultiselect (true );
442
+
443
+ $ this ->assertEquals (array ('Batman ' ), $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
444
+ $ this ->assertEquals (array ('Superman ' , 'Spiderman ' ), $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
445
+ $ this ->assertEquals (array ('Superman ' , 'Spiderman ' ), $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
446
+
447
+ $ question = new ChoiceQuestion ('What is your favorite superhero? ' , $ heroes , '0,1 ' );
448
+ $ question ->setMaxAttempts (1 );
449
+ $ question ->setMultiselect (true );
450
+
451
+ $ this ->assertEquals (array ('Superman ' , 'Batman ' ), $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
452
+
453
+ $ question = new ChoiceQuestion ('What is your favorite superhero? ' , $ heroes , ' 0 , 1 ' );
454
+ $ question ->setMaxAttempts (1 );
455
+ $ question ->setMultiselect (true );
456
+
457
+ $ this ->assertEquals (array ('Superman ' , 'Batman ' ), $ questionHelper ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
458
+ }
459
+
460
+ /**
461
+ * @group legacy
462
+ */
463
+ public function testLegacyAsk ()
464
+ {
465
+ $ dialog = new QuestionHelper ();
466
+
467
+ $ dialog ->setInputStream ($ this ->getInputStream ("\n8AM \n" ));
468
+
469
+ $ question = new Question ('What time is it? ' , '2PM ' );
470
+ $ this ->assertEquals ('2PM ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
471
+
472
+ $ question = new Question ('What time is it? ' , '2PM ' );
473
+ $ this ->assertEquals ('8AM ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ output = $ this ->createOutputInterface (), $ question ));
474
+
475
+ rewind ($ output ->getStream ());
476
+ $ this ->assertEquals ('What time is it? ' , stream_get_contents ($ output ->getStream ()));
477
+ }
478
+
479
+ /**
480
+ * @group legacy
481
+ */
482
+ public function testLegacyAskWithAutocomplete ()
483
+ {
484
+ if (!$ this ->hasSttyAvailable ()) {
485
+ $ this ->markTestSkipped ('`stty` is required to test autocomplete functionality ' );
486
+ }
487
+
488
+ // Acm<NEWLINE>
489
+ // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
490
+ // <NEWLINE>
491
+ // <UP ARROW><UP ARROW><NEWLINE>
492
+ // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
493
+ // <DOWN ARROW><NEWLINE>
494
+ // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
495
+ // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
496
+ $ inputStream = $ this ->getInputStream ("Acm \nAc \177\177s \tTest \n\n\033[A \033[A \n\033[A \033[A \033[A \033[A \033[A \tTest \n\033[B \nS \177\177\033[B \033[B \nF00 \177\177oo \t\n" );
497
+
498
+ $ dialog = new QuestionHelper ();
499
+ $ dialog ->setInputStream($ inputStream );
500
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
501
+ $ dialog ->setHelperSet ($ helperSet );
502
+
503
+ $ question = new Question ('Please select a bundle ' , 'FrameworkBundle ' );
504
+ $ question ->setAutocompleterValues (array ('AcmeDemoBundle ' , 'AsseticBundle ' , 'SecurityBundle ' , 'FooBundle ' ));
505
+
506
+ $ this ->assertEquals ('AcmeDemoBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
507
+ $ this ->assertEquals ('AsseticBundleTest ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
508
+ $ this ->assertEquals ('FrameworkBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
509
+ $ this ->assertEquals ('SecurityBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
510
+ $ this ->assertEquals ('FooBundleTest ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
511
+ $ this ->assertEquals ('AcmeDemoBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
512
+ $ this ->assertEquals ('AsseticBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
513
+ $ this ->assertEquals ('FooBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
514
+ }
515
+
516
+ /**
517
+ * @group legacy
518
+ */
519
+ public function testLegacyAskWithAutocompleteWithNonSequentialKeys ()
520
+ {
521
+ if (!$ this ->hasSttyAvailable ()) {
522
+ $ this ->markTestSkipped ('`stty` is required to test autocomplete functionality ' );
523
+ }
524
+
525
+ // <UP ARROW><UP ARROW><NEWLINE><DOWN ARROW><DOWN ARROW><NEWLINE>
526
+ $ inputStream = $ this ->getInputStream ("\033[A \033[A \n\033[B \033[B \n" );
527
+
528
+ $ dialog = new QuestionHelper ();
529
+ $ dialog ->setInputStream ($ inputStream );
530
+ $ dialog ->setHelperSet (new HelperSet (array (new FormatterHelper ())));
531
+
532
+ $ question = new ChoiceQuestion ('Please select a bundle ' , array (1 => 'AcmeDemoBundle ' , 4 => 'AsseticBundle ' ));
533
+ $ question ->setMaxAttempts (1 );
534
+
535
+ $ this ->assertEquals ('AcmeDemoBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
536
+ $ this ->assertEquals ('AsseticBundle ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
537
+ }
538
+
539
+ /**
540
+ * @group legacy
541
+ */
542
+ public function testLegacyAskHiddenResponse ()
543
+ {
544
+ if ('\\' === DIRECTORY_SEPARATOR ) {
545
+ $ this ->markTestSkipped ('This test is not supported on Windows ' );
546
+ }
547
+
548
+ $ dialog = new QuestionHelper ();
549
+ $ dialog ->setInputStream ($ this ->getInputStream ("8AM \n" ));
550
+
551
+ $ question = new Question ('What time is it? ' );
552
+ $ question ->setHidden (true );
553
+
554
+ $ this ->assertEquals ('8AM ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
555
+ }
556
+
557
+ /**
558
+ * @group legacy
559
+ * @dataProvider getAskConfirmationData
560
+ */
561
+ public function testLegacyAskConfirmation ($ question , $ expected , $ default = true )
562
+ {
563
+ $ dialog = new QuestionHelper ();
564
+
565
+ $ dialog ->setInputStream ($ this ->getInputStream ($ question ."\n" ));
566
+ $ question = new ConfirmationQuestion ('Do you like French fries? ' , $ default );
567
+ $ this ->assertEquals ($ expected , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ), 'confirmation question should ' .($ expected ? 'pass ' : 'cancel ' ));
568
+ }
569
+
570
+ /**
571
+ * @group legacy
572
+ */
573
+ public function testLegacyAskConfirmationWithCustomTrueAnswer ()
574
+ {
575
+ $ dialog = new QuestionHelper ();
576
+
577
+ $ dialog ->setInputStream ($ this ->getInputStream ("j \ny \n" ));
578
+ $ question = new ConfirmationQuestion ('Do you like French fries? ' , false , '/^(j|y)/i ' );
579
+ $ this ->assertTrue ($ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
580
+ $ question = new ConfirmationQuestion ('Do you like French fries? ' , false , '/^(j|y)/i ' );
581
+ $ this ->assertTrue ($ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
582
+ }
583
+
584
+ /**
585
+ * @group legacy
586
+ */
587
+ public function testLegacyAskAndValidate ()
588
+ {
589
+ $ dialog = new QuestionHelper ();
590
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
591
+ $ dialog ->setHelperSet ($ helperSet );
592
+
593
+ $ error = 'This is not a color! ' ;
594
+ $ validator = function ($ color ) use ($ error ) {
595
+ if (!in_array ($ color , array ('white ' , 'black ' ))) {
596
+ throw new \InvalidArgumentException ($ error );
597
+ }
598
+
599
+ return $ color ;
600
+ };
601
+
602
+ $ question = new Question ('What color was the white horse of Henry IV? ' , 'white ' );
603
+ $ question ->setValidator ($ validator );
604
+ $ question ->setMaxAttempts (2 );
605
+
606
+ $ dialog ->setInputStream ($ this ->getInputStream ("\nblack \n" ));
607
+ $ this ->assertEquals ('white ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
608
+ $ this ->assertEquals ('black ' , $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question ));
609
+
610
+ $ dialog ->setInputStream ($ this ->getInputStream ("green \nyellow \norange \n" ));
611
+ try {
612
+ $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question );
613
+ $ this ->fail ();
614
+ } catch (\InvalidArgumentException $ e ) {
615
+ $ this ->assertEquals ($ error , $ e ->getMessage ());
616
+ }
617
+ }
618
+
619
+ /**
620
+ * @group legacy
621
+ * @dataProvider simpleAnswerProvider
622
+ */
623
+ public function testLegacySelectChoiceFromSimpleChoices ($ providedAnswer , $ expectedValue )
624
+ {
625
+ $ possibleChoices = array (
626
+ 'My environment 1 ' ,
627
+ 'My environment 2 ' ,
628
+ 'My environment 3 ' ,
629
+ );
630
+
631
+ $ dialog = new QuestionHelper ();
632
+ $ dialog ->setInputStream ($ this ->getInputStream ($ providedAnswer ."\n" ));
633
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
634
+ $ dialog ->setHelperSet ($ helperSet );
635
+
636
+ $ question = new ChoiceQuestion ('Please select the environment to load ' , $ possibleChoices );
637
+ $ question ->setMaxAttempts (1 );
638
+ $ answer = $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question );
639
+
640
+ $ this ->assertSame ($ expectedValue , $ answer );
641
+ }
642
+
643
+ /**
644
+ * @group legacy
645
+ * @dataProvider mixedKeysChoiceListAnswerProvider
646
+ */
647
+ public function testLegacyChoiceFromChoicelistWithMixedKeys ($ providedAnswer , $ expectedValue )
648
+ {
649
+ $ possibleChoices = array (
650
+ '0 ' => 'No environment ' ,
651
+ '1 ' => 'My environment 1 ' ,
652
+ 'env_2 ' => 'My environment 2 ' ,
653
+ 3 => 'My environment 3 ' ,
654
+ );
655
+
656
+ $ dialog = new QuestionHelper ();
657
+ $ dialog ->setInputStream ($ this ->getInputStream ($ providedAnswer ."\n" ));
658
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
659
+ $ dialog ->setHelperSet ($ helperSet );
660
+
661
+ $ question = new ChoiceQuestion ('Please select the environment to load ' , $ possibleChoices );
662
+ $ question ->setMaxAttempts (1 );
663
+ $ answer = $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question );
664
+
665
+ $ this ->assertSame ($ expectedValue , $ answer );
666
+ }
667
+
668
+ /**
669
+ * @group legacy
670
+ * @dataProvider answerProvider
671
+ */
672
+ public function testLegacySelectChoiceFromChoiceList ($ providedAnswer , $ expectedValue )
673
+ {
674
+ $ possibleChoices = array (
675
+ 'env_1 ' => 'My environment 1 ' ,
676
+ 'env_2 ' => 'My environment ' ,
677
+ 'env_3 ' => 'My environment ' ,
678
+ );
679
+
680
+ $ dialog = new QuestionHelper ();
681
+ $ dialog ->setInputStream ($ this ->getInputStream ($ providedAnswer ."\n" ));
682
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
683
+ $ dialog ->setHelperSet ($ helperSet );
684
+
685
+ $ question = new ChoiceQuestion ('Please select the environment to load ' , $ possibleChoices );
686
+ $ question ->setMaxAttempts (1 );
687
+ $ answer = $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question );
688
+
689
+ $ this ->assertSame ($ expectedValue , $ answer );
690
+ }
691
+
692
+ /**
693
+ * @group legacy
694
+ * @expectedException \InvalidArgumentException
695
+ * @expectedExceptionMessage The provided answer is ambiguous. Value should be one of env_2 or env_3.
696
+ */
697
+ public function testLegacyAmbiguousChoiceFromChoicelist ()
698
+ {
699
+ $ possibleChoices = array (
700
+ 'env_1 ' => 'My first environment ' ,
701
+ 'env_2 ' => 'My environment ' ,
702
+ 'env_3 ' => 'My environment ' ,
703
+ );
704
+
705
+ $ dialog = new QuestionHelper ();
706
+ $ dialog ->setInputStream ($ this ->getInputStream ("My environment \n" ));
707
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
708
+ $ dialog ->setHelperSet ($ helperSet );
709
+
710
+ $ question = new ChoiceQuestion ('Please select the environment to load ' , $ possibleChoices );
711
+ $ question ->setMaxAttempts (1 );
712
+
713
+ $ dialog ->ask ($ this ->createInputInterfaceMock (), $ this ->createOutputInterface (), $ question );
714
+ }
715
+
716
+ /**
717
+ * @requires function mb_strwidth
718
+ * @legacy
719
+ */
720
+ public function testLegacyChoiceOutputFormattingQuestionForUtf8Keys ()
721
+ {
722
+ $ question = 'Lorem ipsum? ' ;
723
+ $ possibleChoices = array (
724
+ 'foo ' => 'foo ' ,
725
+ 'żółw ' => 'bar ' ,
726
+ 'łabądź ' => 'baz ' ,
727
+ );
728
+ $ outputShown = array (
729
+ $ question ,
730
+ ' [<info>foo </info>] foo ' ,
731
+ ' [<info>żółw </info>] bar ' ,
732
+ ' [<info>łabądź</info>] baz ' ,
733
+ );
734
+ $ output = $ this ->getMock ('\Symfony\Component\Console\Output\OutputInterface ' );
735
+ $ output ->method ('getFormatter ' )->willReturn (new OutputFormatter ());
736
+
737
+ $ dialog = new QuestionHelper ();
738
+ $ dialog ->setInputStream ($ this ->getInputStream ("\n" ));
739
+ $ helperSet = new HelperSet (array (new FormatterHelper ()));
740
+ $ dialog ->setHelperSet ($ helperSet );
741
+
742
+ $ output ->expects ($ this ->once ())->method ('writeln ' )->with ($ this ->equalTo ($ outputShown ));
743
+
744
+ $ question = new ChoiceQuestion ($ question , $ possibleChoices , 'foo ' );
745
+ $ dialog ->ask ($ this ->createInputInterfaceMock (), $ output , $ question );
746
+ }
747
+
397
748
protected function getInputStream ($ input )
398
749
{
399
750
$ stream = fopen ('php://memory ' , 'r+ ' , false );
@@ -424,6 +775,11 @@ protected function createStreamableInputInterfaceMock($stream = null, $interacti
424
775
return $ mock ;
425
776
}
426
777
778
+ protected function createInputInterfaceMock ($ interactive = true )
779
+ {
780
+ return $ this ->createStreamableInputInterfaceMock (null , $ interactive );
781
+ }
782
+
427
783
private function hasSttyAvailable ()
428
784
{
429
785
exec ('stty 2>&1 ' , $ output , $ exitcode );
0 commit comments