ProcessFactoryTest.php 5.0 KB

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