ParallelConfigFactoryTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Fidry\CpuCoreCounter\CpuCoreCounter;
  14. use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
  15. use PhpCsFixer\Runner\Parallel\ParallelConfig;
  16. use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
  17. use PhpCsFixer\Tests\TestCase;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Runner\Parallel\ParallelConfigFactory
  22. */
  23. final class ParallelConfigFactoryTest extends TestCase
  24. {
  25. public function testSequentialConfigHasExactlyOneProcess(): void
  26. {
  27. $config = ParallelConfigFactory::sequential();
  28. self::assertSame(1, $config->getMaxProcesses());
  29. }
  30. /**
  31. * @see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7777#discussion_r1591623367
  32. */
  33. public function testDetectConfigurationWithoutParams(): void
  34. {
  35. $parallelConfigFactoryReflection = new \ReflectionClass(ParallelConfigFactory::class);
  36. $cpuDetector = $parallelConfigFactoryReflection->getProperty('cpuDetector');
  37. $cpuDetector->setAccessible(true);
  38. $cpuDetector->setValue($parallelConfigFactoryReflection, new CpuCoreCounter([
  39. new DummyCpuCoreFinder(7),
  40. ]));
  41. $config = ParallelConfigFactory::detect();
  42. self::assertSame(7, $config->getMaxProcesses());
  43. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
  44. self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config->getProcessTimeout());
  45. $cpuDetector->setValue($parallelConfigFactoryReflection, null);
  46. }
  47. public function testDetectConfigurationWithParams(): void
  48. {
  49. $config = ParallelConfigFactory::detect(22, 2_200);
  50. self::assertSame(22, $config->getFilesPerProcess());
  51. self::assertSame(2_200, $config->getProcessTimeout());
  52. }
  53. public function testDetectConfigurationWithDefaultValue(): void
  54. {
  55. $config = ParallelConfigFactory::detect(null, 60);
  56. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
  57. self::assertSame(60, $config->getProcessTimeout());
  58. }
  59. /**
  60. * @requires PHP 8.0
  61. */
  62. public function testDetectConfigurationWithNamedArgs(): void
  63. {
  64. // First argument omitted, second one provided via named argument
  65. $config1 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['processTimeout' => 300]);
  66. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config1->getFilesPerProcess());
  67. self::assertSame(300, $config1->getProcessTimeout());
  68. // Flipped order of arguments using named arguments syntax
  69. $config2 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], [
  70. 'processTimeout' => 300,
  71. 'filesPerProcess' => 5,
  72. ]);
  73. self::assertSame(5, $config2->getFilesPerProcess());
  74. self::assertSame(300, $config2->getProcessTimeout());
  75. // Only first argument provided, but via named argument
  76. $config3 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['filesPerProcess' => 7]);
  77. self::assertSame(7, $config3->getFilesPerProcess());
  78. self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config3->getProcessTimeout());
  79. }
  80. }