|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Nette\Utils\Helpers; |
| 6 | +use Nette\Utils\Process; |
| 7 | +use Nette\Utils\ProcessFailedException; |
| 8 | +use Nette\Utils\ProcessTimeoutException; |
| 9 | +use Tester\Assert; |
| 10 | + |
| 11 | +require __DIR__ . '/../bootstrap.php'; |
| 12 | + |
| 13 | + |
| 14 | +// Process execution - success |
| 15 | + |
| 16 | +test('run executable successfully', function () { |
| 17 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'echo "hello";']); |
| 18 | + Assert::true($process->isSuccess()); |
| 19 | + Assert::same(0, $process->getExitCode()); |
| 20 | + Assert::same('hello', $process->getStdOutput()); |
| 21 | + Assert::same('', $process->getStdError()); |
| 22 | +}); |
| 23 | + |
| 24 | +test('run command successfully', function () { |
| 25 | + $process = Process::runCommand('echo hello'); |
| 26 | + Assert::true($process->isSuccess()); |
| 27 | + Assert::same(0, $process->getExitCode()); |
| 28 | + Assert::same('hello' . PHP_EOL, $process->getStdOutput()); |
| 29 | + Assert::same('', $process->getStdError()); |
| 30 | +}); |
| 31 | + |
| 32 | + |
| 33 | +// Process execution - errors |
| 34 | + |
| 35 | +test('run executable with error', function () { |
| 36 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'exit(1);']); |
| 37 | + Assert::false($process->isSuccess()); |
| 38 | + Assert::same(1, $process->getExitCode()); |
| 39 | +}); |
| 40 | + |
| 41 | +test('run executable ensure success throws exception on error', function () { |
| 42 | + Assert::exception( |
| 43 | + fn() => Process::runExecutable(PHP_BINARY, ['-r', 'exit(1);'])->ensureSuccess(), |
| 44 | + ProcessFailedException::class, |
| 45 | + 'Process failed with non-zero exit code: 1', |
| 46 | + ); |
| 47 | +}); |
| 48 | + |
| 49 | +test('run command with error', function () { |
| 50 | + $process = Process::runCommand('"' . PHP_BINARY . '" -r "exit(1);"'); |
| 51 | + Assert::false($process->isSuccess()); |
| 52 | + Assert::same(1, $process->getExitCode()); |
| 53 | +}); |
| 54 | + |
| 55 | +test('run command ensure success throws exception on error', function () { |
| 56 | + Assert::exception( |
| 57 | + fn() => Process::runCommand('"' . PHP_BINARY . '" -r "exit(1);"')->ensureSuccess(), |
| 58 | + ProcessFailedException::class, |
| 59 | + 'Process failed with non-zero exit code: 1', |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | + |
| 64 | +// Process state monitoring |
| 65 | + |
| 66 | +test('is running', function () { |
| 67 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'sleep(1);']); |
| 68 | + Assert::true($process->isRunning()); |
| 69 | + $process->wait(); |
| 70 | + Assert::false($process->isRunning()); |
| 71 | +}); |
| 72 | + |
| 73 | +test('get pid', function () { |
| 74 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'sleep(1);']); |
| 75 | + Assert::type('int', $process->getPid()); |
| 76 | + $process->wait(); |
| 77 | + Assert::null($process->getPid()); |
| 78 | +}); |
| 79 | + |
| 80 | + |
| 81 | +// Waiting for process |
| 82 | + |
| 83 | +test('wait', function () { |
| 84 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'echo "hello";']); |
| 85 | + $process->wait(); |
| 86 | + $process->wait(); |
| 87 | + Assert::false($process->isRunning()); |
| 88 | + Assert::same(0, $process->getExitCode()); |
| 89 | + Assert::same('hello', $process->getStdOutput()); |
| 90 | +}); |
| 91 | + |
| 92 | +test('wait with callback', function () { |
| 93 | + $output = ''; |
| 94 | + $error = ''; |
| 95 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'echo "hello"; fwrite(STDERR, "error");']); |
| 96 | + $process->wait(function ($stdOut, $stdErr) use (&$output, &$error) { |
| 97 | + $output .= $stdOut; |
| 98 | + $error .= $stdErr; |
| 99 | + }); |
| 100 | + Assert::same('hello', $output); |
| 101 | + Assert::same('error', $error); |
| 102 | +}); |
| 103 | + |
| 104 | + |
| 105 | +// Automatically call wait() |
| 106 | + |
| 107 | +test('getStdOutput() automatically call wait()', function () { |
| 108 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'echo "hello";']); |
| 109 | + Assert::same('hello', $process->getStdOutput()); |
| 110 | + Assert::false($process->isRunning()); |
| 111 | +}); |
| 112 | + |
| 113 | +test('getExitCode() automatically call wait()', function () { |
| 114 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'echo exit(2);']); |
| 115 | + Assert::same(2, $process->getExitCode()); |
| 116 | + Assert::false($process->isRunning()); |
| 117 | +}); |
| 118 | + |
| 119 | + |
| 120 | +// Terminating process |
| 121 | + |
| 122 | +test('terminate', function () { |
| 123 | + $process = Process::runExecutable(PHP_BINARY, ['-r', 'sleep(5);']); |
| 124 | + $process->terminate(); |
| 125 | + Assert::false($process->isRunning()); |
| 126 | +}); |
| 127 | + |
| 128 | +test('terminate() and then wait()', function () { |
| 129 | + $process = Process::runExecutable(PHP_BINARY, ['-f', 'sleep(5);']); |
| 130 | + $process->terminate(); |
| 131 | + $process->wait(); |
| 132 | + Assert::false($process->isRunning()); |
| 133 | +}); |
| 134 | + |
| 135 | + |
| 136 | +// Timeout |
| 137 | + |
| 138 | +test('timeout', function () { |
| 139 | + Assert::exception( |
| 140 | + fn() => Process::runExecutable(PHP_BINARY, ['-r', 'sleep(5);'], timeout: 0.1)->wait(), |
| 141 | + ProcessTimeoutException::class, |
| 142 | + 'Process exceeded the time limit of 0.1 seconds', |
| 143 | + ); |
| 144 | +}); |
| 145 | + |
| 146 | + |
| 147 | +// bypass_shell |
| 148 | + |
| 149 | +if (Helpers::IsWindows) { |
| 150 | + test('bypass_shell = false', function () { |
| 151 | + $process = Process::runCommand('"' . PHP_BINARY . '" -r "echo 123;"', options: ['bypass_shell' => false]); |
| 152 | + Assert::same('123', $process->getStdOutput()); |
| 153 | + }); |
| 154 | +} |
0 commit comments