summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Tester
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
committerpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
commitbf6655a534a6775d30cafa67bd801276bda1d98d (patch)
treec6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/console/Tester
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/console/Tester')
-rw-r--r--vendor/symfony/console/Tester/ApplicationTester.php83
-rw-r--r--vendor/symfony/console/Tester/CommandCompletionTester.php54
-rw-r--r--vendor/symfony/console/Tester/CommandTester.php74
-rw-r--r--vendor/symfony/console/Tester/Constraint/CommandIsSuccessful.php43
-rw-r--r--vendor/symfony/console/Tester/TesterTrait.php178
5 files changed, 432 insertions, 0 deletions
diff --git a/vendor/symfony/console/Tester/ApplicationTester.php b/vendor/symfony/console/Tester/ApplicationTester.php
new file mode 100644
index 0000000..cebb6f8
--- /dev/null
+++ b/vendor/symfony/console/Tester/ApplicationTester.php
@@ -0,0 +1,83 @@
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
12namespace Symfony\Component\Console\Tester;
13
14use Symfony\Component\Console\Application;
15use Symfony\Component\Console\Input\ArrayInput;
16
17/**
18 * Eases the testing of console applications.
19 *
20 * When testing an application, don't forget to disable the auto exit flag:
21 *
22 * $application = new Application();
23 * $application->setAutoExit(false);
24 *
25 * @author Fabien Potencier <fabien@symfony.com>
26 */
27class ApplicationTester
28{
29 use TesterTrait;
30
31 public function __construct(
32 private Application $application,
33 ) {
34 }
35
36 /**
37 * Executes the application.
38 *
39 * Available options:
40 *
41 * * interactive: Sets the input interactive flag
42 * * decorated: Sets the output decorated flag
43 * * verbosity: Sets the output verbosity flag
44 * * capture_stderr_separately: Make output of stdOut and stdErr separately available
45 *
46 * @return int The command exit code
47 */
48 public function run(array $input, array $options = []): int
49 {
50 $prevShellVerbosity = getenv('SHELL_VERBOSITY');
51
52 try {
53 $this->input = new ArrayInput($input);
54 if (isset($options['interactive'])) {
55 $this->input->setInteractive($options['interactive']);
56 }
57
58 if ($this->inputs) {
59 $this->input->setStream(self::createStream($this->inputs));
60 }
61
62 $this->initOutput($options);
63
64 return $this->statusCode = $this->application->run($this->input, $this->output);
65 } finally {
66 // SHELL_VERBOSITY is set by Application::configureIO so we need to unset/reset it
67 // to its previous value to avoid one test's verbosity to spread to the following tests
68 if (false === $prevShellVerbosity) {
69 if (\function_exists('putenv')) {
70 @putenv('SHELL_VERBOSITY');
71 }
72 unset($_ENV['SHELL_VERBOSITY']);
73 unset($_SERVER['SHELL_VERBOSITY']);
74 } else {
75 if (\function_exists('putenv')) {
76 @putenv('SHELL_VERBOSITY='.$prevShellVerbosity);
77 }
78 $_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity;
79 $_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity;
80 }
81 }
82 }
83}
diff --git a/vendor/symfony/console/Tester/CommandCompletionTester.php b/vendor/symfony/console/Tester/CommandCompletionTester.php
new file mode 100644
index 0000000..76cbaf1
--- /dev/null
+++ b/vendor/symfony/console/Tester/CommandCompletionTester.php
@@ -0,0 +1,54 @@
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
12namespace Symfony\Component\Console\Tester;
13
14use Symfony\Component\Console\Command\Command;
15use Symfony\Component\Console\Completion\CompletionInput;
16use Symfony\Component\Console\Completion\CompletionSuggestions;
17
18/**
19 * Eases the testing of command completion.
20 *
21 * @author Jérôme Tamarelle <jerome@tamarelle.net>
22 */
23class CommandCompletionTester
24{
25 public function __construct(
26 private Command $command,
27 ) {
28 }
29
30 /**
31 * Create completion suggestions from input tokens.
32 */
33 public function complete(array $input): array
34 {
35 $currentIndex = \count($input);
36 if ('' === end($input)) {
37 array_pop($input);
38 }
39 array_unshift($input, $this->command->getName());
40
41 $completionInput = CompletionInput::fromTokens($input, $currentIndex);
42 $completionInput->bind($this->command->getDefinition());
43 $suggestions = new CompletionSuggestions();
44
45 $this->command->complete($completionInput, $suggestions);
46
47 $options = [];
48 foreach ($suggestions->getOptionSuggestions() as $option) {
49 $options[] = '--'.$option->getName();
50 }
51
52 return array_map('strval', array_merge($options, $suggestions->getValueSuggestions()));
53 }
54}
diff --git a/vendor/symfony/console/Tester/CommandTester.php b/vendor/symfony/console/Tester/CommandTester.php
new file mode 100644
index 0000000..d39cde7
--- /dev/null
+++ b/vendor/symfony/console/Tester/CommandTester.php
@@ -0,0 +1,74 @@
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
12namespace Symfony\Component\Console\Tester;
13
14use Symfony\Component\Console\Command\Command;
15use Symfony\Component\Console\Input\ArrayInput;
16
17/**
18 * Eases the testing of console commands.
19 *
20 * @author Fabien Potencier <fabien@symfony.com>
21 * @author Robin Chalas <robin.chalas@gmail.com>
22 */
23class CommandTester
24{
25 use TesterTrait;
26
27 public function __construct(
28 private Command $command,
29 ) {
30 }
31
32 /**
33 * Executes the command.
34 *
35 * Available execution options:
36 *
37 * * interactive: Sets the input interactive flag
38 * * decorated: Sets the output decorated flag
39 * * verbosity: Sets the output verbosity flag
40 * * capture_stderr_separately: Make output of stdOut and stdErr separately available
41 *
42 * @param array $input An array of command arguments and options
43 * @param array $options An array of execution options
44 *
45 * @return int The command exit code
46 */
47 public function execute(array $input, array $options = []): int
48 {
49 // set the command name automatically if the application requires
50 // this argument and no command name was passed
51 if (!isset($input['command'])
52 && (null !== $application = $this->command->getApplication())
53 && $application->getDefinition()->hasArgument('command')
54 ) {
55 $input = array_merge(['command' => $this->command->getName()], $input);
56 }
57
58 $this->input = new ArrayInput($input);
59 // Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
60 $this->input->setStream(self::createStream($this->inputs));
61
62 if (isset($options['interactive'])) {
63 $this->input->setInteractive($options['interactive']);
64 }
65
66 if (!isset($options['decorated'])) {
67 $options['decorated'] = false;
68 }
69
70 $this->initOutput($options);
71
72 return $this->statusCode = $this->command->run($this->input, $this->output);
73 }
74}
diff --git a/vendor/symfony/console/Tester/Constraint/CommandIsSuccessful.php b/vendor/symfony/console/Tester/Constraint/CommandIsSuccessful.php
new file mode 100644
index 0000000..09c6194
--- /dev/null
+++ b/vendor/symfony/console/Tester/Constraint/CommandIsSuccessful.php
@@ -0,0 +1,43 @@
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
12namespace Symfony\Component\Console\Tester\Constraint;
13
14use PHPUnit\Framework\Constraint\Constraint;
15use Symfony\Component\Console\Command\Command;
16
17final class CommandIsSuccessful extends Constraint
18{
19 public function toString(): string
20 {
21 return 'is successful';
22 }
23
24 protected function matches($other): bool
25 {
26 return Command::SUCCESS === $other;
27 }
28
29 protected function failureDescription($other): string
30 {
31 return 'the command '.$this->toString();
32 }
33
34 protected function additionalFailureDescription($other): string
35 {
36 $mapping = [
37 Command::FAILURE => 'Command failed.',
38 Command::INVALID => 'Command was invalid.',
39 ];
40
41 return $mapping[$other] ?? sprintf('Command returned exit status %d.', $other);
42 }
43}
diff --git a/vendor/symfony/console/Tester/TesterTrait.php b/vendor/symfony/console/Tester/TesterTrait.php
new file mode 100644
index 0000000..1ab7a70
--- /dev/null
+++ b/vendor/symfony/console/Tester/TesterTrait.php
@@ -0,0 +1,178 @@
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
12namespace Symfony\Component\Console\Tester;
13
14use PHPUnit\Framework\Assert;
15use Symfony\Component\Console\Input\InputInterface;
16use Symfony\Component\Console\Output\ConsoleOutput;
17use Symfony\Component\Console\Output\OutputInterface;
18use Symfony\Component\Console\Output\StreamOutput;
19use Symfony\Component\Console\Tester\Constraint\CommandIsSuccessful;
20
21/**
22 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
23 */
24trait TesterTrait
25{
26 private StreamOutput $output;
27 private array $inputs = [];
28 private bool $captureStreamsIndependently = false;
29 private InputInterface $input;
30 private int $statusCode;
31
32 /**
33 * Gets the display returned by the last execution of the command or application.
34 *
35 * @throws \RuntimeException If it's called before the execute method
36 */
37 public function getDisplay(bool $normalize = false): string
38 {
39 if (!isset($this->output)) {
40 throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?');
41 }
42
43 rewind($this->output->getStream());
44
45 $display = stream_get_contents($this->output->getStream());
46
47 if ($normalize) {
48 $display = str_replace(\PHP_EOL, "\n", $display);
49 }
50
51 return $display;
52 }
53
54 /**
55 * Gets the output written to STDERR by the application.
56 *
57 * @param bool $normalize Whether to normalize end of lines to \n or not
58 */
59 public function getErrorOutput(bool $normalize = false): string
60 {
61 if (!$this->captureStreamsIndependently) {
62 throw new \LogicException('The error output is not available when the tester is run without "capture_stderr_separately" option set.');
63 }
64
65 rewind($this->output->getErrorOutput()->getStream());
66
67 $display = stream_get_contents($this->output->getErrorOutput()->getStream());
68
69 if ($normalize) {
70 $display = str_replace(\PHP_EOL, "\n", $display);
71 }
72
73 return $display;
74 }
75
76 /**
77 * Gets the input instance used by the last execution of the command or application.
78 */
79 public function getInput(): InputInterface
80 {
81 return $this->input;
82 }
83
84 /**
85 * Gets the output instance used by the last execution of the command or application.
86 */
87 public function getOutput(): OutputInterface
88 {
89 return $this->output;
90 }
91
92 /**
93 * Gets the status code returned by the last execution of the command or application.
94 *
95 * @throws \RuntimeException If it's called before the execute method
96 */
97 public function getStatusCode(): int
98 {
99 return $this->statusCode ?? throw new \RuntimeException('Status code not initialized, did you execute the command before requesting the status code?');
100 }
101
102 public function assertCommandIsSuccessful(string $message = ''): void
103 {
104 Assert::assertThat($this->statusCode, new CommandIsSuccessful(), $message);
105 }
106
107 /**
108 * Sets the user inputs.
109 *
110 * @param array $inputs An array of strings representing each input
111 * passed to the command input stream
112 *
113 * @return $this
114 */
115 public function setInputs(array $inputs): static
116 {
117 $this->inputs = $inputs;
118
119 return $this;
120 }
121
122 /**
123 * Initializes the output property.
124 *
125 * Available options:
126 *
127 * * decorated: Sets the output decorated flag
128 * * verbosity: Sets the output verbosity flag
129 * * capture_stderr_separately: Make output of stdOut and stdErr separately available
130 */
131 private function initOutput(array $options): void
132 {
133 $this->captureStreamsIndependently = $options['capture_stderr_separately'] ?? false;
134 if (!$this->captureStreamsIndependently) {
135 $this->output = new StreamOutput(fopen('php://memory', 'w', false));
136 if (isset($options['decorated'])) {
137 $this->output->setDecorated($options['decorated']);
138 }
139 if (isset($options['verbosity'])) {
140 $this->output->setVerbosity($options['verbosity']);
141 }
142 } else {
143 $this->output = new ConsoleOutput(
144 $options['verbosity'] ?? ConsoleOutput::VERBOSITY_NORMAL,
145 $options['decorated'] ?? null
146 );
147
148 $errorOutput = new StreamOutput(fopen('php://memory', 'w', false));
149 $errorOutput->setFormatter($this->output->getFormatter());
150 $errorOutput->setVerbosity($this->output->getVerbosity());
151 $errorOutput->setDecorated($this->output->isDecorated());
152
153 $reflectedOutput = new \ReflectionObject($this->output);
154 $strErrProperty = $reflectedOutput->getProperty('stderr');
155 $strErrProperty->setValue($this->output, $errorOutput);
156
157 $reflectedParent = $reflectedOutput->getParentClass();
158 $streamProperty = $reflectedParent->getProperty('stream');
159 $streamProperty->setValue($this->output, fopen('php://memory', 'w', false));
160 }
161 }
162
163 /**
164 * @return resource
165 */
166 private static function createStream(array $inputs)
167 {
168 $stream = fopen('php://memory', 'r+', false);
169
170 foreach ($inputs as $input) {
171 fwrite($stream, $input.\PHP_EOL);
172 }
173
174 rewind($stream);
175
176 return $stream;
177 }
178}