Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Functional testing php-ssh2 commands with PHPUnit and Vagrant
#!/bin/bash
# Get the Vagrant Keys
wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant
wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
# Fetch the VagrantFile
wget https://raw.github.com/gist/3612867/72a471a920fd6e08a89d96913a0ccbb4eb46fc8f/VagrantFile
# Fetch The Example test (write the tests for your work-flow
wget https://raw.github.com/gist/3612867/fa90ad49a26d300ec326af0ab2f84a8f96bebfe5/Ssh2Test.php
#initialize the vagrant image
vagrant init
# boot it and provision it
vagrant up
# run the phpunit test(s)
phpunit Ssh2Test
<?php
/**
*
*/
class Ssh2Test extends PHPUnit_Framework_TestCase
{
/**
* @var resource
*/
protected $_connection;
protected $_localPath = '/tmp/testFile.txt';
public function setup()
{
$this->_connection = ssh2_connect('localhost', 2222);
}
public function tearDown()
{
$this->_connection = null;
if(file_exists($this->_localPath)){
unlink($this->_localPath);
}
}
public function testPasswordAuth()
{
ssh2_auth_password($this->_connection, 'vagrant', 'vagrant');
$this->validateResult();
}
public function testPublicKeyAuth()
{
/*
* Adjust to the path you have placed the vagrant ssh keys
* These keys are available at:
* @link https://github.com/mitchellh/vagrant/tree/master/keys/
*/
$pathToKeys = __DIR__ . DIRECTORY_SEPARATOR;
ssh2_auth_pubkey_file(
$this->_connection, 'vagrant',
$pathToKeys. 'vagrant.pub' , $pathToKeys. 'vagrant'
);
$this->validateResult();
}
protected function validateResult()
{
$this->assertEquals(
'8838FB807067EFF260FA497D8D2D6398',
ssh2_fingerprint($this->_connection)
);
$sftp = ssh2_sftp($this->_connection);
file_put_contents(
"ssh2.sftp://$sftp//export/shared/testFile.txt",
$time = microtime()
);
$this->assertTrue(file_exists($this->_localPath));
$this->assertEquals($time, file_get_contents($this->_localPath));
}
}
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://ibpvagrant.s3.amazonaws.com/precise64.box"
# Provide the shared path to upload the file to:
config.vm.share_folder "v-data", "/export/shared", "/tmp/"
# Configure SSHD to allow the PasswordAuthentication
Vagrant::Config.run do |config|
config.vm.provision :shell, :inline => "sed -i 's/#PasswordAuthentication/PasswordAuthentication/g' /etc/ssh/sshd_config"
config.vm.provision :shell, :inline => "service ssh restart"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.