ParallelConfigFactoryTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. \Closure::bind(static function (): void {
  36. ParallelConfigFactory::$cpuDetector = new CpuCoreCounter([
  37. new DummyCpuCoreFinder(7),
  38. ]);
  39. }, null, ParallelConfigFactory::class)();
  40. $config = ParallelConfigFactory::detect();
  41. self::assertSame(7, $config->getMaxProcesses());
  42. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
  43. self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config->getProcessTimeout());
  44. \Closure::bind(static function (): void {
  45. ParallelConfigFactory::$cpuDetector = null;
  46. }, null, ParallelConfigFactory::class)();
  47. }
  48. public function testDetectConfigurationWithParams(): void
  49. {
  50. $config = ParallelConfigFactory::detect(22, 2_200);
  51. self::assertSame(22, $config->getFilesPerProcess());
  52. self::assertSame(2_200, $config->getProcessTimeout());
  53. }
  54. public function testDetectConfigurationWithDefaultValue(): void
  55. {
  56. $config = ParallelConfigFactory::detect(null, 60);
  57. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config->getFilesPerProcess());
  58. self::assertSame(60, $config->getProcessTimeout());
  59. }
  60. /**
  61. * @requires PHP 8.0
  62. */
  63. public function testDetectConfigurationWithNamedArgs(): void
  64. {
  65. // First argument omitted, second one provided via named argument
  66. $config1 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['processTimeout' => 300]);
  67. self::assertSame(ParallelConfig::DEFAULT_FILES_PER_PROCESS, $config1->getFilesPerProcess());
  68. self::assertSame(300, $config1->getProcessTimeout());
  69. // Flipped order of arguments using named arguments syntax
  70. $config2 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], [
  71. 'processTimeout' => 300,
  72. 'filesPerProcess' => 5,
  73. ]);
  74. self::assertSame(5, $config2->getFilesPerProcess());
  75. self::assertSame(300, $config2->getProcessTimeout());
  76. // Only first argument provided, but via named argument
  77. $config3 = \call_user_func_array([ParallelConfigFactory::class, 'detect'], ['filesPerProcess' => 7]);
  78. self::assertSame(7, $config3->getFilesPerProcess());
  79. self::assertSame(ParallelConfig::DEFAULT_PROCESS_TIMEOUT, $config3->getProcessTimeout());
  80. }
  81. }