Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / symfony / console / Symfony / Component / Console / Tests / Helper / DialogHelperTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Tests\Helper;
13
14 use Symfony\Component\Console\Helper\DialogHelper;
15 use Symfony\Component\Console\Helper\HelperSet;
16 use Symfony\Component\Console\Helper\FormatterHelper;
17 use Symfony\Component\Console\Output\StreamOutput;
18
19 class DialogHelperTest extends \PHPUnit_Framework_TestCase
20 {
21     public function testAsk()
22     {
23         $dialog = new DialogHelper();
24
25         $dialog->setInputStream($this->getInputStream("\n8AM\n"));
26
27         $this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
28         $this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
29
30         rewind($output->getStream());
31         $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
32     }
33
34     public function testAskConfirmation()
35     {
36         $dialog = new DialogHelper();
37
38         $dialog->setInputStream($this->getInputStream("\n\n"));
39         $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
40         $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
41
42         $dialog->setInputStream($this->getInputStream("y\nyes\n"));
43         $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
44         $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
45
46         $dialog->setInputStream($this->getInputStream("n\nno\n"));
47         $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
48         $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
49     }
50
51     public function testAskAndValidate()
52     {
53         $dialog = new DialogHelper();
54         $helperSet = new HelperSet(array(new FormatterHelper()));
55         $dialog->setHelperSet($helperSet);
56
57         $question ='What color was the white horse of Henry IV?';
58         $error = 'This is not a color!';
59         $validator = function ($color) use ($error) {
60             if (!in_array($color, array('white', 'black'))) {
61                 throw new \InvalidArgumentException($error);
62             }
63
64             return $color;
65         };
66
67         $dialog->setInputStream($this->getInputStream("\nblack\n"));
68         $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
69         $this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
70
71         $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
72         try {
73             $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
74             $this->fail();
75         } catch (\InvalidArgumentException $e) {
76             $this->assertEquals($error, $e->getMessage());
77         }
78     }
79
80     protected function getInputStream($input)
81     {
82         $stream = fopen('php://memory', 'r+', false);
83         fputs($stream, $input);
84         rewind($stream);
85
86         return $stream;
87     }
88
89     protected function getOutputStream()
90     {
91         return new StreamOutput(fopen('php://memory', 'r+', false));
92     }
93 }