diff --git a/composer.json b/composer.json index 7ca19ff..a94c5d0 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,8 @@ }, "extra": { "branch-alias": { - "dev-master": "2.4-dev", - "dev-develop": "2.5-dev" + "dev-master": "2.1-dev", + "dev-develop": "2.2-dev" } }, "autoload-dev": { diff --git a/src/Adapter/AbstractAdapter.php b/src/Adapter/AbstractAdapter.php index ea1ddb8..8d33fb4 100644 --- a/src/Adapter/AbstractAdapter.php +++ b/src/Adapter/AbstractAdapter.php @@ -12,6 +12,9 @@ use Zend\Console\Charset; use Zend\Console\Exception; +/** + * Common console adapter codebase + */ abstract class AbstractAdapter implements AdapterInterface { /** @@ -85,7 +88,7 @@ public function writeLine($text = "", $color = null, $bgColor = null) $text = trim($text, "\r\n"); // Replace newline characters with spaces - $test = str_replace("\n", " ", $text); + $text = str_replace("\n", " ", $text); // Trim the line if it's too long and output text $consoleWidth = $this->getWidth(); @@ -356,32 +359,6 @@ public function isUtf8() return true; } - /** - * Return current cursor position - array($x, $y) - * - * - * @return array array($x, $y); - */ - public function getPos() - { - } - -// /** -// * Return current cursor X coordinate (column) -// * -// * -// * @return false|int Integer or false if failed to determine. -// */ -// public function getX(); -// -// /** -// * Return current cursor Y coordinate (row) -// * -// * -// * @return false|int Integer or false if failed to determine. -// */ -// public function getY(); - /** * Set cursor position * @@ -416,22 +393,6 @@ public function getTitle() return ''; } - /** - * Set console window title - * - * @param $title - */ - public function setTitle($title) - { - } - - /** - * Reset console window title to previous value. - */ - public function resetTitle() - { - } - /** * Prepare a string that will be rendered in color. * @@ -601,7 +562,7 @@ public function readChar($mask = null) $f = fopen('php://stdin','r'); do { $char = fread($f,1); - } while ($mask === null || stristr($mask, $char)); + } while ("" === $char || ($mask !== null && false === strstr($mask, $char))); fclose($f); return $char; } diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index 2b9e320..eb3f8d3 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -143,28 +143,6 @@ public function getSize(); */ public function isUtf8(); - -// /** -// * Return current cursor position - array($x, $y) -// * -// * @return array array($x, $y); -// */ -// public function getPos(); -// -// /** -// * Return current cursor X coordinate (column) -// * -// * @return false|int Integer or false if failed to determine. -// */ -// public function getX(); -// -// /** -// * Return current cursor Y coordinate (row) -// * -// * @return false|int Integer or false if failed to determine. -// */ -// public function getY(); - /** * Set cursor position * @@ -190,19 +168,6 @@ public function showCursor(); */ public function getTitle(); - /** - * Set console window title - * - * @param $title - */ - public function setTitle($title); - - /** - * Reset console window title to previous value. - */ - public function resetTitle(); - - /** * Prepare a string that will be rendered in color. * diff --git a/src/Adapter/Posix.php b/src/Adapter/Posix.php index d3e6f5c..3044a63 100644 --- a/src/Adapter/Posix.php +++ b/src/Adapter/Posix.php @@ -258,15 +258,6 @@ public function resetColor() echo "\x1b[24;39m"; // reset fg underline } - /** - * Return current console window title. - * - * @return string - */ - public function getTitle() - { - } - /** * Set Console charset to use. * @@ -315,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/Virtual.php b/src/Adapter/Virtual.php index 3f25a1c..bd983cb 100644 --- a/src/Adapter/Virtual.php +++ b/src/Adapter/Virtual.php @@ -116,16 +116,6 @@ public function isUtf8() return false; } - /** - * Set cursor position - * - * @param int $x - * @param int $y - */ - public function setPos($x, $y) - { - } - /** * Return current console window title. * diff --git a/src/Adapter/Windows.php b/src/Adapter/Windows.php index 7f40a35..9ba1b9c 100644 --- a/src/Adapter/Windows.php +++ b/src/Adapter/Windows.php @@ -144,15 +144,6 @@ public function isUtf8() return false; } - /** - * Set cursor position - * @param int $x - * @param int $y - */ - public function setPos($x, $y) - { - } - /** * Return current console window title. * @@ -257,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/Console.php b/src/Console.php index 7389e60..0e84cb8 100644 --- a/src/Console.php +++ b/src/Console.php @@ -101,6 +101,14 @@ public static function getInstance($forceAdapter = null, $forceCharset = null) return static::$instance; } + /** + * Reset the console instance + */ + public static function resetInstance() + { + static::$instance = null; + } + /** * Check if currently running under MS Windows * @@ -135,10 +143,10 @@ public static function isAnsicon() */ public static function isConsole() { - if (null !== static::$isConsole && is_bool(static::$isConsole)) { - return static::$isConsole; + if (null === static::$isConsole) { + static::$isConsole = (PHP_SAPI == 'cli'); } - return PHP_SAPI == 'cli'; + return static::$isConsole; } /** @@ -148,6 +156,9 @@ public static function isConsole() */ public static function overrideIsConsole($flag) { + if(null != $flag) { + $flag = (bool)$flag; + } static::$isConsole = $flag; } diff --git a/src/Prompt/Char.php b/src/Prompt/Char.php index b49b21e..648ff2c 100644 --- a/src/Prompt/Char.php +++ b/src/Prompt/Char.php @@ -39,33 +39,25 @@ class Char extends AbstractPrompt /** * Ask the user for a single key stroke * - * @param string $promptText The prompt text to display in console - * @param string $allowedChars A list of allowed chars (i.e. "abc12345") - * @param bool $ignoreCase If true, case will be ignored and prompt will always return lower-cased response - * @param bool $allowEmpty Is empty response allowed? - * @param bool $echo Display the selection after user presses key + * @param string $promptText The prompt text to display in console + * @param string $allowedChars A list of allowed chars (i.e. "abc12345") + * @param bool $ignoreCase If true, case will be ignored and prompt will always return lower-cased response + * @param bool $allowEmpty Is empty response allowed? + * @param bool $echo Display the selection after user presses key */ public function __construct( $promptText = 'Please hit a key', - $allowedChars = 'abc', + $allowedChars = '0123456789abcdefghijklmnopqrstuvwxyz', $ignoreCase = true, $allowEmpty = false, $echo = true ) { - if ($promptText !== null) { - $this->setPromptText($promptText); - } - - if ($allowEmpty !== null) { - $this->setAllowEmpty($allowEmpty); - } + $this->setPromptText($promptText); + $this->setAllowEmpty($allowEmpty); + $this->setIgnoreCase($ignoreCase); - if ($ignoreCase !== null) { - $this->setIgnoreCase($ignoreCase); - } - - if ($allowedChars !== null) { + if (null != $allowedChars) { if ($this->ignoreCase) { $this->setAllowedChars(strtolower($allowedChars)); } else { @@ -73,9 +65,7 @@ public function __construct( } } - if ($echo !== null) { - $this->setEcho($echo); - } + $this->setEcho($echo); } /** @@ -99,43 +89,28 @@ 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; } /** - * @param bool $allowEmpty + * @param bool $allowEmpty */ public function setAllowEmpty($allowEmpty) { - $this->allowEmpty = $allowEmpty; + $this->allowEmpty = (bool) $allowEmpty; } /** @@ -179,11 +154,11 @@ public function getAllowedChars() } /** - * @param bool $ignoreCase + * @param bool $ignoreCase */ public function setIgnoreCase($ignoreCase) { - $this->ignoreCase = $ignoreCase; + $this->ignoreCase = (bool) $ignoreCase; } /** @@ -195,11 +170,11 @@ public function getIgnoreCase() } /** - * @param bool $echo + * @param bool $echo */ public function setEcho($echo) { - $this->echo = $echo; + $this->echo = (bool) $echo; } /** 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/src/Request.php b/src/Request.php index 84ecab7..16d6bf0 100644 --- a/src/Request.php +++ b/src/Request.php @@ -119,11 +119,7 @@ public function getParam($name, $default = null) */ public function params() { - if ($this->params === null) { - $this->params = new Parameters(); - } - - return $this->params; + return $this->getParams(); } /** @@ -139,6 +135,18 @@ public function setEnv(Parameters $env) return $this; } + /** + * Return a single parameter container responsible for env parameters + * + * @param string $name Parameter name + * @param string $default (optional) default value in case the parameter does not exist + * @return \Zend\Stdlib\Parameters + */ + public function getEnv($name, $default = null) + { + return $this->env()->get($name, $default); + } + /** * Return the parameter container responsible for env parameters * diff --git a/test/Adapater/AbstractAdapterTest.php b/test/Adapater/AbstractAdapterTest.php new file mode 100644 index 0000000..21ba5ae --- /dev/null +++ b/test/Adapater/AbstractAdapterTest.php @@ -0,0 +1,103 @@ +adapter = new ConsoleAdapter(); + $this->adapter->stream = fopen('php://memory', 'w+'); + } + + public function tearDown() + { + fclose($this->adapter->stream); + } + + public function testWriteChar() + { + ob_start(); + $this->adapter->write('foo'); + $this->assertEquals('foo', ob_get_clean()); + } + + public function testWriteText() + { + ob_start(); + $this->adapter->writeText('foo'); + $this->assertEquals('foo', ob_get_clean()); + } + + public function testWriteLine() + { + ob_start(); + $this->adapter->writeLine('foo'); + $this->assertEquals("foo\n", ob_get_clean()); + + ob_start(); + $this->adapter->writeLine("foo\nbar"); + $this->assertEquals("foo bar\n", ob_get_clean()); + + ob_start(); + $this->adapter->writeLine("\rfoo\r"); + $this->assertEquals("foo\n", ob_get_clean()); + } + + public function testReadLine() + { + fwrite($this->adapter->stream, 'baz'); + + $line = $this->adapter->readLine(); + $this->assertEquals($line, 'baz'); + } + + public function testReadLineWithLimit() + { + fwrite($this->adapter->stream, 'baz, bar, foo'); + + $line = $this->adapter->readLine(6); + $this->assertEquals($line, 'baz, b'); + } + + public function testReadChar() + { + fwrite($this->adapter->stream, 'bar'); + + $char = $this->adapter->readChar(); + $this->assertEquals($char, 'b'); + } + + public function testReadCharWithMask() + { + fwrite($this->adapter->stream, 'bar'); + + $char = $this->adapter->readChar('ar'); + $this->assertEquals($char, 'a'); + } + + public function testReadCharWithMaskInsensitiveCase() + { + fwrite($this->adapter->stream, 'bAr'); + + $char = $this->adapter->readChar('ar'); + $this->assertEquals($char, 'r'); + } +} diff --git a/test/Xterm256Test.php b/test/Color/Xterm256Test.php similarity index 99% rename from test/Xterm256Test.php rename to test/Color/Xterm256Test.php index 4dacc9a..89ed59c 100644 --- a/test/Xterm256Test.php +++ b/test/Color/Xterm256Test.php @@ -8,7 +8,7 @@ * @package Zend_Console */ -namespace ZendTest\Console; +namespace ZendTest\Console\Color; use ReflectionClass; use Zend\Console\Color\Xterm256; diff --git a/test/ConsoleTest.php b/test/ConsoleTest.php new file mode 100644 index 0000000..49779a3 --- /dev/null +++ b/test/ConsoleTest.php @@ -0,0 +1,86 @@ +assertTrue(Console::isConsole()); + $className = Console::detectBestAdapter(); + $adpater = new $className; + $this->assertTrue($adpater instanceof Adapter\AdapterInterface); + + Console::overrideIsConsole(false); + + $this->assertFalse(Console::isConsole()); + $this->assertEquals(null, Console::detectBestAdapter()); + } + + public function testCanOverrideIsConsole() + { + $this->assertEquals(true, Console::isConsole()); + + Console::overrideIsConsole(true); + $this->assertEquals(true, Console::isConsole()); + + Console::overrideIsConsole(false); + $this->assertEquals(false, Console::isConsole()); + + Console::overrideIsConsole(1); + $this->assertEquals(true, Console::isConsole()); + + Console::overrideIsConsole('false'); + $this->assertEquals(true, Console::isConsole()); + } + + public function testCanGetInstance() + { + $console = Console::getInstance(); + $this->assertTrue($console instanceof Adapter\AdapterInterface); + } + + public function testCanNotGetInstanceInNoConsoleMode() + { + Console::overrideIsConsole(false); + $this->setExpectedException('Zend\Console\Exception\RuntimeException'); + Console::getInstance(); + } + + public function testCanForceInstance() + { + $console = Console::getInstance('Posix'); + $this->assertTrue($console instanceof Adapter\AdapterInterface); + $this->assertTrue($console instanceof Adapter\Posix); + + Console::overrideIsConsole(null); + Console::resetInstance(); + + $console = Console::getInstance('Windows'); + $this->assertTrue($console instanceof Adapter\AdapterInterface); + $this->assertTrue($console instanceof Adapter\Windows); + } +} diff --git a/test/Prompt/CharTest.php b/test/Prompt/CharTest.php new file mode 100644 index 0000000..ffcac9d --- /dev/null +++ b/test/Prompt/CharTest.php @@ -0,0 +1,103 @@ +adapter = new ConsoleAdapter(); + $this->adapter->stream = fopen('php://memory', 'w+'); + } + + public function tearDown() + { + fclose($this->adapter->stream); + } + + public function testCanPromptChar() + { + fwrite($this->adapter->stream, 'a'); + + $char = new Char(); + $char->setEcho(false); + $char->setConsole($this->adapter); + ob_start(); + $response = $char->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "Please hit a key\n"); + $this->assertEquals('a', $response); + } + + public function testCanPromptCharWithCharNotInDefaultMask() + { + fwrite($this->adapter->stream, '*zywa'); + + $char = new Char(); + $char->setEcho(false); + $char->setConsole($this->adapter); + ob_start(); + $response = $char->show(); + ob_get_clean(); + $this->assertEquals('z', $response); + } + + public function testCanPromptCharWithNewQuestionAndMask() + { + fwrite($this->adapter->stream, 'foo123'); + + $char = new Char("Give a number", '0123456789'); + $char->setEcho(false); + $char->setConsole($this->adapter); + ob_start(); + $response = $char->show(); + $text = ob_get_clean(); + $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/Prompt/LineTest.php b/test/Prompt/LineTest.php new file mode 100644 index 0000000..3f110b7 --- /dev/null +++ b/test/Prompt/LineTest.php @@ -0,0 +1,72 @@ +adapter = new ConsoleAdapter(); + $this->adapter->stream = fopen('php://memory', 'w+'); + } + + public function tearDown() + { + fclose($this->adapter->stream); + } + + public function testCanReadLine() + { + fwrite($this->adapter->stream, 'Bryan is in the kitchen'); + + $line = new Line('Where is Bryan ?'); + $line->setConsole($this->adapter); + ob_start(); + $response = $line->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "Where is Bryan ?"); + $this->assertEquals('Bryan is in the kitchen', $response); + } + + public function testCanReadLineWithMax() + { + fwrite($this->adapter->stream, 'Kitchen no ?'); + + $line = new Line('Where is Bryan ?', false, 7); + $line->setConsole($this->adapter); + ob_start(); + $response = $line->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "Where is Bryan ?"); + $this->assertEquals('Kitchen', $response); + } + + public function testCanReadLineWithEmptyAnswer() + { + $line = new Line('Where is Bryan ?', true); + $line->setConsole($this->adapter); + ob_start(); + $response = $line->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "Where is Bryan ?"); + $this->assertEquals('', $response); + } +} diff --git a/test/Prompt/NumberTest.php b/test/Prompt/NumberTest.php new file mode 100644 index 0000000..c79a9f0 --- /dev/null +++ b/test/Prompt/NumberTest.php @@ -0,0 +1,115 @@ +adapter = new ConsoleAdapter(); + $this->adapter->stream = fopen('php://memory', 'w+'); + } + + public function tearDown() + { + fclose($this->adapter->stream); + } + + public function testCanReadNumber() + { + fwrite($this->adapter->stream, "123"); + + $number = new Number(); + $number->setConsole($this->adapter); + ob_start(); + $response = $number->show(); + $text = ob_get_clean(); + $this->assertEquals($text, "Please enter a number: "); + $this->assertEquals('123', $response); + } + + public function testCanReadNumberOnMultilign() + { + fwrite($this->adapter->stream, "a\n"); + fwrite($this->adapter->stream, "123\n"); + rewind($this->adapter->stream); + $this->adapter->autoRewind = false; + + $number = new Number(); + $number->setConsole($this->adapter); + ob_start(); + $response = $number->show(); + $text = ob_get_clean(); + $this->assertTrue((bool) preg_match('#a is not a number#', $text)); + $this->assertEquals('123', $response); + } + + public function testCanNotReadFloatByDefault() + { + fwrite($this->adapter->stream, "1.23\n"); + fwrite($this->adapter->stream, "123\n"); + rewind($this->adapter->stream); + $this->adapter->autoRewind = false; + + $number = new Number(); + $number->setConsole($this->adapter); + ob_start(); + $response = $number->show(); + $text = ob_get_clean(); + $this->assertTrue((bool) preg_match('#Please enter a non-floating number#', $text)); + $this->assertEquals('123', $response); + } + + public function testCanForceToReadFloat() + { + fwrite($this->adapter->stream, "1.23\n"); + fwrite($this->adapter->stream, "123\n"); + rewind($this->adapter->stream); + $this->adapter->autoRewind = false; + + $number = new Number('Give me a number', false, true); + $number->setConsole($this->adapter); + ob_start(); + $response = $number->show(); + $text = ob_get_clean(); + $this->assertEquals($text, 'Give me a number'); + $this->assertEquals('1.23', $response); + } + + public function testCanDefineAMax() + { + fwrite($this->adapter->stream, "1\n"); + fwrite($this->adapter->stream, "11\n"); + fwrite($this->adapter->stream, "6\n"); + rewind($this->adapter->stream); + $this->adapter->autoRewind = false; + + $number = new Number('Give me a number', false, false, 5, 10); + $number->setConsole($this->adapter); + ob_start(); + $response = $number->show(); + $text = ob_get_clean(); + + $this->assertTrue((bool) preg_match('#Please enter a number not smaller than 5#', $text)); + $this->assertTrue((bool) preg_match('#Please enter a number not greater than 10#', $text)); + $this->assertEquals('6', $response); + } +} diff --git a/test/Prompt/SelectTest.php b/test/Prompt/SelectTest.php new file mode 100644 index 0000000..8aed22f --- /dev/null +++ b/test/Prompt/SelectTest.php @@ -0,0 +1,63 @@ +adapter = new ConsoleAdapter(); + $this->adapter->stream = fopen('php://memory', 'w+'); + } + + public function tearDown() + { + fclose($this->adapter->stream); + } + + public function testCanSelectOption() + { + fwrite($this->adapter->stream, "0"); + + $select = new Select('Select an option :', array('foo', 'bar')); + $select->setConsole($this->adapter); + ob_start(); + $response = $select->show(); + $text = ob_get_clean(); + $this->assertTrue((bool) preg_match('#0\) foo#', $text)); + $this->assertTrue((bool) preg_match('#1\) bar#', $text)); + $this->assertEquals('0', $response); + } + + public function testCanSelectOptionWithCustomIndex() + { + fwrite($this->adapter->stream, "2"); + + $select = new Select('Select an option :', array('2' => 'foo', '6' => 'bar')); + $select->setConsole($this->adapter); + ob_start(); + $response = $select->show(); + $text = ob_get_clean(); + $this->assertTrue((bool) preg_match('#2\) foo#', $text)); + $this->assertTrue((bool) preg_match('#6\) bar#', $text)); + $this->assertEquals('2', $response); + } +} diff --git a/test/RequestTest.php b/test/RequestTest.php new file mode 100644 index 0000000..6799bff --- /dev/null +++ b/test/RequestTest.php @@ -0,0 +1,46 @@ +markTestSkipped("Cannot Test Zend\\Console\\Getopt without 'register_argc_argv' ini option true."); + } + } + + public function testCanConstructRequestAndGetParams() + { + $_SERVER['argv'] = array('foo.php', 'foo' => 'baz', 'bar'); + $_ENV["FOO_VAR"] = "bar"; + + $request = new Request(); + $params = $request->getParams(); + + $this->assertEquals(2, count($params)); + $this->assertEquals($params->toArray(), array('foo' => 'baz', 'bar')); + $this->assertEquals($request->getParam('foo'), 'baz'); + $this->assertEquals($request->getScriptName(), 'foo.php'); + $this->assertEquals(1, count($request->env())); + $this->assertEquals($request->env()->get('FOO_VAR'), 'bar'); + $this->assertEquals($request->getEnv('FOO_VAR'), 'bar'); + } +} diff --git a/test/ResponseTest.php b/test/ResponseTest.php new file mode 100644 index 0000000..d5edc78 --- /dev/null +++ b/test/ResponseTest.php @@ -0,0 +1,78 @@ +response = new Response(); + } + + public function testInitialisation() + { + $this->assertEquals(false, $this->response->contentSent()); + $this->assertEquals(0, $this->response->getErrorLevel()); + } + + public function testSetContent() + { + $this->response->setContent('foo, bar'); + $this->assertEquals(false, $this->response->contentSent()); + ob_start(); + $this->response->sendContent(); + $content = ob_get_clean(); + $this->assertEquals('foo, bar', $content); + $this->assertEquals(true, $this->response->contentSent()); + $this->assertEquals($this->response, $this->response->sendContent()); + } + + /* + public function testSetContentWithExit() + { + if (!function_exists('set_exit_overload')) { + $this->markTestSkipped("Install ext/test_helpers to test method with exit : https://github.com/sebastianbergmann/php-test-helpers."); + } + + $self = $this; + set_exit_overload( + function($param = null) use ($self) { + if ($param) { + $self->assertEquals($param, 1); + } + + return false; + } + ); + $this->response->setErrorLevel(1); + $this->response->setContent('foo, bar'); + ob_start(); + $this->response->send(); + $content = ob_get_clean(); + $this->assertEquals('foo, bar', $content); + + unset_exit_overload(); + } + */ +} diff --git a/test/TestAssets/ConsoleAdapter.php b/test/TestAssets/ConsoleAdapter.php new file mode 100644 index 0000000..66f7a56 --- /dev/null +++ b/test/TestAssets/ConsoleAdapter.php @@ -0,0 +1,54 @@ +autoRewind) { + rewind($this->stream); + } + $line = stream_get_line($this->stream, $maxLength, PHP_EOL); + return rtrim($line,"\n\r"); + } + + /** + * Read a single character from the console input + * + * @param string|null $mask A list of allowed chars + * @return string + */ + public function readChar($mask = null) + { + if($this->autoRewind) { + rewind($this->stream); + } + do { + $char = fread($this->stream, 1); + } while ("" === $char || ($mask !== null && false === strstr($mask, $char))); + return $char; + } +}