|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Webmozarts Console Parallelization package. |
| 5 | + * |
| 6 | + * (c) Webmozarts GmbH <[email protected]> |
| 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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Webmozarts\Console\Parallelization\Integration; |
| 15 | + |
| 16 | +use PHPUnit\Framework\Attributes\CoversNothing; |
| 17 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 20 | +use Symfony\Component\Console\Tester\CommandTester; |
| 21 | +use Symfony\Component\Filesystem\Filesystem; |
| 22 | +use Webmozarts\Console\Parallelization\Fixtures\Command\DebugChildProcessCommand; |
| 23 | +use Webmozarts\Console\Parallelization\Integration\App\BareKernel; |
| 24 | +use Webmozarts\Console\Parallelization\Logger\DummyLogger; |
| 25 | +use function file_get_contents; |
| 26 | + |
| 27 | +/** |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +#[CoversNothing] |
| 31 | +class DebugChildProcessInputsTest extends TestCase |
| 32 | +{ |
| 33 | + private DebugChildProcessCommand $command; |
| 34 | + private CommandTester $commandTester; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + $this->command = (new Application(new BareKernel()))->add(new DebugChildProcessCommand()); |
| 39 | + $this->commandTester = new CommandTester($this->command); |
| 40 | + } |
| 41 | + |
| 42 | + protected function tearDown(): void |
| 43 | + { |
| 44 | + (new Filesystem())->remove(DebugChildProcessCommand::OUTPUT_FILE); |
| 45 | + } |
| 46 | + |
| 47 | + #[DataProvider('inputProvider')] |
| 48 | + public function test_it_can_run_the_command_without_sub_processes( |
| 49 | + string $item, |
| 50 | + ?string $simpleOption, |
| 51 | + array $arrayOption, |
| 52 | + string $expected, |
| 53 | + ): void { |
| 54 | + $logger = new DummyLogger(); |
| 55 | + |
| 56 | + $this->command->setItem($item); |
| 57 | + $this->command->setLogger($logger); |
| 58 | + |
| 59 | + $this->commandTester->execute( |
| 60 | + [ |
| 61 | + 'command' => 'debug:process', |
| 62 | + '--simple-option' => $simpleOption, |
| 63 | + '--array-option' => $arrayOption, |
| 64 | + ], |
| 65 | + ['interactive' => true], |
| 66 | + ); |
| 67 | + |
| 68 | + $output = $this->commandTester->getDisplay(); |
| 69 | + $actual = file_get_contents(DebugChildProcessCommand::OUTPUT_FILE); |
| 70 | + |
| 71 | + $this->commandTester->assertCommandIsSuccessful($output); |
| 72 | + self::assertSame($expected, $actual, $output); |
| 73 | + } |
| 74 | + |
| 75 | + public static function inputProvider(): iterable |
| 76 | + { |
| 77 | + // This test fails... |
| 78 | + // yield 'default' => [ |
| 79 | + // 'item', |
| 80 | + // null, |
| 81 | + // [], |
| 82 | + // DebugChildProcessCommand::createContent( |
| 83 | + // 'item', |
| 84 | + // '', |
| 85 | + // [], |
| 86 | + // ), |
| 87 | + // ]; |
| 88 | + |
| 89 | + yield 'with values' => [ |
| 90 | + 'item', |
| 91 | + 'option', |
| 92 | + ['option1', 'option2'], |
| 93 | + DebugChildProcessCommand::createContent( |
| 94 | + 'item', |
| 95 | + 'option', |
| 96 | + ['option1--array-option=option2'], |
| 97 | + ), |
| 98 | + ]; |
| 99 | + |
| 100 | + yield 'escaped string token' => [ |
| 101 | + '"foo"', |
| 102 | + '"bar"', |
| 103 | + ['"option1"', '"option2"'], |
| 104 | + DebugChildProcessCommand::createContent( |
| 105 | + '"foo"', |
| 106 | + '"\"bar\""', |
| 107 | + ['"\"option1\""--array-option="\"option2\""'], |
| 108 | + ), |
| 109 | + ]; |
| 110 | + |
| 111 | + yield 'escaped string token with both types of quotes' => [ |
| 112 | + '"o_id in(\'20\')"', |
| 113 | + '"p_id in(\'22\')"', |
| 114 | + ['"option in(\'1\')"', '"option in(\'2\')"'], |
| 115 | + DebugChildProcessCommand::createContent( |
| 116 | + '"o_id in(\'20\')"', |
| 117 | + '"\"p_id in(\'22\')\""', |
| 118 | + ['"\"option in(\'1\')\""--array-option="\"option in(\'2\')\""'], |
| 119 | + ), |
| 120 | + ]; |
| 121 | + |
| 122 | + yield 'with values with spaces' => [ |
| 123 | + 'a b c d', |
| 124 | + 'd c b a', |
| 125 | + ['option 1', 'option 2'], |
| 126 | + DebugChildProcessCommand::createContent( |
| 127 | + 'a b c d', |
| 128 | + '"d c b a"', |
| 129 | + ['"option 1"--array-option="option 2"'], |
| 130 | + ), |
| 131 | + ]; |
| 132 | + } |
| 133 | +} |
0 commit comments