Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/zendframework/zendframework#6646-console-passwo…
Browse files Browse the repository at this point in the history
…rd-prompt' into develop

Close zendframework/zendframework#6646
  • Loading branch information
Ocramius committed Jan 3, 2015
161 parents 9d3f144 + c7e321d + d776971 + df35d8a + c0d4707 + fb9dce0 + c45b896 + 739e942 + 29a4517 + 3179585 + ae205d0 + 6d3ab5a + 5b7a64f + 7591112 + 7610b9a + f91c471 + acef74d + d753e94 + 7a829bf + 3b93376 + 6c2eca4 + 48bc01e + 3c88cd5 + fbfc750 + 441b9de + b57e2b9 + 2beef92 + 3ae4add + 994832b + 6db3cde + e0f83ee + 712223d + 3b4ec35 + e53a267 + 51bf3c7 + 10a1e95 + 30ee303 + 96417b9 + 50ea13f + 24d1c0c + b09295e + 13758aa + 99316f8 + 40b8ac3 + e284e0b + 8ff51b4 + 4b5ea1e + 5b24371 + ed932b6 + 357852d + 262e5d6 + 9057a3f + 0e1e846 + 5d11876 + 0ae0984 + 6ff135c + 0b45aba + 1778b0c + a574713 + ba3f132 + 56365a6 + 019f89a + b09b3ba + 2af32d0 + cb39fc7 + 3f8a6fd + 8999753 + a7cd86b + da1c469 + 5371cdb + fd9f625 + 99d3fcd + 1e3ed17 + a360459 + 1a1e9aa + 4c8955c + 94f2feb + 5b450ef + e64e5b0 + e9d19a6 + a5a9b6e + 5e59c3a + a47f2db + 3cf915a + a2a5c91 + 1cd3439 + ef9c643 + 298a619 + 001ae99 + c37ab8e + 017f6f1 + 0711f77 + 38af804 + 23120b4 + 5f96f53 + a257169 + 8085bc4 + 3110cd4 + f270d41 + 9536f09 + 4ec1292 + adcdd78 + e80fff6 + 99fc5ce + 1d23e94 + fa167cb + a93e92f + 13a2df8 + 06689fb + 2d601eb + 72a3295 + a59f42c + 0f4489d + b3f7029 + 3c492ce + 259bcdc + a811ae5 + f66f985 + 173e949 + b93077e + f11a10d + 5ac4be0 + cb31aff + d34bdbb + 725b978 + 27b423e + 624ac5e + a3340ac + 80ab2e3 + 3429053 + 998a6d7 + 8b9ab71 + d4dffb0 + 5456435 + 97180fa + 126be7f + c16eb72 + 8ccb096 + 50481b9 + 411738a + 50b5c0b + 0721376 + ca3399f + 5abf474 + 7783b57 + a53deb5 + c8885be + 1adf3e3 + 5a45e41 + 3370b44 + b4caa32 + 2a7fc79 + 755e064 + fde42e0 + fb9bce8 + 3ff447c + 7cbed25 + 61aff3c + a27b9cd + bcf0eb7 + a918fa9 commit dfbe5c2
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/Prompt/Password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Console\Prompt;

final class Password extends AbstractPrompt
{
/**
* @var string
*/
private $promptText;

/**
* @var bool
*/
private $echo;

/**
* Ask the user for a password
*
* @param string $promptText The prompt text to display in console
* @param bool $echo Display the selection after user presses key
*/
public function __construct($promptText = 'Password: ', $echo = false)
{
$this->promptText = (string) $promptText;
$this->echo = (bool) $echo;
}

/**
* Show the prompt to user and return a string.
*
* @return string
*/
public function show()
{
$console = $this->getConsole();

$console->writeLine($this->promptText);

$password = '';

/**
* Read characters from console
*/
while (true) {
$char = $console->readChar();

$console->clearLine();

if (PHP_EOL == $char) {
break;
}

$password .= $char;

if ($this->echo) {
$console->write(str_repeat('*', strlen($password)));
}
}

return $password;
}
}
97 changes: 97 additions & 0 deletions test/Prompt/PasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Console\Prompt;

