diff --git a/src/Adapter/AbstractAdapter.php b/src/Adapter/AbstractAdapter.php index f684f30..8d33fb4 100644 --- a/src/Adapter/AbstractAdapter.php +++ b/src/Adapter/AbstractAdapter.php @@ -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; } diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index 0c314ad..3044a63 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -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(); diff --git a/src/Adapter/Windows.php b/src/Adapter/Windows.php index 85966c4..9ba1b9c 100644 --- a/src/Adapter/Windows.php +++ b/src/Adapter/Windows.php @@ -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; } diff --git a/src/Adapter/WindowsAnsicon.php b/src/Adapter/WindowsAnsicon.php index b487505..9621c82 100644 --- a/src/Adapter/WindowsAnsicon.php +++ b/src/Adapter/WindowsAnsicon.php @@ -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; } diff --git a/src/Prompt/Char.php b/src/Prompt/Char.php index 9fb7a69..648ff2c 100644 --- a/src/Prompt/Char.php +++ b/src/Prompt/Char.php @@ -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; } diff --git a/src/Prompt/Confirm.php b/src/Prompt/Confirm.php index 65a8a05..83f949a 100644 --- a/src/Prompt/Confirm.php +++ b/src/Prompt/Confirm.php @@ -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; } diff --git a/test/Adapater/AbstractAdapterTest.php b/test/Adapater/AbstractAdapterTest.php index 76d131a..21ba5ae 100644 --- a/test/Adapater/AbstractAdapterTest.php +++ b/test/Adapater/AbstractAdapterTest.php @@ -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'); } } diff --git a/test/Prompt/CharTest.php b/test/Prompt/CharTest.php index 77e5926..ffcac9d 100644 --- a/test/Prompt/CharTest.php +++ b/test/Prompt/CharTest.php @@ -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); } @@ -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); + } } diff --git a/test/Prompt/ConfirmTest.php b/test/Prompt/ConfirmTest.php new file mode 100644 index 0000000..b9afd05 --- /dev/null +++ b/test/Prompt/ConfirmTest.php @@ -0,0 +1,108 @@ +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); + } +} diff --git a/test/TestAssets/ConsoleAdapter.php b/test/TestAssets/ConsoleAdapter.php index 0fd8de0..66f7a56 100644 --- a/test/TestAssets/ConsoleAdapter.php +++ b/test/TestAssets/ConsoleAdapter.php @@ -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; } }