Version 1
[yaffs-website] / vendor / symfony / console / Tester / ApplicationTester.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\Tester;
13
14 use Symfony\Component\Console\Application;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Console\Output\StreamOutput;
19
20 /**
21  * Eases the testing of console applications.
22  *
23  * When testing an application, don't forget to disable the auto exit flag:
24  *
25  *     $application = new Application();
26  *     $application->setAutoExit(false);
27  *
28  * @author Fabien Potencier <fabien@symfony.com>
29  */
30 class ApplicationTester
31 {
32     private $application;
33     private $input;
34     private $output;
35     private $statusCode;
36
37     /**
38      * Constructor.
39      *
40      * @param Application $application An Application instance to test
41      */
42     public function __construct(Application $application)
43     {
44         $this->application = $application;
45     }
46
47     /**
48      * Executes the application.
49      *
50      * Available options:
51      *
52      *  * interactive: Sets the input interactive flag
53      *  * decorated:   Sets the output decorated flag
54      *  * verbosity:   Sets the output verbosity flag
55      *
56      * @param array $input   An array of arguments and options
57      * @param array $options An array of options
58      *
59      * @return int The command exit code
60      */
61     public function run(array $input, $options = array())
62     {
63         $this->input = new ArrayInput($input);
64         if (isset($options['interactive'])) {
65             $this->input->setInteractive($options['interactive']);
66         }
67
68         $this->output = new StreamOutput(fopen('php://memory', 'w', false));
69         if (isset($options['decorated'])) {
70             $this->output->setDecorated($options['decorated']);
71         }
72         if (isset($options['verbosity'])) {
73             $this->output->setVerbosity($options['verbosity']);
74         }
75
76         return $this->statusCode = $this->application->run($this->input, $this->output);
77     }
78
79     /**
80      * Gets the display returned by the last execution of the application.
81      *
82      * @param bool $normalize Whether to normalize end of lines to \n or not
83      *
84      * @return string The display
85      */
86     public function getDisplay($normalize = false)
87     {
88         rewind($this->output->getStream());
89
90         $display = stream_get_contents($this->output->getStream());
91
92         if ($normalize) {
93             $display = str_replace(PHP_EOL, "\n", $display);
94         }
95
96         return $display;
97     }
98
99     /**
100      * Gets the input instance used by the last execution of the application.
101      *
102      * @return InputInterface The current input instance
103      */
104     public function getInput()
105     {
106         return $this->input;
107     }
108
109     /**
110      * Gets the output instance used by the last execution of the application.
111      *
112      * @return OutputInterface The current output instance
113      */
114     public function getOutput()
115     {
116         return $this->output;
117     }
118
119     /**
120      * Gets the status code returned by the last execution of the application.
121      *
122      * @return int The status code
123      */
124     public function getStatusCode()
125     {
126         return $this->statusCode;
127     }
128 }