PhpSubprocessTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Process\PhpExecutableFinder;
  13. use Symfony\Component\Process\Process;
  14. class PhpSubprocessTest extends TestCase
  15. {
  16. private static $phpBin;
  17. public static function setUpBeforeClass(): void
  18. {
  19. $phpBin = new PhpExecutableFinder();
  20. self::$phpBin = getenv('SYMFONY_PROCESS_PHP_TEST_BINARY') ?: ('phpdbg' === \PHP_SAPI ? 'php' : $phpBin->find());
  21. }
  22. /**
  23. * @dataProvider subprocessProvider
  24. */
  25. public function testSubprocess(string $processClass, string $memoryLimit, string $expectedMemoryLimit)
  26. {
  27. $process = new Process([self::$phpBin,
  28. '-d',
  29. 'memory_limit='.$memoryLimit,
  30. __DIR__.'/OutputMemoryLimitProcess.php',
  31. '-e', self::$phpBin,
  32. '-p', $processClass,
  33. ]);
  34. $process->mustRun();
  35. $this->assertEquals($expectedMemoryLimit, trim($process->getOutput()));
  36. }
  37. public static function subprocessProvider(): \Generator
  38. {
  39. yield 'Process does ignore dynamic memory_limit' => [
  40. 'Process',
  41. self::getRandomMemoryLimit(),
  42. self::getDefaultMemoryLimit(),
  43. ];
  44. yield 'PhpSubprocess does not ignore dynamic memory_limit' => [
  45. 'PhpSubprocess',
  46. self::getRandomMemoryLimit(),
  47. self::getRandomMemoryLimit(),
  48. ];
  49. }
  50. private static function getDefaultMemoryLimit(): string
  51. {
  52. return trim(ini_get_all()['memory_limit']['global_value']);
  53. }
  54. private static function getRandomMemoryLimit(): string
  55. {
  56. $memoryLimit = 123; // Take something that's really unlikely to be configured on a user system.
  57. while (($formatted = $memoryLimit.'M') === self::getDefaultMemoryLimit()) {
  58. ++$memoryLimit;
  59. }
  60. return $formatted;
  61. }
  62. }