|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Unit\lucatume\WPBrowser\Extension; |
| 5 | + |
| 6 | +use lucatume\WPBrowser\Extension\PidBasedController; |
| 7 | +use lucatume\WPBrowser\Exceptions\RuntimeException; |
| 8 | +use Symfony\Component\Process\Process; |
| 9 | + |
| 10 | +class PidBasedControllerTest extends \Codeception\Test\Unit |
| 11 | +{ |
| 12 | + public function test_isProcessRunning_on_posix():void{ |
| 13 | + $testClass = new class { |
| 14 | + use PidBasedController; |
| 15 | + |
| 16 | + public function openIsProcessRunning(string $pidFile):bool{ |
| 17 | + return $this->isProcessRunning($pidFile); |
| 18 | + } |
| 19 | + }; |
| 20 | + $hash = md5(microtime()); |
| 21 | + $pidFile = sys_get_temp_dir()."/test-{$hash}.pid"; |
| 22 | + $pid = posix_getpid(); |
| 23 | + if(!file_put_contents($pidFile,$pid)){ |
| 24 | + $this->fail('Could not write pid to file '.$pidFile); |
| 25 | + } |
| 26 | + |
| 27 | + $this->assertTrue($testClass->openIsProcessRunning($pidFile)); |
| 28 | + $this->assertFileExists($pidFile); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_isProcessRunning_returns_false_if_pid_file_not_exists():void{ |
| 32 | + $testClass = new class { |
| 33 | + use PidBasedController; |
| 34 | + |
| 35 | + public function openIsProcessRunning(string $pidFile):bool{ |
| 36 | + return $this->isProcessRunning($pidFile); |
| 37 | + } |
| 38 | + }; |
| 39 | + $pid = posix_getpid(); |
| 40 | + |
| 41 | + $this->assertFalse($testClass->openIsProcessRunning(__DIR__ .'/test.pid')); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_isProcessRunning_throws_if_pid_file_cannot_be_read():void{ |
| 45 | + $testClass = new class { |
| 46 | + use PidBasedController; |
| 47 | + |
| 48 | + public function openIsProcessRunning(string $pidFile):bool{ |
| 49 | + return $this->isProcessRunning($pidFile); |
| 50 | + } |
| 51 | + }; |
| 52 | + $pid = posix_getpid(); |
| 53 | + $hash = md5(microtime()); |
| 54 | + $pidFile = sys_get_temp_dir()."/test-{$hash}.pid"; |
| 55 | + $pid = posix_getpid(); |
| 56 | + if(!file_put_contents($pidFile,$pid)){ |
| 57 | + $this->fail('Could not write pid to file '.$pidFile); |
| 58 | + } |
| 59 | + // Change the file mode to not be readable by the current user. |
| 60 | + chmod($pidFile,0000); |
| 61 | + |
| 62 | + $this->assertFalse($testClass->openIsProcessRunning($pidFile)); |
| 63 | + } |
| 64 | + |
| 65 | + public function test_isProcessRunning_returns_false_if_process_is_not_running():void{ |
| 66 | + $testClass = new class { |
| 67 | + use PidBasedController; |
| 68 | + |
| 69 | + public function openIsProcessRunning(string $pidFile):bool{ |
| 70 | + return $this->isProcessRunning($pidFile); |
| 71 | + } |
| 72 | + }; |
| 73 | + $hash = md5(microtime()); |
| 74 | + $pidFile = sys_get_temp_dir()."/test-{$hash}.pid"; |
| 75 | + $process = new Process(['echo', '23']); |
| 76 | + $process->start(); |
| 77 | + $pid = $process->getPid(); |
| 78 | + $process->wait(); |
| 79 | + if(!file_put_contents($pidFile,$pid)){ |
| 80 | + $this->fail('Could not write pid to file '.$pidFile); |
| 81 | + } |
| 82 | + |
| 83 | + $this->assertFalse($testClass->openIsProcessRunning($pidFile)); |
| 84 | + $this->assertFileNotExists($pidFile); |
| 85 | + } |
| 86 | +} |
0 commit comments