use Zend\Console\Prompt\Password;

/**
* Tests for {@see \Zend\Console\Prompt\Password}
*
* @covers \Zend\Console\Prompt\Password
*/
class PasswordTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Zend\Console\Adapter\AbstractAdapter|\PHPUnit_Framework_MockObject_MockObject
*/
protected $adapter;

public function setUp()
{
$this->adapter = $this->getMock(
'Zend\Console\Adapter\AbstractAdapter',
array('write', 'writeLine', 'readChar')
);
}

public function testCanPromptPassword()
{
$this->adapter->expects($this->at(0))->method('writeLine')->with('Password: ');
$this->adapter->expects($this->at(1))->method('readChar')->will($this->returnValue('f'));
$this->adapter->expects($this->at(2))->method('clearLine');
$this->adapter->expects($this->at(3))->method('readChar')->will($this->returnValue('o'));
$this->adapter->expects($this->at(4))->method('clearLine');
$this->adapter->expects($this->at(5))->method('readChar')->will($this->returnValue('o'));
$this->adapter->expects($this->at(6))->method('clearLine');
$this->adapter->expects($this->at(7))->method('readChar')->will($this->returnValue(PHP_EOL));
$this->adapter->expects($this->never())->method('write');

$char = new Password('Password: ');

$char->setConsole($this->adapter);

$this->assertEquals('foo', $char->show());
}

public function testCanPromptPasswordRepeatedly()
{
$this->adapter->expects($this->at(0))->method('writeLine')->with('New password? ');
$this->adapter->expects($this->at(1))->method('readChar')->will($this->returnValue('b'));
$this->adapter->expects($this->at(2))->method('clearLine');
$this->adapter->expects($this->at(3))->method('readChar')->will($this->returnValue('a'));
$this->adapter->expects($this->at(4))->method('clearLine');
$this->adapter->expects($this->at(5))->method('readChar')->will($this->returnValue('r'));
$this->adapter->expects($this->at(6))->method('clearLine');
$this->adapter->expects($this->at(7))->method('readChar')->will($this->returnValue(PHP_EOL));
$this->adapter->expects($this->at(8))->method('writeLine')->with('New password? ');
$this->adapter->expects($this->at(9))->method('readChar')->will($this->returnValue('b'));
$this->adapter->expects($this->at(10))->method('clearLine');
$this->adapter->expects($this->at(11))->method('readChar')->will($this->returnValue('a'));
$this->adapter->expects($this->at(12))->method('clearLine');
$this->adapter->expects($this->at(13))->method('readChar')->will($this->returnValue('z'));
$this->adapter->expects($this->at(14))->method('clearLine');
$this->adapter->expects($this->at(15))->method('readChar')->will($this->returnValue(PHP_EOL));
$this->adapter->expects($this->never())->method('write');

$char = new Password('New password? ');

$char->setConsole($this->adapter);

$this->assertEquals('bar', $char->show());
$this->assertEquals('baz', $char->show());
}

public function testProducesStarSymbolOnInput()
{
$this->adapter->expects($this->at(1))->method('readChar')->will($this->returnValue('t'));
$this->adapter->expects($this->at(2))->method('write')->with('*');
$this->adapter->expects($this->at(3))->method('readChar')->will($this->returnValue('a'));
$this->adapter->expects($this->at(4))->method('write')->with('**');
$this->adapter->expects($this->at(5))->method('readChar')->will($this->returnValue('b'));
$this->adapter->expects($this->at(6))->method('write')->with('***');
$this->adapter->expects($this->at(7))->method('readChar')->will($this->returnValue(PHP_EOL));

$char = new Password('New password? ', true);

$char->setConsole($this->adapter);

$this->assertSame('tab', $char->show());
}
}

0 comments on commit dfbe5c2

Please sign in to comment.