Add testing for PasswordGetter (#298, #299)

This commit is contained in:
Vitaliy Zhuk 2015-05-27 10:48:18 +03:00
parent bedb0904e2
commit ac5e4781e8
8 changed files with 352 additions and 19 deletions

View File

@ -1,5 +1,7 @@
<?php
/* (c) Anton Medvedev <anton@elfet.ru>
/**
* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.

View File

@ -1,11 +1,14 @@
<?php
/* (c) Anton Medvedev <anton@elfet.ru>
/**
* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Server;
use Deployer\Server\Password\PasswordGetterInterface;
/**
@ -205,7 +208,7 @@ class Configuration
/**
* Set password for connection
*
* @param string $password
* @param string|PasswordGetterInterface $password
*
* @return Configuration
*/

View File

@ -10,7 +10,6 @@
namespace Deployer\Server\Password;
use Deployer\Task\Context;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@ -52,7 +51,7 @@ class AskPasswordGetter implements PasswordGetterInterface
{
$askMessage = sprintf('[%s@%s] Password:', $user, $host);
$questionHelper = new QuestionHelper();
$questionHelper = $this->createQuestionHelper();
$question = new Question($askMessage);
$question->setHidden(true);
@ -76,4 +75,14 @@ class AskPasswordGetter implements PasswordGetterInterface
return $askPasswordGetter->getPassword($host, $user);
});
}
/**
* Create question helper
*
* @return QuestionHelper
*/
protected function createQuestionHelper()
{
return new QuestionHelper();
}
}

View File

@ -14,7 +14,7 @@ namespace Deployer\Server\Password;
*
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class SinglePasswordGetter implements PasswordGetterInterface
class SimplePasswordGetter implements PasswordGetterInterface
{
/**
* @var string

View File

@ -1,5 +1,7 @@
<?php
/* (c) Anton Medvedev <anton@elfet.ru>
/**
* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@ -7,107 +9,229 @@
namespace Deployer\Server;
/**
* Builder testing
*/
class BuilderTest extends \PHPUnit_Framework_TestCase
{
public function testPassword()
/**
* Test set user for connection
*/
public function testUser()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_PASSWORD)
->will($this->returnSelf());
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setUser')
->with('user')
->will($this->returnSelf());
$b = new Builder($config, $env);
$b->user('user');
}
/**
* Test set password with
*/
public function testPasswordSetScalar()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_PASSWORD)
->will($this->returnSelf());
$config->expects($this->once())
->method('setPassword')
->with('password')
->will($this->returnSelf());
$env = $this->getMock('Deployer\Server\Environment');
$b = new Builder($config, $env);
$b->user('user');
$b->password('password');
}
/**
* Test set password getter
*/
public function testPasswordSetGetter()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$password = $this->getMockForAbstractClass('Deployer\Server\Password\PasswordGetterInterface');
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_PASSWORD)
->will($this->returnSelf());
$config->expects($this->once())
->method('setPassword')
->with($password)
->will($this->returnSelf());
$b = new Builder($config, $env);
$b->password($password);
}
/**
* Test set invalid password getter
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The password should be a string or PasswordGetterInterface instances, but "stdClass" given.
*/
public function testPasswordWithInvalidObject()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$password = (object)[];
$b = new Builder($config, $env);
$b->password($password);
}
/**
* Test password with non scalar value
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The password should be a string or PasswordGetterInterface instances, but "array" given.
*/
public function testPasswordWithNonScalarValue()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$password = ['foo', 'bar'];
$b = new Builder($config, $env);
$b->password($password);
}
/**
* Test password with null value
*/
public function testPasswordWithNullValue()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_PASSWORD)
->will($this->returnSelf());
$config->expects($this->once())
->method('setPassword')
->with($this->isInstanceOf('Deployer\Server\Password\CallablePasswordGetter'))
->will($this->returnSelf());
$b = new Builder($config, $env);
$b->password();
}
/**
* Test set host and port
*/
public function testHostAndPort()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setHost')
->with('localhost')
->will($this->returnSelf());
$config->expects($this->once())
->method('setPort')
->with(22)
->will($this->returnSelf());
$env = $this->getMock('Deployer\Server\Environment');
$b = new Builder($config, $env);
$b->host('localhost');
$b->port(22);
}
/**
* Test set configuration file for connection
*/
public function testConfig()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_CONFIG)
->will($this->returnSelf());
$config->expects($this->once())
->method('setConfigFile')
->with('~/.config')
->will($this->returnSelf());
$env = $this->getMock('Deployer\Server\Environment');
$b = new Builder($config, $env);
$b->configFile('~/.config');
}
/**
* Test set pem file for connection
*/
public function testPem()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_PEM_FILE)
->will($this->returnSelf());
$config->expects($this->once())
->method('setPemFile')
->with('~/.pem')
->will($this->returnSelf());
$env = $this->getMock('Deployer\Server\Environment');
$b = new Builder($config, $env);
$b->pemFile('~/.pem');
}
/**
* Test set public key for connection
*/
public function testPublicKey()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_IDENTITY_FILE)
->will($this->returnSelf());
$config->expects($this->once())
->method('setPublicKey')
->with('~/.ssh/id_rsa.pub')
->will($this->returnSelf());
$config->expects($this->once())
->method('setPrivateKey')
->with('~/.ssh/id_rsa')
->will($this->returnSelf());
$config->expects($this->once())
->method('setPassPhrase')
->with('')
->will($this->returnSelf());
$env = $this->getMock('Deployer\Server\Environment');
$b = new Builder($config, $env);
$b->identityFile();
}
/**
* Test set environment variable
*/
public function testEnv()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
@ -117,9 +241,11 @@ class BuilderTest extends \PHPUnit_Framework_TestCase
$config
->method('getName')
->will($this->returnValue('test-name'));
$config
->method('getHost')
->will($this->returnValue('test-host'));
$config
->method('getPort')
->will($this->returnValue(22));
@ -144,6 +270,9 @@ class BuilderTest extends \PHPUnit_Framework_TestCase
$b->env('name', 'value');
}
/**
* Test use forward agent for connection
*/
public function testForwardAgent()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();

