|
| 1 | +<?php |
| 2 | + |
| 3 | +use Clue\React\SshProxy\SshProcessConnector; |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | +use React\EventLoop\Factory; |
| 6 | +use React\Socket\ConnectionInterface; |
| 7 | + |
| 8 | +class IntegrationSshProcessConnectorTest extends TestCase |
| 9 | +{ |
| 10 | + public function testConnectWillResolveWithConnectionInterfaceWhenProcessOutputsChannelOpenConfirmMessage() |
| 11 | + { |
| 12 | + $loop = Factory::create(); |
| 13 | + $connector = new SshProcessConnector('host', $loop); |
| 14 | + |
| 15 | + $ref = new ReflectionProperty($connector, 'cmd'); |
| 16 | + $ref->setAccessible(true); |
| 17 | + $ref->setValue($connector, 'echo "debug2: channel 0: open confirm rwindow 2097152 rmax 32768" >&2; #'); |
| 18 | + |
| 19 | + $promise = $connector->connect('example.com:80'); |
| 20 | + $promise->then($this->expectCallableOnceWith($this->isInstanceOf('React\Socket\ConnectionInterface'))); |
| 21 | + |
| 22 | + $loop->run(); |
| 23 | + } |
| 24 | + |
| 25 | + public function testConnectWillRejectWithExceptionWhenProcessOutputsChannelOpenFailedMessage() |
| 26 | + { |
| 27 | + $loop = Factory::create(); |
| 28 | + $connector = new SshProcessConnector('host', $loop); |
| 29 | + |
| 30 | + $ref = new ReflectionProperty($connector, 'cmd'); |
| 31 | + $ref->setAccessible(true); |
| 32 | + $ref->setValue($connector, 'echo "channel 0: open failed: administratively prohibited: open failed" >&2; #'); |
| 33 | + |
| 34 | + $promise = $connector->connect('example.com:80'); |
| 35 | + $promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException'))); |
| 36 | + |
| 37 | + $loop->run(); |
| 38 | + } |
| 39 | + |
| 40 | + public function testConnectWillRejectWithExceptionWhenProcessOutputsEndsWithoutChannelMessage() |
| 41 | + { |
| 42 | + $loop = Factory::create(); |
| 43 | + $connector = new SshProcessConnector('host', $loop); |
| 44 | + |
| 45 | + $ref = new ReflectionProperty($connector, 'cmd'); |
| 46 | + $ref->setAccessible(true); |
| 47 | + $ref->setValue($connector, 'echo foo >&2; #'); |
| 48 | + |
| 49 | + $promise = $connector->connect('example.com:80'); |
| 50 | + $promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException'))); |
| 51 | + |
| 52 | + $loop->run(); |
| 53 | + } |
| 54 | + |
| 55 | + public function testConnectWillResolveWithConnectionThatWillEmitImmediateDataFromProcessStdoutAfterChannelOpenConfirmMessage() |
| 56 | + { |
| 57 | + $loop = Factory::create(); |
| 58 | + $connector = new SshProcessConnector('host', $loop); |
| 59 | + |
| 60 | + $ref = new ReflectionProperty($connector, 'cmd'); |
| 61 | + $ref->setAccessible(true); |
| 62 | + $ref->setValue($connector, 'echo "debug2: channel 0: open confirm rwindow 2097152 rmax 32768" >&2; echo foo #'); |
| 63 | + |
| 64 | + $promise = $connector->connect('example.com:80'); |
| 65 | + |
| 66 | + $data = $this->expectCallableOnceWith("foo\n"); |
| 67 | + $promise->then(function (ConnectionInterface $connection) use ($data) { |
| 68 | + $connection->on('data', $data); |
| 69 | + }); |
| 70 | + |
| 71 | + $loop->run(); |
| 72 | + } |
| 73 | + |
| 74 | + protected function expectCallableOnceWith($value) |
| 75 | + { |
| 76 | + $mock = $this->createCallableMock(); |
| 77 | + |
| 78 | + $mock |
| 79 | + ->expects($this->once()) |
| 80 | + ->method('__invoke') |
| 81 | + ->with($value); |
| 82 | + |
| 83 | + return $mock; |
| 84 | + } |
| 85 | + |
| 86 | + protected function createCallableMock() |
| 87 | + { |
| 88 | + return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock(); |
| 89 | + } |
| 90 | +} |
0 commit comments