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

Commit

Permalink
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public function readChar($mask = null)
$f = fopen('php://stdin','r');
do {
$char = fread($f,1);
} while ($mask !== null && "" !== $char && false === stristr($mask, $char));
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));
fclose($f);
return $char;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Posix.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function readChar($mask = null)
$stream = fopen('php://stdin', 'rb');
do {
$char = fgetc($stream);
} while (strlen($char) !== 1 || ($mask !== null && stristr($mask, $char) === false));
} while (strlen($char) !== 1 || ($mask !== null && false === strstr($mask, $char)));
fclose($stream);

$this->restoreTTYMode();
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function readChar($mask = null)

// Fetch the char from mask
$char = substr($mask, $return - 1, 1);
} while (!$char || ($mask !== null && !stristr($mask, $char)));
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));

return $char;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/WindowsAnsicon.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function readChar($mask = null)

// Fetch the char from mask
$char = substr($mask, $return - 1, 1);
} while (!$char || ($mask !== null && !stristr($mask, $char)));
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));

return $char;
}
Expand Down
35 changes: 10 additions & 25 deletions src/Prompt/Char.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,18 @@ public function show()
$mask = implode("", $mask); // convert back to string
}

do {
/**
* Read char from console
*/
$char = $this->getConsole()->readChar($mask);

/**
* Lowercase the response if case is irrelevant
*/
if ($this->ignoreCase) {
$char = strtolower($char);
}
/**
* Read char from console
*/
$char = $this->getConsole()->readChar($mask);

/**
* Check if it is an allowed char
*/
if (stristr($this->allowedChars, $char) !== false) {
if ($this->echo) {
echo trim($char)."\n";
} else {
if ($this->promptText) {
echo "\n"; // skip to next line but only if we had any prompt text
}
}
break;
if ($this->echo) {
echo trim($char)."\n";
} else {
if ($this->promptText) {
echo "\n"; // skip to next line but only if we had any prompt text
}
} while (true);
}

return $this->lastResponse = $char;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Prompt/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ public function __construct(
*/
public function show()
{
$response = parent::show() === $this->yesChar;
$char = parent::show();
if($this->ignoreCase) {
$response = strtolower($char) === strtolower($this->yesChar);
} else {
$response = $char === $this->yesChar;
}
return $this->lastResponse = $response;
}

Expand Down
10 changes: 1 addition & 9 deletions test/Adapater/AbstractAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ public function testReadCharWithMaskInsensitiveCase()
fwrite($this->adapter->stream, 'bAr');

$char = $this->adapter->readChar('ar');
$this->assertEquals($char, 'A');
}

public function testReadCharWithNoReturn()
{
fwrite($this->adapter->stream, 'bar');

$char = $this->adapter->readChar('foo');
$this->assertEquals($char, '');
$this->assertEquals($char, 'r');
}
}
31 changes: 29 additions & 2 deletions test/Prompt/CharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public function testCanPromptChar()

public function testCanPromptCharWithCharNotInDefaultMask()
{
fwrite($this->adapter->stream, 'zywa');
fwrite($this->adapter->stream, '*zywa');

$char = new Char();
$char->setEcho(false);
$char->setConsole($this->adapter);
ob_start();
$response = $char->show();
$text = ob_get_clean();
ob_get_clean();
$this->assertEquals('z', $response);
}

Expand All @@ -73,4 +73,31 @@ public function testCanPromptCharWithNewQuestionAndMask()
$this->assertEquals($text, "Give a number\n");
$this->assertEquals('1', $response);
}

public function testCanPromptCharWithIgnoreCaseByDefault()
{
fwrite($this->adapter->stream, 'FOObar');

$char = new Char();
$char->setEcho(false);
$char->setConsole($this->adapter);
ob_start();
$response = $char->show();
ob_get_clean();
$this->assertEquals('F', $response);
}

public function testCanPromptCharWithoutIgnoreCase()
{
fwrite($this->adapter->stream, 'FOObar');

$char = new Char();
$char->setEcho(false);
$char->setConsole($this->adapter);
$char->setIgnoreCase(false);
ob_start();
$response = $char->show();
ob_get_clean();
$this->assertEquals('b', $response);
}
}
108 changes: 108 additions & 0 deletions test/Prompt/ConfirmTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Console\Char;

use Zend\Console\Prompt\Confirm;
use ZendTest\Console\TestAssets\ConsoleAdapter;

/**
* @group Zend_Console
*/
class ConfirmTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ConsoleAdapter
*/
protected $adapter;

public function setUp()
{
$this->adapter = new ConsoleAdapter();
$this->adapter->stream = fopen('php://memory', 'w+');
}

public function tearDown()
{
fclose($this->adapter->stream);
}

public function testCanPromptConfirm()
{
fwrite($this->adapter->stream, 'y');

$confirm = new Confirm("Is ZF2 the best framework ?");
$confirm->setEcho(false);
$confirm->setConsole($this->adapter);
ob_start();
$response = $confirm->show();
$text = ob_get_clean();
$this->assertEquals($text, "Is ZF2 the best framework ?\n");
$this->assertTrue($response);
}

public function testCanPromptConfirmWithDefaultIgnoreCase()
{
fwrite($this->adapter->stream, 'Y');

$confirm = new Confirm("Is ZF2 the best framework ?");
$confirm->setEcho(false);
$confirm->setConsole($this->adapter);
ob_start();
$response = $confirm->show();
$text = ob_get_clean();
$this->assertEquals($text, "Is ZF2 the best framework ?\n");
$this->assertTrue($response);
}

public function testCanPromptConfirmWithoutIgnoreCase()
{
fwrite($this->adapter->stream, 'Yn');

$confirm = new Confirm("Is ZF2 the best framework ?");
$confirm->setEcho(false);
$confirm->setConsole($this->adapter);
$confirm->setIgnoreCase(false);
ob_start();
$response = $confirm->show();
$text = ob_get_clean();
$this->assertEquals($text, "Is ZF2 the best framework ?\n");
$this->assertFalse($response);
}

public function testCanPromptConfirmWithYesNoCharChanged()
{
fwrite($this->adapter->stream, 'on0');

$confirm = new Confirm("Is ZF2 the best framework ?", "1", "0");
$confirm->setEcho(false);
$confirm->setConsole($this->adapter);
ob_start();
$response = $confirm->show();
$text = ob_get_clean();
$this->assertEquals($text, "Is ZF2 the best framework ?\n");
$this->assertFalse($response);
}

public function testCanPromptConfirmWithYesNoCharChangedWithSetter()
{
fwrite($this->adapter->stream, 'oaB');

$confirm = new Confirm("Is ZF2 the best framework ?", "1", "0");
$confirm->setYesChar("A");
$confirm->setNoChar("B");
$confirm->setEcho(false);
$confirm->setConsole($this->adapter);
ob_start();
$response = $confirm->show();
$text = ob_get_clean();
$this->assertEquals($text, "Is ZF2 the best framework ?\n");
$this->assertTrue($response);
}
}
2 changes: 1 addition & 1 deletion test/TestAssets/ConsoleAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function readChar($mask = null)
}
do {
$char = fread($this->stream, 1);
} while ($mask !== null && "" !== $char && false === stristr($mask, $char));
} while ("" === $char || ($mask !== null && false === strstr($mask, $char)));
return $char;
}
}

0 comments on commit ca3399f

Please sign in to comment.