SessionTest.php (5048B)
1 <?php 2 3 namespace Ssh; 4 5 /** 6 * @covers \Ssh\Session 7 */ 8 class SessionTest extends \PHPUnit_Framework_TestCase 9 { 10 protected $configuration; 11 12 public function setUp() 13 { 14 $this->configuration = $this->getMock('Ssh\Configuration', array('asArguments'), array('my-host')); 15 $this->configuration->expects($this->any()) 16 ->method('asArguments') 17 ->will($this->returnValue(array('my-host', 21, array(), array()))); 18 } 19 20 public function testAuthenticateOnResourceCreation() 21 { 22 $resource = tmpfile(); 23 24 $authentication = $this->getMock('Ssh\Authentication\Password', array(), array('John', 's3cr3t')); 25 $authentication->expects($this->once()) 26 ->method('authenticate') 27 ->with($this->equalTo($resource)) 28 ->will($this->returnValue(true)); 29 30 $session = $this->getMock('Ssh\Session', array('connect'), array($this->configuration, $authentication)); 31 $session->expects($this->once()) 32 ->method('connect') 33 ->will($this->returnValue($resource)); 34 35 $session->getResource(); 36 } 37 38 public function testAuthenticateOnAuthenticationDefinition() 39 { 40 $resource = tmpfile(); 41 42 $session = new Session($this->configuration); 43 44 $property = new \ReflectionProperty($session, 'resource'); 45 $property->setAccessible(true); 46 $property->setValue($session, $resource); 47 48 $authentication = $this->getMock('Ssh\Authentication\Password', array('authenticate'), array('John', 's3cr3t')); 49 $authentication->expects($this->once()) 50 ->method('authenticate') 51 ->with($this->equalTo($resource)) 52 ->will($this->returnValue(true)); 53 54 $session->setAuthentication($authentication); 55 } 56 57 public function testCreateResourceWillThrowAnExceptionOnConnectionFailure() 58 { 59 $session = $this->getMock('Ssh\Session', array('connect'), array($this->configuration)); 60 $session->expects($this->any()) 61 ->method('connect') 62 ->will($this->returnValue(false)); 63 64 $method = new \ReflectionMethod($session, 'createResource'); 65 $method->setAccessible(true); 66 67 $this->setExpectedException('RuntimeException'); 68 69 $method->invoke($session); 70 } 71 72 public function testCreateResourceWillThrowAnExceptionOnAuthenticationFailure() 73 { 74 $authentication = $this->getMock('Ssh\Authentication\Password', array('authenticate'), array('John', 's3cr3t')); 75 $authentication->expects($this->any()) 76 ->method('authenticate') 77 ->will($this->returnValue(false)); 78 79 $session = $this->getMock('Ssh\Session', array('connect'), array($this->configuration, $authentication)); 80 $session->expects($this->any()) 81 ->method('connect') 82 ->will($this->returnValue(true)); 83 84 $method = new \ReflectionMethod($session, 'createResource'); 85 $method->setAccessible(true); 86 87 $this->setExpectedException('RuntimeException'); 88 89 $method->invoke($session); 90 } 91 92 /** 93 * @expectedException \InvalidArgumentException 94 * @expectedExceptionMessage The subsystem 'does_not_exist' is not supported. 95 */ 96 public function testCreateInvalidSubsystem() 97 { 98 $session = new Session($this->configuration); 99 100 $session->getSubsystem('does_not_exist'); 101 } 102 103 public function testGetConfiguration() 104 { 105 $session = new Session($this->configuration); 106 107 $this->assertEquals($this->configuration, $session->getConfiguration()); 108 } 109 110 public function testGetSubsystemSftp() 111 { 112 $session = new Session($this->configuration); 113 114 $this->assertInstanceOf('\Ssh\Sftp', $session->getSftp()); 115 } 116 117 public function testGetSubsystemPublickey() 118 { 119 $session = new Session($this->configuration); 120 121 $this->assertInstanceOf('\Ssh\Publickey', $session->getPublickey()); 122 } 123 124 public function testGetSubsystemExec() 125 { 126 $session = new Session($this->configuration); 127 128 $this->assertInstanceOf('\Ssh\Exec', $session->getExec()); 129 } 130 131 /** 132 * @expectedException \RuntimeException 133 * @expectedExceptionMessage The authentication over the current SSH connection failed. 134 */ 135 public function testAuthenficationException() 136 { 137 // A Authentication that will always fail. 138 $authentication = $this->getMock('Ssh\Authentication\Password', array(), array('John', 's3cr3t')); 139 $authentication->expects($this->once()) 140 ->method('authenticate') 141 ->will($this->returnValue(false)); 142 143 $session = new Session($this->configuration); 144 145 // We need to inject a resource, to trigger the authentification. 146 $resource = tmpfile(); 147 $property = new \ReflectionProperty($session, 'resource'); 148 $property->setAccessible(true); 149 $property->setValue($session, $resource); 150 151 $session->setAuthentication($authentication); 152 } 153 }