|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Symfony\Flex\Tests; |
| 6 | + |
| 7 | +use Composer\Composer; |
| 8 | +use Composer\IO\NullIO; |
| 9 | +use Composer\Util\ProcessExecutor; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\Process\PhpExecutableFinder; |
| 12 | +use Symfony\Flex\Options; |
| 13 | +use Symfony\Flex\ScriptExecutor; |
| 14 | + |
| 15 | +final class ScriptExecutorTest extends TestCase |
| 16 | +{ |
| 17 | + /** @backupGlobals enabled */ |
| 18 | + public function testMemoryLimit(): void |
| 19 | + { |
| 20 | + // Arrange |
| 21 | + $command = './command.php'; |
| 22 | + $memoryLimit = '32M'; |
| 23 | + putenv("COMPOSER_MEMORY_LIMIT={$memoryLimit}"); |
| 24 | + $executorMock = $this->createMock(ProcessExecutor::class); |
| 25 | + $scriptExecutor = new ScriptExecutor( |
| 26 | + new Composer(), |
| 27 | + new NullIO(), |
| 28 | + new Options(), |
| 29 | + $executorMock, |
| 30 | + ); |
| 31 | + $expectedCommand = $this->buildExpectedCommand($command, $memoryLimit); |
| 32 | + |
| 33 | + // Expect |
| 34 | + $executorMock |
| 35 | + ->method('execute') |
| 36 | + ->with($expectedCommand) |
| 37 | + ; |
| 38 | + $this->expectNotToPerformAssertions(); |
| 39 | + |
| 40 | + // Act |
| 41 | + $scriptExecutor->execute( |
| 42 | + 'php-script', |
| 43 | + $command |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + private function buildExpectedCommand(string $command, ?string $memoryLimit = null): string |
| 48 | + { |
| 49 | + $phpFinder = new PhpExecutableFinder(); |
| 50 | + if (!$php = $phpFinder->find(false)) { |
| 51 | + throw new \RuntimeException('The PHP executable could not be found, add it to your PATH and try again.'); |
| 52 | + } |
| 53 | + |
| 54 | + $arguments = $phpFinder->findArguments(); |
| 55 | + $ini = php_ini_loaded_file(); |
| 56 | + $arguments[] = "--php-ini={$ini}"; |
| 57 | + |
| 58 | + if (null !== $memoryLimit) { |
| 59 | + $arguments[] = "-d memory_limit={$memoryLimit}"; |
| 60 | + } |
| 61 | + |
| 62 | + $phpArgs = implode(' ', array_map([ProcessExecutor::class, 'escape'], $arguments)); |
| 63 | + |
| 64 | + return ProcessExecutor::escape($php).($phpArgs ? ' '.$phpArgs : '').' '.$command; |
| 65 | + } |
| 66 | +} |
0 commit comments