|
| 1 | +<?php |
| 2 | + |
| 3 | +use Clue\React\SshProxy\Io; |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | + |
| 6 | +class FunctionsTest extends TestCase |
| 7 | +{ |
| 8 | + public function testFdsReturnsArray() |
| 9 | + { |
| 10 | + $fds = Io\fds(); |
| 11 | + |
| 12 | + $this->assertInternalType('array', $fds); |
| 13 | + } |
| 14 | + |
| 15 | + public function testFdsReturnsArrayWithStdioHandles() |
| 16 | + { |
| 17 | + // skip when running with closed handles: vendor/bin/phpunit 0<&- |
| 18 | + if (!defined('STDIN') || !defined('STDOUT') || !defined('STDERR') || !@fstat(STDIN) || !@fstat(STDOUT) || !@fstat(STDERR)) { |
| 19 | + $this->markTestSkipped('Test suite does not appear to run with standard I/O handles'); |
| 20 | + } |
| 21 | + |
| 22 | + $fds = Io\fds(); |
| 23 | + |
| 24 | + $this->assertContains(0, $fds); |
| 25 | + $this->assertContains(1, $fds); |
| 26 | + $this->assertContains(2, $fds); |
| 27 | + } |
| 28 | + |
| 29 | + public function testFdsReturnsSameArrayTwice() |
| 30 | + { |
| 31 | + $fds = Io\fds(); |
| 32 | + $second = Io\fds(); |
| 33 | + |
| 34 | + $this->assertEquals($fds, $second); |
| 35 | + } |
| 36 | + |
| 37 | + public function testFdsWithInvalidPathReturnsArray() |
| 38 | + { |
| 39 | + $fds = Io\fds('/dev/null'); |
| 40 | + |
| 41 | + $this->assertInternalType('array', $fds); |
| 42 | + } |
| 43 | + |
| 44 | + public function testFdsWithInvalidPathReturnsSubsetOfFdsFromDevFd() |
| 45 | + { |
| 46 | + if (@scandir('/dev/fd') === false) { |
| 47 | + $this->markTestSkipped('Unable to read /dev/fd'); |
| 48 | + } |
| 49 | + |
| 50 | + $fds = Io\fds(); |
| 51 | + $second = Io\fds('/dev/null'); |
| 52 | + |
| 53 | + foreach ($second as $one) { |
| 54 | + $this->assertContains($one, $fds); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function testProcessWithoutFdsReturnsProcessWithoutClosingDefaultHandles() |
| 59 | + { |
| 60 | + $process = Io\processWithoutFds('sleep 10'); |
| 61 | + |
| 62 | + $this->assertInstanceOf('React\ChildProcess\Process', $process); |
| 63 | + |
| 64 | + $this->assertNotContains(' 0>&-', $process->getCommand()); |
| 65 | + $this->assertNotContains(' 1>&-', $process->getCommand()); |
| 66 | + $this->assertNotContains(' 2>&-', $process->getCommand()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testProcessWithoutFdsReturnsProcessWithOriginalCommandPartOfActualCommandWhenDescriptorsNeedToBeClosed() |
| 70 | + { |
| 71 | + // skip when running with closed handles: vendor/bin/phpunit 0<&- |
| 72 | + // bypass for example with dummy handles: vendor/bin/phpunit 8<&- |
| 73 | + $fds = Io\fds(); |
| 74 | + if (!$fds || max($fds) < 3) { |
| 75 | + $this->markTestSkipped('Did not detect additional file descriptors to be closed'); |
| 76 | + } |
| 77 | + |
| 78 | + $process = Io\processWithoutFds('sleep 10'); |
| 79 | + |
| 80 | + $this->assertInstanceOf('React\ChildProcess\Process', $process); |
| 81 | + |
| 82 | + $this->assertNotEquals('sleep 10', $process->getCommand()); |
| 83 | + $this->assertContains('sleep 10', $process->getCommand()); |
| 84 | + } |
| 85 | +} |
0 commit comments