|
3 | 3 | namespace Terremoth\AsyncTest; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\Component\Process\Process as SymfonyProcess; |
6 | 7 | use Terremoth\Async\PhpFile; |
7 | 8 | use InvalidArgumentException; |
8 | 9 | use Exception; |
@@ -97,4 +98,76 @@ public function testRunDoesNotThrow(): void |
97 | 98 |
|
98 | 99 | $this->expectNotToPerformAssertions(); |
99 | 100 | } |
| 101 | + |
| 102 | + |
| 103 | + public function testRunExecutesWindowsFlowWhenOsIsWindows(): void |
| 104 | + { |
| 105 | + $tempFile = tempnam(sys_get_temp_dir(), 'test_win') . '.php'; |
| 106 | + file_put_contents($tempFile, '<?php echo "hello";'); |
| 107 | + |
| 108 | + try { |
| 109 | + $phpFile = $this->getMockBuilder(PhpFile::class) |
| 110 | + ->setConstructorArgs([$tempFile, ['arg1', 'arg2']]) |
| 111 | + ->onlyMethods(['getOsFamily', 'startProcess', 'executeCommand']) |
| 112 | + ->getMock(); |
| 113 | + |
| 114 | + $phpFile->expects($this->once()) |
| 115 | + ->method('getOsFamily') |
| 116 | + ->willReturn('Windows'); |
| 117 | + |
| 118 | + $phpFile->expects($this->once()) |
| 119 | + ->method('startProcess') |
| 120 | + ->willReturnCallback(function (SymfonyProcess $process) use ($tempFile) { |
| 121 | + $commandLine = $process->getCommandLine(); |
| 122 | + |
| 123 | + $this->assertStringContainsString('start', $commandLine); |
| 124 | + $this->assertStringContainsString('/B', $commandLine); |
| 125 | + $this->assertStringContainsString($tempFile, $commandLine); |
| 126 | + $this->assertStringContainsString('arg1', $commandLine); |
| 127 | + }); |
| 128 | + |
| 129 | + $phpFile->expects($this->never()) |
| 130 | + ->method('executeCommand'); |
| 131 | + |
| 132 | + $phpFile->run(); |
| 133 | + } finally { |
| 134 | + if (file_exists($tempFile)) { |
| 135 | + unlink($tempFile); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public function testRunExecutesUnixFlowWhenOsIsLinux(): void |
| 141 | + { |
| 142 | + $tempFile = tempnam(sys_get_temp_dir(), 'test_unix') . '.php'; |
| 143 | + file_put_contents($tempFile, '<?php echo "hello";'); |
| 144 | + |
| 145 | + try { |
| 146 | + $phpFile = $this->getMockBuilder(PhpFile::class) |
| 147 | + ->setConstructorArgs([$tempFile, ['argA']]) |
| 148 | + ->onlyMethods(['getOsFamily', 'startProcess', 'executeCommand']) |
| 149 | + ->getMock(); |
| 150 | + |
| 151 | + $phpFile->expects($this->once()) |
| 152 | + ->method('getOsFamily') |
| 153 | + ->willReturn('Linux'); |
| 154 | + |
| 155 | + $phpFile->expects($this->never()) |
| 156 | + ->method('startProcess'); |
| 157 | + |
| 158 | + $phpFile->expects($this->once()) |
| 159 | + ->method('executeCommand') |
| 160 | + ->willReturnCallback(function (string $command) use ($tempFile) { |
| 161 | + $this->assertStringContainsString('> /dev/null 2>&1 &', $command); |
| 162 | + $this->assertStringContainsString($tempFile, $command); |
| 163 | + $this->assertStringContainsString('argA', $command); |
| 164 | + }); |
| 165 | + |
| 166 | + $phpFile->run(); |
| 167 | + } finally { |
| 168 | + if (file_exists($tempFile)) { |
| 169 | + unlink($tempFile); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
100 | 173 | } |
0 commit comments