ProcessFactoryTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Runner\Parallel;
  13. use PhpCsFixer\Console\Command\FixCommand;
  14. use PhpCsFixer\Preg;
  15. use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
  16. use PhpCsFixer\Runner\Parallel\Process;
  17. use PhpCsFixer\Runner\Parallel\ProcessFactory;
  18. use PhpCsFixer\Runner\Parallel\ProcessIdentifier;
  19. use PhpCsFixer\Runner\RunnerConfig;
  20. use PhpCsFixer\Tests\TestCase;
  21. use PhpCsFixer\ToolInfo;
  22. use React\EventLoop\StreamSelectLoop;
  23. use Symfony\Component\Console\Application;
  24. use Symfony\Component\Console\Input\ArrayInput;
  25. use Symfony\Component\Console\Input\InputDefinition;
  26. /**
  27. * @author Greg Korba <greg@codito.dev>
  28. *
  29. * @internal
  30. *
  31. * @covers \PhpCsFixer\Runner\Parallel\ProcessFactory
  32. */
  33. final class ProcessFactoryTest extends TestCase
  34. {
  35. private InputDefinition $inputDefinition;
  36. protected function setUp(): void
  37. {
  38. $fixCommand = new FixCommand(new ToolInfo());
  39. $application = new Application();
  40. $application->add($fixCommand);
  41. // In order to have full list of options supported by the command (e.g. `--verbose`)
  42. $fixCommand->mergeApplicationDefinition(false);
  43. $this->inputDefinition = $fixCommand->getDefinition();
  44. }
  45. /**
  46. * This test is not executed on Windows because process pipes are not supported there, due to their blocking nature
  47. * on this particular OS. The cause of this lays in `react/child-process` component, but it's related only to tests,
  48. * as parallel runner works properly on Windows too. Feel free to fiddle with it and add testing support for Windows.
  49. *
  50. * @requires OS Linux|Darwin
  51. *
  52. * @param array<string, string> $input
  53. *
  54. * @dataProvider provideCreateCases
  55. */
  56. public function testCreate(array $input, RunnerConfig $config, string $expectedAdditionalArgs): void
  57. {
  58. $factory = new ProcessFactory(new ArrayInput($input, $this->inputDefinition));
  59. $identifier = ProcessIdentifier::create();
  60. $process = $factory->create(new StreamSelectLoop(), $config, $identifier, 1_234);
  61. $command = \Closure::bind(static fn (Process $process): string => $process->command, null, Process::class)($process);
  62. // PHP binary and Fixer executable are not fixed, so we need to remove them from the command
  63. $command = Preg::replace('/^(.*php-cs-fixer[\'"]? )+(.+)/', '$2', $command);
  64. self::assertSame(
  65. trim(
  66. \sprintf(
  67. 'worker --port 1234 --identifier \'%s\' %s',
  68. $identifier->toString(),
  69. trim($expectedAdditionalArgs)
  70. )
  71. ),
  72. $command
  73. );
  74. $timeoutSeconds = \Closure::bind(static fn (Process $process): int => $process->timeoutSeconds, null, Process::class)($process);
  75. self::assertSame($config->getParallelConfig()->getProcessTimeout(), $timeoutSeconds);
  76. }
  77. /**
  78. * @return iterable<array{0: array<string, mixed>, 1: RunnerConfig, 2: string}>
  79. */
  80. public static function provideCreateCases(): iterable
  81. {
  82. yield 'no additional params' => [[], self::createRunnerConfig(false), ''];
  83. yield 'dry run' => [[], self::createRunnerConfig(true), '--dry-run'];
  84. yield 'diff enabled' => [['--diff' => true], self::createRunnerConfig(false), '--diff'];
  85. yield 'stop-on-violation enabled' => [['--stop-on-violation' => true], self::createRunnerConfig(false), '--stop-on-violation'];
  86. yield 'allow risky' => [['--allow-risky' => 'yes'], self::createRunnerConfig(false), '--allow-risky \'yes\''];
  87. yield 'config' => [['--config' => 'foo.php'], self::createRunnerConfig(false), '--config \'foo.php\''];
  88. yield 'rules' => [['--rules' => '@PhpCsFixer'], self::createRunnerConfig(false), '--rules \'@PhpCsFixer\''];
  89. yield 'using-cache' => [['--using-cache' => 'no'], self::createRunnerConfig(false), '--using-cache \'no\''];
  90. yield 'cache-file' => [
  91. ['--cache-file' => 'cache.json'],
  92. self::createRunnerConfig(false),
  93. '--cache-file \'cache.json\'',
  94. ];
  95. yield 'dry run with other options' => [
  96. [
  97. '--config' => 'conf.php',
  98. '--diff' => true,
  99. '--using-cache' => 'yes',
  100. '--stop-on-violation' => true,
  101. ],
  102. self::createRunnerConfig(true),
  103. '--dry-run --diff --stop-on-violation --config \'conf.php\' --using-cache \'yes\'',
  104. ];
  105. }
  106. private static function createRunnerConfig(bool $dryRun): RunnerConfig
  107. {
  108. return new RunnerConfig($dryRun, false, ParallelConfigFactory::sequential());
  109. }
  110. }