/** * Create a Command object that will execute the SSH command * @param string $remoteCommand Command to run on remote server * @return Shell\Command Command instance configured to establish SSH * connection and execute the remote command */ public function createShellCommand($remoteCommand) { $sshCommandStem = 'ssh %s -q -p %s'; $args = array($this->host, $this->port); foreach ($this->options as $option) { $sshCommandStem .= ' -o %s'; $args[] = $option; } if ($this->user) { $sshCommandStem .= ' -l %s'; $args[] = $this->user; } if ($this->keyFile) { $sshCommandStem .= ' -i %s'; $args[] = $this->keyFile; } $sshCommandStem .= ' %s'; $args[] = $remoteCommand; // Put all the args into one array array_unshift($args, $sshCommandStem); /** @var \Bart\Shell $shell */ $shell = Diesel::create('Bart\\Shell'); /** @var \Bart\Shell\Command $cmd */ return call_user_func_array(array($shell, 'command'), $args); }
/** * Shmock the class {@see self::shmock()} and then register that shmock * to be returned by @see \Bart\Diesel * * @param string $className FQCN to shmock and reigster with Diesel * @param callable $configureShmock Closure that will configure all expectations on \Shmock\PHPUnitSpec * @param boolean $noConstructor Disable original constructor? * @return mixed The shmocked class */ public function shmockAndDieselify($className, $configureShmock, $noConstructor = false) { $shmock = $this->shmock($className, $configureShmock, $noConstructor); Diesel::registerInstantiator($className, function () use($shmock) { return $shmock; }); return $shmock; }
public function __construct(array $config) { $this->host = $config['server']; $this->port = $config['port']; $this->timeout = $config['timeout']; $this->auth_dn = $config['binduser']; $this->auth_pwd = $config['bindpw']; $this->basedn = $config['basedn']; $this->logger = \Logger::getLogger(__CLASS__); $this->phpldap = Diesel::create('Bart\\PHPLDAP'); }
public function testGetNewCommand() { Diesel::registerInstantiator('Bart\\Shell', function () { // Just let Shell do its magic return new Shell(); }); $host = 'www.example.com'; $ssh = new SshWrapper($host); $command = $ssh->createShellCommand('who -r'); $expectedSshCommand = "ssh 'www.example.com' -q -p '22' -o 'UserKnownHostsFile=/dev/null' -o 'StrictHostKeyChecking=no' 'who -r'"; $this->assertEquals($expectedSshCommand, "{$command}", 'the command string'); }
public function testFailedSearch() { if ($this->skipIfNoLdap()) { return; } $mock = $this->getMock('Bart\\PHPLDAP'); $mock->expects($this->exactly(2))->method('ldap_bind')->will($this->returnValueMap(array(array('conn', 'binduser', 'bindpw', true), array('conn', $this->brayDN, 'jbraynardpwd', false)))); $this->stubSearchSequence($mock); Diesel::registerInstantiator('Bart\\PHPLDAP', function () use($mock) { return $mock; }); $ldap = new Ldap($this->config); $ldap->connect(); $this->assertThrows('\\Bart\\LdapException', "LDAP Auth: failure, username/password did not match for {$this->brayDN}", function () use($mock, $ldap) { $ldap->auth_user('jbraynard', 'jbraynardpwd'); }); }
public function test_get_current_user() { $server = 'server'; $cmd = 'whoami'; $confStub = array('connection' => array('user' => 'jbraynard', 'key_file_location' => 'path/to/private_key')); // =( ...until there's a better way to match commands to results $sshCmd = "ssh -q -p 22 -i {$confStub['connection']['key_file_location']}" . " -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" . " {$confStub['connection']['user']}@{$server} {$cmd} 2>&1"; $mockShell = new Stub\MockShell($this); $mockShell->expectExec($sshCmd, array('Anything'), 0, 0); $mockConfig = $this->getMock('\\Bart\\Config_Parser', array(), array(), '', false); $mockConfig->expects($this->once())->method('parse_conf_file')->will($this->returnValue($confStub)); Diesel::registerInstantiator('Bart\\Shell', function () use($mockShell) { return $mockShell; }); Diesel::registerInstantiator('Bart\\Config_Parser', function () use($mockConfig) { return $mockConfig; }); $ssh = new Ssh($server); $ssh->use_auto_user(); $result = $ssh->execute($cmd); $this->assertEquals(0, $result['exit_status'], 'Wrong exit_status'); $this->assertEquals(array('Anything'), $result['output'], 'Wrong output'); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Diesel::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function testSingleton_WithArgs() { $this->assertThrows('\\Bart\\DieselException', 'Diesel::singleton only accepts no-argument classes', function () { Diesel::singleton('ignored', 'some argument'); }); }
private function setupMockCurl($testUrl, $testPort) { $mockCurl = $this->getMock('\\Bart\\Curl', array(), array($testUrl, $testPort)); $phpu = $this; Diesel::registerInstantiator('\\Bart\\Curl', function ($urlParam, $portParam) use($phpu, $mockCurl, $testUrl, $testPort) { $phpu->assertEquals($testUrl, $urlParam, 'url'); $phpu->assertEquals($testPort, $portParam, 'port'); return $mockCurl; }); return $mockCurl; }
/** * @return \Bart\Curl Create and configure a new Curl instance */ protected function initCurl() { // Set default port and update if specified $port = substr($this->baseUri, 0, 5) == 'https' ? 443 : 80; if (preg_match('#http[s]?://[^:]+:(\\d+)($|/)#', $this->baseUri, $matches)) { $port = $matches[1]; } // Bart\Curl does no validation of URLS and simply concats hostURI and path // so we will just pass fully validated URI with request /** @var \Bart\Curl curler */ $curler = Diesel::create('\\Bart\\Curl', $this->baseUri, $port); if ($this->authMethod !== null) { $curler->setAuth($this->username, $this->password, $this->authMethod); } $curlOpts = array(CURLOPT_TIMEOUT => $this->timeout, CURLOPT_HEADER => true); if (!$this->sslPeerVerification) { $curlOpts[CURLOPT_SSL_VERIFYPEER] = false; } $curler->setPhpCurlOpts($curlOpts); return $curler; }