diff --git a/Attribute/Option.php b/Attribute/Option.php index 2f0256b17..788353463 100644 --- a/Attribute/Option.php +++ b/Attribute/Option.php @@ -158,7 +158,7 @@ public function resolveValue(InputInterface $input): mixed private function handleUnion(\ReflectionUnionType $type): self { $types = array_map( - static fn(\ReflectionType $t) => $t instanceof \ReflectionNamedType ? $t->getName() : null, + static fn (\ReflectionType $t) => $t instanceof \ReflectionNamedType ? $t->getName() : null, $type->getTypes(), ); diff --git a/Command/Command.php b/Command/Command.php index f6cd84997..72a10cf76 100644 --- a/Command/Command.php +++ b/Command/Command.php @@ -134,7 +134,7 @@ public function __construct(?string $name = null) $this->setHelp($attribute?->help ?? ''); } - if (\is_callable($this) && (new \ReflectionMethod($this, 'execute'))->getDeclaringClass()->name === self::class) { + if (\is_callable($this) && self::class === (new \ReflectionMethod($this, 'execute'))->getDeclaringClass()->name) { $this->code = new InvokableCommand($this, $this(...)); } diff --git a/Command/TraceableCommand.php b/Command/TraceableCommand.php index 315f385de..ed11cc29f 100644 --- a/Command/TraceableCommand.php +++ b/Command/TraceableCommand.php @@ -292,7 +292,7 @@ public function run(InputInterface $input, OutputInterface $output): int $event = $this->stopwatch->start($this->getName(), 'command'); try { - $this->exitCode = parent::run($input, $output); + $this->exitCode = $this->command->run($input, $output); } finally { $event->stop(); diff --git a/Descriptor/JsonDescriptor.php b/Descriptor/JsonDescriptor.php index 956303709..9a8e696cd 100644 --- a/Descriptor/JsonDescriptor.php +++ b/Descriptor/JsonDescriptor.php @@ -108,7 +108,7 @@ private function getInputOptionData(InputOption $option, bool $negated = false): 'is_value_required' => false, 'is_multiple' => false, 'description' => 'Negate the "--'.$option->getName().'" option', - 'default' => false, + 'default' => null === $option->getDefault() ? null : !$option->getDefault(), ] : [ 'name' => '--'.$option->getName(), 'shortcut' => $option->getShortcut() ? '-'.str_replace('|', '|-', $option->getShortcut()) : '', diff --git a/Formatter/OutputFormatter.php b/Formatter/OutputFormatter.php index 3c8c287e8..eab86976d 100644 --- a/Formatter/OutputFormatter.php +++ b/Formatter/OutputFormatter.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Console\Formatter; use Symfony\Component\Console\Exception\InvalidArgumentException; +use Symfony\Component\Console\Helper\Helper; use function Symfony\Component\String\b; @@ -136,9 +137,11 @@ public function formatAndWrap(?string $message, int $width): string continue; } + // convert byte position to character position. + $pos = Helper::length(substr($message, 0, $pos)); // add the text up to the next tag - $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength); - $offset = $pos + \strlen($text); + $output .= $this->applyCurrentStyle(Helper::substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength); + $offset = $pos + Helper::length($text); // opening tag? if ($open = '/' !== $text[1]) { @@ -159,7 +162,7 @@ public function formatAndWrap(?string $message, int $width): string } } - $output .= $this->applyCurrentStyle(substr($message, $offset), $output, $width, $currentLineLength); + $output .= $this->applyCurrentStyle(Helper::substr($message, $offset), $output, $width, $currentLineLength); return strtr($output, ["\0" => '\\', '\\<' => '<', '\\>' => '>']); } @@ -226,8 +229,18 @@ private function applyCurrentStyle(string $text, string $current, int $width, in } if ($currentLineLength) { - $prefix = substr($text, 0, $i = $width - $currentLineLength)."\n"; - $text = substr($text, $i); + $lines = explode("\n", $text, 2); + $prefix = Helper::substr($lines[0], 0, $i = $width - $currentLineLength)."\n"; + $text = Helper::substr($lines[0], $i); + + if (isset($lines[1])) { + // $prefix may contain the full first line in which the \n is already a part of $prefix. + if ('' !== $text) { + $text .= "\n"; + } + + $text .= $lines[1]; + } } else { $prefix = ''; } @@ -242,8 +255,8 @@ private function applyCurrentStyle(string $text, string $current, int $width, in $lines = explode("\n", $text); - foreach ($lines as $line) { - $currentLineLength += \strlen($line); + foreach ($lines as $i => $line) { + $currentLineLength = 0 === $i ? $currentLineLength + Helper::length($line) : Helper::length($line); if ($width <= $currentLineLength) { $currentLineLength = 0; } diff --git a/Helper/Helper.php b/Helper/Helper.php index bdd8d9e95..46e7e2f58 100644 --- a/Helper/Helper.php +++ b/Helper/Helper.php @@ -80,6 +80,10 @@ public static function substr(?string $string, int $from, ?int $length = null): { $string ??= ''; + if (preg_match('//u', $string)) { + return (new UnicodeString($string))->slice($from, $length); + } + if (false === $encoding = mb_detect_encoding($string, null, true)) { return substr($string, $from, $length); } diff --git a/Helper/TreeNode.php b/Helper/TreeNode.php index 7f2ed8a4a..8c35266c1 100644 --- a/Helper/TreeNode.php +++ b/Helper/TreeNode.php @@ -58,7 +58,7 @@ public function getValue(): string public function addChild(self|string|callable $node): self { if (\is_string($node)) { - $node = new self($node, $this); + $node = new self($node); } $this->children[] = $node; diff --git a/Messenger/RunCommandMessageHandler.php b/Messenger/RunCommandMessageHandler.php index 0fdf7d017..df5f48af0 100644 --- a/Messenger/RunCommandMessageHandler.php +++ b/Messenger/RunCommandMessageHandler.php @@ -16,6 +16,8 @@ use Symfony\Component\Console\Exception\RunCommandFailedException; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\BufferedOutput; +use Symfony\Component\Messenger\Exception\RecoverableExceptionInterface; +use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface; /** * @author Kevin Bond @@ -36,6 +38,8 @@ public function __invoke(RunCommandMessage $message): RunCommandContext try { $exitCode = $this->application->run($input, $output); + } catch (UnrecoverableExceptionInterface|RecoverableExceptionInterface $e) { + throw $e; } catch (\Throwable $e) { throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch())); } diff --git a/Tests/Command/InvokableCommandTest.php b/Tests/Command/InvokableCommandTest.php index 5ab7951e7..9fc40809a 100644 --- a/Tests/Command/InvokableCommandTest.php +++ b/Tests/Command/InvokableCommandTest.php @@ -156,6 +156,7 @@ public function testExecuteHasPriorityOverInvokeMethod() { $command = new class extends Command { public string $called; + protected function execute(InputInterface $input, OutputInterface $output): int { $this->called = __FUNCTION__; @@ -179,6 +180,7 @@ public function testCallInvokeMethodWhenExtendingCommandClass() { $command = new class extends Command { public string $called; + public function __invoke(): int { $this->called = __FUNCTION__; @@ -195,7 +197,9 @@ public function testInvalidReturnType() { $command = new Command('foo'); $command->setCode(new class { - public function __invoke() {} + public function __invoke() + { + } }); $this->expectException(\TypeError::class); @@ -333,16 +337,16 @@ public function testInvalidOptionDefinition(callable $code) public static function provideInvalidOptionDefinitions(): \Generator { yield 'no-default' => [ - function (#[Option] string $a) {} + function (#[Option] string $a) {}, ]; yield 'nullable-bool-default-true' => [ - function (#[Option] ?bool $a = true) {} + function (#[Option] ?bool $a = true) {}, ]; yield 'nullable-bool-default-false' => [ - function (#[Option] ?bool $a = false) {} + function (#[Option] ?bool $a = false) {}, ]; yield 'invalid-union-type' => [ - function (#[Option] array|bool $a = false) {} + function (#[Option] array|bool $a = false) {}, ]; yield 'union-type-cannot-allow-null' => [ function (#[Option] string|bool|null $a = null) {}, diff --git a/Tests/Command/TraceableCommandTest.php b/Tests/Command/TraceableCommandTest.php new file mode 100644 index 000000000..1bf709f8b --- /dev/null +++ b/Tests/Command/TraceableCommandTest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Command; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Command\TraceableCommand; +use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Console\Tests\Fixtures\LoopExampleCommand; +use Symfony\Component\Stopwatch\Stopwatch; + +class TraceableCommandTest extends TestCase +{ + private Application $application; + + protected function setUp(): void + { + $this->application = new Application(); + $this->application->add(new LoopExampleCommand()); + } + + public function testRunIsOverriddenWithoutProfile() + { + $command = $this->application->find('app:loop:example'); + $commandTester = new CommandTester($command); + $commandTester->execute([]); + $commandTester->assertCommandIsSuccessful(); + + $output = $commandTester->getDisplay(); + $this->assertLoopOutputCorrectness($output); + } + + public function testRunIsNotOverriddenWithProfile() + { + // Simulate the bug environment by wrapping + // our command in TraceableCommand, which is what Symfony does + // when you use the --profile option. + $command = new LoopExampleCommand(); + $traceableCommand = new TraceableCommand($command, new Stopwatch()); + + $this->application->add($traceableCommand); + + $commandTester = new CommandTester($traceableCommand); + $commandTester->execute([]); + $commandTester->assertCommandIsSuccessful(); + + $output = $commandTester->getDisplay(); + $this->assertLoopOutputCorrectness($output); + } + + public function assertLoopOutputCorrectness(string $output) + { + $completeChar = '\\' !== \DIRECTORY_SEPARATOR ? '▓' : '='; + self::assertMatchesRegularExpression('~3/3\s+\['.$completeChar.'+]\s+100%~u', $output); + self::assertStringContainsString('Loop finished.', $output); + self::assertEquals(3, substr_count($output, 'Hello world')); + } +} diff --git a/Tests/Descriptor/JsonDescriptorTest.php b/Tests/Descriptor/JsonDescriptorTest.php index 399bd8f23..914ed3597 100644 --- a/Tests/Descriptor/JsonDescriptorTest.php +++ b/Tests/Descriptor/JsonDescriptorTest.php @@ -36,10 +36,9 @@ private function normalizeOutputRecursively($output) return array_map($this->normalizeOutputRecursively(...), $output); } - if (null === $output) { - return null; - } - - return parent::normalizeOutput($output); + return match ($output) { + null, true, false => $output, + default => parent::normalizeOutput($output), + }; } } diff --git a/Tests/Fixtures/AbstractLoopCommand.php b/Tests/Fixtures/AbstractLoopCommand.php new file mode 100644 index 000000000..c3715067e --- /dev/null +++ b/Tests/Fixtures/AbstractLoopCommand.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Fixtures; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +abstract class AbstractLoopCommand extends Command +{ + public function run(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $contexts = [1, 2, 3]; + $io->progressStart(count($contexts)); + $code = self::SUCCESS; + + foreach ($contexts as $ignored) { + $io->progressAdvance(); + try { + parent::run($input, $output); + } catch (\Throwable) { + $code = self::FAILURE; + } + } + $io->progressFinish(); + $output->writeln("\nLoop finished."); + + return $code; + } +} diff --git a/Tests/Fixtures/LoopExampleCommand.php b/Tests/Fixtures/LoopExampleCommand.php new file mode 100644 index 000000000..d9eeb4db9 --- /dev/null +++ b/Tests/Fixtures/LoopExampleCommand.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Fixtures; + +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +#[AsCommand('app:loop:example')] +class LoopExampleCommand extends AbstractLoopCommand +{ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $output->writeln(' Hello world'); + + return Command::SUCCESS; + } +} diff --git a/Tests/Fixtures/application_2.json b/Tests/Fixtures/application_2.json index 4a6f411f5..c0e66444e 100644 --- a/Tests/Fixtures/application_2.json +++ b/Tests/Fixtures/application_2.json @@ -94,7 +94,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null }, "shell": { "name": "--shell", @@ -224,7 +224,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null }, "debug": { "name": "--debug", @@ -345,7 +345,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null } } } @@ -457,7 +457,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null }, "short": { "name": "--short", @@ -554,7 +554,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null } } } @@ -659,7 +659,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null } } } @@ -745,7 +745,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null } } } @@ -833,7 +833,7 @@ "is_value_required": false, "is_multiple": false, "description": "Do not ask any interactive question", - "default": false + "default": null } } } diff --git a/Tests/Formatter/OutputFormatterTest.php b/Tests/Formatter/OutputFormatterTest.php index 477f1bdf6..b66b6abe4 100644 --- a/Tests/Formatter/OutputFormatterTest.php +++ b/Tests/Formatter/OutputFormatterTest.php @@ -177,7 +177,7 @@ public function testInlineStyleOptions(string $tag, ?string $expected = null, ?s $expected = $tag.$input.''; $this->assertSame($expected, $formatter->format($expected)); } else { - /* @var OutputFormatterStyle $result */ + /** @var OutputFormatterStyle $result */ $this->assertInstanceOf(OutputFormatterStyle::class, $result); $this->assertSame($expected, $formatter->format($tag.$input.'')); $this->assertSame($expected, $formatter->format($tag.$input.'')); @@ -365,6 +365,14 @@ public function testFormatAndWrap() $this->assertSame("Lore\nm \e[37;41mip\e[39;49m\n\e[37;41msum\e[39;49m \ndolo\nr \e[32msi\e[39m\n\e[32mt\e[39m am\net", $formatter->formatAndWrap('Lorem ipsum dolor sit amet', 4)); $this->assertSame("Lorem \e[37;41mip\e[39;49m\n\e[37;41msum\e[39;49m dolo\nr \e[32msit\e[39m am\net", $formatter->formatAndWrap('Lorem ipsum dolor sit amet', 8)); $this->assertSame("Lorem \e[37;41mipsum\e[39;49m dolor \e[32m\e[39m\n\e[32msit\e[39m, \e[37;41mamet\e[39;49m et \e[32mlauda\e[39m\n\e[32mntium\e[39m architecto", $formatter->formatAndWrap('Lorem ipsum dolor sit, amet et laudantium architecto', 18)); + $this->assertSame("\e[37;41mnon-empty-array\e[39;49m\e[37;41m\e[39;49m given.\n🪪 argument.type", $formatter->formatAndWrap("non-empty-array given.\n🪪 argument.type", 38)); + $this->assertSame("Usuário {{user_name}} não é válid\no.", $formatter->formatAndWrap('Usuário {{user_name}} não é válido.', 50)); + $this->assertSame("foo\e[37;41mb\e[39;49m\n\e[37;41mar\e[39;49mbaz", $formatter->formatAndWrap("foob\narbaz", 7)); + $this->assertSame("foo\e[37;41mbar\e[39;49mbaz\nnewline", $formatter->formatAndWrap("foobarbaz\nnewline", 11)); + $this->assertSame("foobarbaz\n\e[37;41mnewline\e[39;49m", $formatter->formatAndWrap("foobarbaz\nnewline", 11)); + $this->assertSame("foobar\e[37;41mbaz\e[39;49m\n\e[37;41mnewline\e[39;49m", $formatter->formatAndWrap("foobarbaz\nnewline", 11)); + $this->assertSame("foobar\e[37;41mbazne\e[39;49m\n\e[37;41mwline\e[39;49m", $formatter->formatAndWrap("foobarbazne\nwline", 11)); + $this->assertSame("foobar\e[37;41mbazne\e[39;49m\n\e[37;41mw\e[39;49m\n\e[37;41mline\e[39;49m", $formatter->formatAndWrap("foobarbaznew\nline", 11)); $formatter = new OutputFormatter(); @@ -376,6 +384,14 @@ public function testFormatAndWrap() $this->assertSame("Â rèälly\nlöng tîtlè\nthät cöüld\nnèêd\nmúltîplê\nlínès", $formatter->formatAndWrap('Â rèälly löng tîtlè thät cöüld nèêd múltîplê línès', 10)); $this->assertSame("Â rèälly\nlöng tîtlè\nthät cöüld\nnèêd\nmúltîplê\n línès", $formatter->formatAndWrap("Â rèälly löng tîtlè thät cöüld nèêd múltîplê\n línès", 10)); $this->assertSame('', $formatter->formatAndWrap(null, 5)); + $this->assertSame("non-empty-array given.\n🪪 argument.type", $formatter->formatAndWrap("non-empty-array given.\n🪪 argument.type", 38)); + $this->assertSame("Usuário {{user_name}} não é válid\no.", $formatter->formatAndWrap('Usuário {{user_name}} não é válido.', 50)); + $this->assertSame("foob\narbaz", $formatter->formatAndWrap("foob\narbaz", 7)); + $this->assertSame("foobarbaz\nnewline", $formatter->formatAndWrap("foobarbaz\nnewline", 11)); + $this->assertSame("foobarbaz\nnewline", $formatter->formatAndWrap("foobarbaz\nnewline", 11)); + $this->assertSame("foobarbaz\nnewline", $formatter->formatAndWrap("foobarbaz\nnewline", 11)); + $this->assertSame("foobarbazne\nwline", $formatter->formatAndWrap("foobarbazne\nwline", 11)); + $this->assertSame("foobarbazne\nw\nline", $formatter->formatAndWrap("foobarbaznew\nline", 11)); } } diff --git a/Tests/Helper/ProgressBarTest.php b/Tests/Helper/ProgressBarTest.php index ba74035f5..c0278cc33 100644 --- a/Tests/Helper/ProgressBarTest.php +++ b/Tests/Helper/ProgressBarTest.php @@ -423,7 +423,7 @@ public function testOverwriteWithSectionOutputAndEol() $output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter()); $bar = new ProgressBar($output, 50, 0); - $bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '%message%' . PHP_EOL); + $bar->setFormat('[%bar%] %percent:3s%%'.\PHP_EOL.'%message%'.\PHP_EOL); $bar->setMessage(''); $bar->start(); $bar->display(); @@ -435,8 +435,8 @@ public function testOverwriteWithSectionOutputAndEol() rewind($output->getStream()); $this->assertEquals(escapeshellcmd( '[>---------------------------] 0%'.\PHP_EOL.\PHP_EOL. - "\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL. 'Doing something...' . \PHP_EOL . - "\x1b[2A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. 'Doing something foo...' . \PHP_EOL), + "\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL.'Doing something...'.\PHP_EOL. + "\x1b[2A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL.'Doing something foo...'.\PHP_EOL), escapeshellcmd(stream_get_contents($output->getStream())) ); } @@ -448,7 +448,7 @@ public function testOverwriteWithSectionOutputAndEolWithEmptyMessage() $output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter()); $bar = new ProgressBar($output, 50, 0); - $bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '%message%'); + $bar->setFormat('[%bar%] %percent:3s%%'.\PHP_EOL.'%message%'); $bar->setMessage('Start'); $bar->start(); $bar->display(); @@ -460,8 +460,8 @@ public function testOverwriteWithSectionOutputAndEolWithEmptyMessage() rewind($output->getStream()); $this->assertEquals(escapeshellcmd( '[>---------------------------] 0%'.\PHP_EOL.'Start'.\PHP_EOL. - "\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL . - "\x1b[1A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. 'Doing something...' . \PHP_EOL), + "\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL. + "\x1b[1A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL.'Doing something...'.\PHP_EOL), escapeshellcmd(stream_get_contents($output->getStream())) ); } @@ -473,7 +473,7 @@ public function testOverwriteWithSectionOutputAndEolWithEmptyMessageComment() $output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter()); $bar = new ProgressBar($output, 50, 0); - $bar->setFormat('[%bar%] %percent:3s%%' . PHP_EOL . '%message%'); + $bar->setFormat('[%bar%] %percent:3s%%'.\PHP_EOL.'%message%'); $bar->setMessage('Start'); $bar->start(); $bar->display(); @@ -485,8 +485,8 @@ public function testOverwriteWithSectionOutputAndEolWithEmptyMessageComment() rewind($output->getStream()); $this->assertEquals(escapeshellcmd( '[>---------------------------] 0%'.\PHP_EOL."\x1b[33mStart\x1b[39m".\PHP_EOL. - "\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL . - "\x1b[1A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL. "\x1b[33mDoing something...\x1b[39m" . \PHP_EOL), + "\x1b[2A\x1b[0J".'[>---------------------------] 2%'.\PHP_EOL. + "\x1b[1A\x1b[0J".'[=>--------------------------] 4%'.\PHP_EOL."\x1b[33mDoing something...\x1b[39m".\PHP_EOL), escapeshellcmd(stream_get_contents($output->getStream())) ); } diff --git a/Tests/Helper/TableTest.php b/Tests/Helper/TableTest.php index 52ae23301..eb85364da 100644 --- a/Tests/Helper/TableTest.php +++ b/Tests/Helper/TableTest.php @@ -2099,12 +2099,12 @@ public function testGithubIssue60038WidthOfCellWithEmoji() ->setHeaderTitle('Test Title') ->setHeaders(['Title', 'Author']) ->setRows([ - ["🎭 💫 ☯"." Divine Comedy", "Dante Alighieri"], + ['🎭 💫 ☯ Divine Comedy', 'Dante Alighieri'], // the snowflake (e2 9d 84 ef b8 8f) has a variant selector - ["👑 ❄️ 🗡"." Game of Thrones", "George R.R. Martin"], + ['👑 ❄️ 🗡 Game of Thrones', 'George R.R. Martin'], // the snowflake in text style (e2 9d 84 ef b8 8e) has a variant selector - ["❄︎❄︎❄︎ snowflake in text style ❄︎❄︎❄︎", ""], - ["And a very long line to show difference in previous lines", ""], + ['❄︎❄︎❄︎ snowflake in text style ❄︎❄︎❄︎', ''], + ['And a very long line to show difference in previous lines', ''], ]) ; $table->render(); diff --git a/Tests/Helper/TreeHelperTest.php b/Tests/Helper/TreeHelperTest.php index 15ec0f0b2..5d1399b27 100644 --- a/Tests/Helper/TreeHelperTest.php +++ b/Tests/Helper/TreeHelperTest.php @@ -195,6 +195,26 @@ public function testRenderNodeWithMultipleChildren() TREE, self::normalizeLineBreaks(trim($output->fetch()))); } + public function testRenderNodeWithMultipleChildrenWithStringConversion() + { + $rootNode = new TreeNode('Root'); + + $rootNode->addChild('Child 1'); + $rootNode->addChild('Child 2'); + $rootNode->addChild('Child 3'); + + $output = new BufferedOutput(); + $tree = TreeHelper::createTree($output, $rootNode); + + $tree->render(); + $this->assertSame(<<fetch()))); + } + public function testRenderTreeWithDuplicateNodeNames() { $rootNode = new TreeNode('Root'); diff --git a/Tests/Helper/TreeNodeTest.php b/Tests/Helper/TreeNodeTest.php index 981e7ea47..0e80da3bd 100644 --- a/Tests/Helper/TreeNodeTest.php +++ b/Tests/Helper/TreeNodeTest.php @@ -34,6 +34,24 @@ public function testAddingChildren() $this->assertSame($child, iterator_to_array($root->getChildren())[0]); } + public function testAddingChildrenAsString() + { + $root = new TreeNode('Root'); + + $root->addChild('Child 1'); + $root->addChild('Child 2'); + + $this->assertSame(2, iterator_count($root->getChildren())); + + $children = iterator_to_array($root->getChildren()); + + $this->assertSame(0, iterator_count($children[0]->getChildren())); + $this->assertSame(0, iterator_count($children[1]->getChildren())); + + $this->assertSame('Child 1', $children[0]->getValue()); + $this->assertSame('Child 2', $children[1]->getValue()); + } + public function testAddingChildrenWithGenerators() { $root = new TreeNode('Root'); diff --git a/Tests/Messenger/RunCommandMessageHandlerTest.php b/Tests/Messenger/RunCommandMessageHandlerTest.php index 58b33d565..898492374 100644 --- a/Tests/Messenger/RunCommandMessageHandlerTest.php +++ b/Tests/Messenger/RunCommandMessageHandlerTest.php @@ -20,6 +20,10 @@ use Symfony\Component\Console\Messenger\RunCommandMessage; use Symfony\Component\Console\Messenger\RunCommandMessageHandler; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Messenger\Exception\RecoverableExceptionInterface; +use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException; +use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface; +use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; /** * @author Kevin Bond @@ -81,6 +85,38 @@ public function testThrowOnNonSuccess() $this->fail('Exception not thrown.'); } + public function testExecutesCommandThatThrownUnrecoverableException() + { + $handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); + + try { + $handler(new RunCommandMessage('test:command --throw-unrecoverable')); + } catch (UnrecoverableExceptionInterface $e) { + $this->assertSame('Unrecoverable exception message', $e->getMessage()); + $this->assertNull($e->getPrevious()); + + return; + } + + $this->fail('Exception not thrown.'); + } + + public function testExecutesCommandThatThrownRecoverableException() + { + $handler = new RunCommandMessageHandler($this->createApplicationWithCommand()); + + try { + $handler(new RunCommandMessage('test:command --throw-recoverable')); + } catch (RecoverableExceptionInterface $e) { + $this->assertSame('Recoverable exception message', $e->getMessage()); + $this->assertNull($e->getPrevious()); + + return; + } + + $this->fail('Exception not thrown.'); + } + private function createApplicationWithCommand(): Application { $application = new Application(); @@ -92,6 +128,8 @@ public function configure(): void $this ->setName('test:command') ->addOption('throw') + ->addOption('throw-unrecoverable') + ->addOption('throw-recoverable') ->addOption('exit', null, InputOption::VALUE_REQUIRED, 0) ; } @@ -100,6 +138,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $output->write('some message'); + if ($input->getOption('throw-unrecoverable')) { + throw new UnrecoverableMessageHandlingException('Unrecoverable exception message'); + } + + if ($input->getOption('throw-recoverable')) { + throw new RecoverableMessageHandlingException('Recoverable exception message'); + } + if ($input->getOption('throw')) { throw new \RuntimeException('exception message'); }