php / pecl-networking-ssh2

Bindings for the libssh2 library
http://pecl.php.net/package/ssh2
Other
51 stars 61 forks source link

Add reflection API support. #3

Closed mageekguy closed 9 years ago

mageekguy commented 10 years ago

Currently, ssh2 function does not support reflection API. So, it's not possible to retrieve theirs parameters and it's a problem in unit tests (for example) because it's not possible to mock correctly these functions and assert on them efficiently. So, this PR add reflection API support, so it's now possible to do:

fch@Mageekbook:~/Pecl/Ssh2(master *)
26> php -a
Interactive shell

php > $f = new reflectionFunction('ssh2_exec');
php > var_dump($f->getParameters());
array(7) {
  [0]=>
  &object(ReflectionParameter)#2 (1) {
    ["name"]=>
    string(7) "session"
  }
  [1]=>
  &object(ReflectionParameter)#3 (1) {
    ["name"]=>
    string(7) "command"
  }
  [2]=>
  &object(ReflectionParameter)#4 (1) {
    ["name"]=>
    string(3) "pty"
  }
  [3]=>
  &object(ReflectionParameter)#5 (1) {
    ["name"]=>
    string(3) "env"
  }
  [4]=>
  &object(ReflectionParameter)#6 (1) {
    ["name"]=>
    string(5) "width"
  }
  [5]=>
  &object(ReflectionParameter)#7 (1) {
    ["name"]=>
    string(6) "height"
  }
  [6]=>
  &object(ReflectionParameter)#8 (1) {
    ["name"]=>
    string(17) "width_height_type"
  }
}
php > 

And with atoum, you can do:

public function testOpen()
{
     $this
        ->given($session = $this->getTestedClassInstance())

        ->if($this->function->ssh2_connect = false)
        ->then
            ->exception(function() use ($session) { $session->open(); })
                ->isInstanceOf('ssh\session\exception')
                ->hasMessage('Unable to open a SSH session on \'' . $session->getHost() . '\' on port \'' . $session->getPort() . '\'')
            ->function('ssh2_connect')->wasCalledWithArguments($session->getHost(), $session->getPort())->once()

        ->if(
            $this->function->ssh2_connect = $resource = uniqid(),
            $this->function->ssh2_auth_pubkey_file = false
        )
        ->then
            ->exception(function() use ($session) { $session->open(); })
                ->isInstanceOf('ssh\session\exception')
                ->hasMessage('Unable to authenticate user \'' . $session->getUsername() . '\' on host \'' . $session->getHost() . '\' on port \'' . $session->getPort() . '\'')
            ->function('ssh2_connect')
                ->wasCalledWithArguments($session->getHost(), $session->getPort())
                    ->before($this->function('ssh2_auth_pubkey_file')->wasCalledWithArguments($resource, $session->getUsername(), $session->getPublicKeyFile(), $session->getPrivateKeyFile())->once())
                        ->once()
     ;
}
langemeijer commented 9 years ago

Thank you for you contribution, sorry this took so long.

mageekguy commented 9 years ago

better late than never ;)