View File

@ -1,5 +1,7 @@
<?php
/* (c) Anton Medvedev <anton@elfet.ru>
/**
* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@ -7,8 +9,14 @@
namespace Deployer\Server;
/**
* Configuration testing
*/
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
/**
* Base test configuration
*/
public function testConfiguration()
{
$c = new Configuration('name', 'localhost', 80);
@ -28,4 +36,23 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals((isset($_SERVER['HOME']) ? $_SERVER['HOME'] : '~') . '/.pub', $c->setPublicKey('~/.pub')->getPublicKey());
$this->assertEquals((isset($_SERVER['HOME']) ? $_SERVER['HOME'] : '~') . '/.private', $c->setPrivateKey('~/.private')->getPrivateKey());
}
/**
* Test get password with use PasswordGetter system
*/
public function testGetPasswordWithUsePasswordGetter()
{
$configuration = new Configuration('name', 'localhost', 80);
$configuration->setUser('user');
$passwordGetter = $this->getMockForAbstractClass('Deployer\Server\Password\PasswordGetterInterface');
$passwordGetter->expects($this->once())->method('getPassword')
->with('localhost', 'user')
->will($this->returnValue('some-password'));
$configuration->setPassword($passwordGetter);
$password = $configuration->getPassword();
$this->assertEquals('some-password', $password);
}
}

View File

@ -0,0 +1,133 @@
<?php
/**
* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Server\Password;
use Deployer\Task\Context;
use Symfony\Component\Console\Question\Question;
/**
* Testing ask password getter
*
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class AskPasswordGetterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\Console\Input\InputInterface
*/
private $input;
/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
private $output;
/**
* @var AskPasswordGetter|\PHPUnit_Framework_MockObject_MockObject
*/
private $askPasswordGetter;
/**
* @var \Symfony\Component\Console\Helper\QuestionHelper|\PHPUnit_Framework_MockObject_MockObject
*/
private $questionHelper;
/**
* {@inheritDoc}
*/
public function setUp()
{
$this->input = $this->getMockForAbstractClass('Symfony\Component\Console\Input\InputInterface');
$this->output = $this->getMockForAbstractClass('Symfony\Component\Console\Output\OutputInterface');
$this->questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
$this->askPasswordGetter = $this->getMock(
'Deployer\Server\Password\AskPasswordGetter',
[ 'createQuestionHelper' ],
[ $this->input, $this->output ]
);
}
/**
* Test create question helper (use construct)
*/
public function testCreateWithConstruct()
{
new AskPasswordGetter($this->input, $this->output);
}
/**
* Test get password
*/
public function testGetPassword()
{
$this->askPasswordGetter->expects($this->once())->method('createQuestionHelper')
->will($this->returnValue($this->questionHelper));
$this->questionHelper->expects($this->once())->method('ask')
->with($this->input, $this->output, $this->isInstanceOf('Symfony\Component\Console\Question\Question'))
->will($this->returnCallback(function ($input, $output, Question $question) {
// Check question
$this->assertTrue($question->isHidden(), 'The question must be hidden');
$this->assertEquals('[user@host] Password:', $question->getQuestion());
// Return password
return 'some_password';
}));
$realPassword = $this->askPasswordGetter->getPassword('host', 'user');
$this->assertEquals('some_password', $realPassword, 'Password not mismatch.');
}
/**
* Test create lazy ask password getter
*/
public function testCreateLazyAskPasswordGetter()
{
$lazyGetter = AskPasswordGetter::createLazyGetter();
$this->assertInstanceOf('Deployer\Server\Password\CallablePasswordGetter', $lazyGetter);
$context = $this->getMock(
'Deployer\Task\Context',
[ 'getInput', 'getOutput' ],
[],
'',
false
);
$context->expects($this->any())->method('getInput')
->will($this->returnValue($this->input));
$context->expects($this->any())->method('getOutput')
->will($this->returnValue($this->output));
Context::push($context);
$lazyGetter->getPassword('host', 'user');
}
/**
* Test create question helper.
* Attention: method is protected, then use Reflection for access to this method
*/
public function testCreateQuestionHelper()
{
$askPasswordGetter = new AskPasswordGetter($this->input, $this->output);
$ref = new \ReflectionObject($askPasswordGetter);
$method = $ref->getMethod('createQuestionHelper');
$method->setAccessible(true);
$questionHelper = $method->invoke($askPasswordGetter);
$this->assertInstanceOf('Symfony\Component\Console\Helper\QuestionHelper', $questionHelper);
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Server\Password;
/**
* Testing simple password getter
*
* @author Vitaliy Zhuk <zhuk2205@gmail.com>
*/
class SimplePasswordGetterTest extends \PHPUnit_Framework_TestCase
{
/**
* Base test
*/
public function testBase()
{
$getter = new SimplePasswordGetter('foo-bar');
$password = $getter->getPassword('host', 'user');
$this->assertEquals('foo-bar', $password, 'Password mismatch.');
}
}