From 968677aad010db571f5daa33f09f5d6d93fc8d39 Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Sat, 7 Oct 2023 14:51:41 -0500 Subject: [PATCH] Update for PHP 8.1; Add better help color support --- .github/workflows/phpunit.yml | 5 +- .gitignore | 6 +- LICENSE.TXT | 2 +- README.md | 5 + composer.json | 8 +- phpunit.xml | 12 +- src/Command.php | 62 ++++---- src/Console.php | 291 ++++++++++++++++++++-------------- src/Exception.php | 6 +- tests/ConsoleTest.php | 70 +++++++- 10 files changed, 299 insertions(+), 168 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 131ac45..46eabbb 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -6,13 +6,16 @@ on: pull_request: branches: [ master ] +env: + XDEBUG_MODE: debug,coverage + jobs: build: runs-on: ubuntu-latest strategy: matrix: - php-versions: [ '7.4', '8.0' ] + php-versions: [ '8.1', '8.2' ] steps: - uses: actions/checkout@v3 diff --git a/.gitignore b/.gitignore index 23ef4b3..b4b31a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ composer.lock -bin public +script vendor - +kettle +.phpunit.result.cache +.phpunit.cache diff --git a/LICENSE.TXT b/LICENSE.TXT index 686bb74..d8b6beb 100644 --- a/LICENSE.TXT +++ b/LICENSE.TXT @@ -1,6 +1,6 @@ BSD 3 Clause License -Copyright (c) 2009-2023, NOLA Interactive, LLC. +Copyright (c) 2009-2024, NOLA Interactive, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index 487fe8b..69c0a31 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,11 @@ Install `pop-console` using Composer. composer require popphp/pop-console +Or, require it in your composer.json file + + "require": { + "popphp/pop-console" : "4.0.*" + } BASIC USAGE ----------- diff --git a/composer.json b/composer.json index a3c16d4..58847f0 100644 --- a/composer.json +++ b/composer.json @@ -20,11 +20,11 @@ } ], "require": { - "php": ">=7.4.0", - "popphp/popphp": "^3.7.2" + "php": ">=8.1.0", + "popphp/popphp": "^4.0.0" }, "require-dev": { - "phpunit/phpunit": "^9.0.0" + "phpunit/phpunit": "^10.0.0" }, "autoload": { "psr-4": { @@ -41,7 +41,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.2.x-dev" + "dev-master": "4.0.x-dev" } } } diff --git a/phpunit.xml b/phpunit.xml index a23c82c..9a86549 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,9 +1,6 @@ - - - - src - + + @@ -14,4 +11,9 @@ + + + src + + diff --git a/src/Command.php b/src/Command.php index 850877e..27e0181 100644 --- a/src/Command.php +++ b/src/Command.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -19,45 +19,47 @@ * @category Pop * @package Pop\Console * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.2.0 + * @version 4.0.0 */ class Command { /** * Command name - * @var string + * @var ?string */ - protected $name = null; + protected ?string $name = null; /** * Command params - * @var string + * @var ?string */ - protected $params = null; + protected ?string $params = null; /** * Command help - * @var string + * @var ?string */ - protected $help = null; + protected ?string $help = null; /** * Instantiate the command object * - * @param string $name - * @param string $params - * @param string $help - * @return Command + * @param string $name + * @param ?string $params + * @param ?string $help */ - public function __construct($name, $params = null, $help = null) + public function __construct(string $name, ?string $params = null, ?string $help = null) { - $this->name = $name; - $this->params = $params; - $this->help = $help; - return $this; + $this->setName($name); + if ($params !== null) { + $this->setParams($params); + } + if ($help !== null) { + $this->setHelp($help); + } } /** @@ -66,7 +68,7 @@ public function __construct($name, $params = null, $help = null) * @param string $name * @return Command */ - public function setName($name) + public function setName(string $name): Command { $this->name = $name; return $this; @@ -78,7 +80,7 @@ public function setName($name) * @param string $params * @return Command */ - public function setParams($params) + public function setParams(string $params): Command { $this->params = $params; return $this; @@ -90,7 +92,7 @@ public function setParams($params) * @param string $help * @return Command */ - public function setHelp($help) + public function setHelp(string $help): Command { $this->help = $help; return $this; @@ -101,7 +103,7 @@ public function setHelp($help) * * @return string */ - public function getName() + public function getName(): string { return $this->name; } @@ -111,7 +113,7 @@ public function getName() * * @return string */ - public function getParams() + public function getParams(): string { return $this->params; } @@ -119,9 +121,9 @@ public function getParams() /** * Determine if the command has params * - * @return boolean + * @return bool */ - public function hasParams() + public function hasParams(): bool { return (null !== $this->params); } @@ -129,9 +131,9 @@ public function hasParams() /** * Get the command help * - * @return string + * @return string|null */ - public function getHelp() + public function getHelp(): string|null { return $this->help; } @@ -139,9 +141,9 @@ public function getHelp() /** * Determine if the command has help * - * @return boolean + * @return bool */ - public function hasHelp() + public function hasHelp(): bool { return (null !== $this->help); } @@ -151,7 +153,7 @@ public function hasHelp() * * @return string */ - public function __toString() + public function __toString(): string { return $this->name . ((null !== $this->params) ? ' ' . $this->params : null); } diff --git a/src/Console.php b/src/Console.php index 2fc5e1c..de3f9e3 100644 --- a/src/Console.php +++ b/src/Console.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -13,7 +13,8 @@ */ namespace Pop\Console; -use Pop\Router\Match; +use Pop\Router\Match\Cli; +use ReflectionClass; /** * Console class @@ -21,9 +22,9 @@ * @category Pop * @package Pop\Console * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.2.0 + * @version 4.0.0 */ class Console { @@ -53,68 +54,68 @@ class Console * Console character width * @var int */ - protected $width = 80; + protected int $width = 80; /** * Console indentation - * @var string + * @var ?string */ - protected $indent = null; + protected ?string $indent = null; /** * Console response body - * @var string + * @var ?string */ - protected $response = null; + protected ?string $response = null; /** * Commands * @var array */ - protected $commands = []; + protected array $commands = []; /** * Console header - * @var string + * @var ?string */ - protected $header = null; + protected ?string $header = null; /** * Flag for if console header has been sent - * @var boolean + * @var bool */ - protected $headerSent = false; + protected bool $headerSent = false; /** * Console footer - * @var string + * @var ?string */ - protected $footer = null; + protected ?string $footer = null; /** * Help colors * @var array */ - protected $helpColors = []; + protected array $helpColors = []; /** * SERVER array * @var array */ - protected $server = []; + protected array $server = []; /** * ENV array * @var array */ - protected $env = []; + protected array $env = []; /** * Color map of ansi values * * @var array */ - protected static $colorMap = [ + protected static array $colorMap = [ 'foreground' => [ self::NORMAL => '22;39', self::BLACK => '0;30', @@ -153,7 +154,7 @@ class Console * @param int $width * @param string $indent */ - public function __construct($width = 80, $indent = ' ') + public function __construct(int $width = 80, string $indent = ' ') { $this->setWidth($width); $this->setIndent($indent); @@ -168,7 +169,7 @@ public function __construct($width = 80, $indent = ' ') * @param int $width * @return Console */ - public function setWidth($width) + public function setWidth(int $width): Console { $this->width = (int)$width; return $this; @@ -177,10 +178,10 @@ public function setWidth($width) /** * Set the indentation of the console object * - * @param string $indent + * @param ?string $indent * @return Console */ - public function setIndent($indent = null) + public function setIndent(?string $indent = null): Console { $this->indent = $indent; return $this; @@ -192,7 +193,7 @@ public function setIndent($indent = null) * @param string $header * @return Console */ - public function setHeader($header) + public function setHeader(string $header): Console { $this->header = $header; return $this; @@ -204,7 +205,7 @@ public function setHeader($header) * @param string $footer * @return Console */ - public function setFooter($footer) + public function setFooter(string $footer): Console { $this->footer = $footer; return $this; @@ -213,10 +214,10 @@ public function setFooter($footer) /** * Set the console header sent flag * - * @param boolean $headerSent + * @param bool $headerSent * @return Console */ - public function setHeaderSent($headerSent = true) + public function setHeaderSent(bool $headerSent = true): Console { $this->headerSent = (bool)$headerSent; return $this; @@ -225,22 +226,26 @@ public function setHeaderSent($headerSent = true) /** * Set the console help colors * - * @param int $color1 - * @param int $color2 - * @param int $color3 + * @param int $color1 + * @param ?int $color2 + * @param ?int $color3 + * @param ?int $color4 * @return Console */ - public function setHelpColors($color1, $color2 = null, $color3 = null) + public function setHelpColors(int $color1, ?int $color2 = null, ?int $color3 = null, ?int $color4 = null): Console { $this->helpColors = [ $color1 ]; - if (null !== $color2) { + if ($color2 !== null) { $this->helpColors[] = $color2; } - if (null !== $color3) { + if ($color3 !== null) { $this->helpColors[] = $color3; } + if ($color4 !== null) { + $this->helpColors[] = $color4; + } return $this; } @@ -250,7 +255,7 @@ public function setHelpColors($color1, $color2 = null, $color3 = null) * * @return int */ - public function getWidth() + public function getWidth(): int { return $this->width; } @@ -260,7 +265,7 @@ public function getWidth() * * @return string */ - public function getIndent() + public function getIndent(): string { return $this->indent; } @@ -268,10 +273,10 @@ public function getIndent() /** * Get the console header * - * @param boolean $formatted + * @param bool $formatted * @return string */ - public function getHeader($formatted = false) + public function getHeader(bool $formatted = false): string { return ($formatted) ? $this->formatTemplate($this->header) : $this->header; } @@ -279,10 +284,10 @@ public function getHeader($formatted = false) /** * Get the console footer * - * @param boolean $formatted + * @param bool $formatted * @return string */ - public function getFooter($formatted = false) + public function getFooter(bool $formatted = false): string { return ($formatted) ? $this->formatTemplate($this->footer) : $this->footer; } @@ -290,9 +295,9 @@ public function getFooter($formatted = false) /** * Get the console header sent flag * - * @return boolean + * @return bool */ - public function getHeaderSent() + public function getHeaderSent(): bool { return $this->headerSent; } @@ -302,38 +307,48 @@ public function getHeaderSent() * * @return array */ - public function getHelpColors() + public function getHelpColors(): array { return $this->helpColors; } + /** + * Get the console help colors + * + * @return array + */ + public function getAvailableColors(): array + { + return (new ReflectionClass('Pop\Console\Console'))->getConstants(); + } + /** * Get a value from $_SERVER, or the whole array * - * @param string $key - * @return string|array + * @param ?string $key + * @return string|array|null */ - public function getServer($key = null) + public function getServer(?string $key = null): string|array|null { - if (null === $key) { + if ($key === null) { return $this->server; } else { - return (isset($this->server[$key])) ? $this->server[$key] : null; + return $this->server[$key] ?? null; } } /** * Get a value from $_ENV, or the whole array * - * @param string $key - * @return string|array + * @param ?string $key + * @return string|array|null */ - public function getEnv($key = null) + public function getEnv(?string $key = null): string|array|null { - if (null === $key) { + if ($key === null) { return $this->env; } else { - return (isset($this->env[$key])) ? $this->env[$key] : null; + return $this->env[$key] ?? null; } } @@ -343,7 +358,7 @@ public function getEnv($key = null) * @param Command $command * @return Console */ - public function addCommand(Command $command) + public function addCommand(Command $command): Console { $this->commands[$command->getName()] = $command; return $this; @@ -355,7 +370,7 @@ public function addCommand(Command $command) * @param array $commands * @return Console */ - public function addCommands(array $commands) + public function addCommands(array $commands): Console { foreach ($commands as $command) { $this->addCommand($command); @@ -368,7 +383,7 @@ public function addCommands(array $commands) * * @return array */ - public function getCommands() + public function getCommands(): array { return $this->commands; } @@ -377,20 +392,20 @@ public function getCommands() * Get a command * * @param string $command - * @return Command + * @return Command|null */ - public function getCommand($command) + public function getCommand(string $command): Command|null { - return (isset($this->commands[$command])) ? $this->commands[$command] : null; + return $this->commands[$command] ?? null; } /** * Check if the console object has a command * * @param string $command - * @return boolean + * @return bool */ - public function hasCommand($command) + public function hasCommand(string $command): bool { return isset($this->commands[$command]); } @@ -398,11 +413,11 @@ public function hasCommand($command) /** * Get commands from routes * - * @param Match\Cli $routeMatch - * @param string $scriptName + * @param Cli $routeMatch + * @param ?string $scriptName * @return array */ - public function getCommandsFromRoutes(Match\Cli $routeMatch, $scriptName = null) + public function getCommandsFromRoutes(Cli $routeMatch, ?string $scriptName = null): array { $routeMatch->match(); @@ -417,7 +432,7 @@ public function getCommandsFromRoutes(Match\Cli $routeMatch, $scriptName = null) $help = (isset($commandRoutes[$name]) && isset($commandRoutes[$name]['help'])) ? $commandRoutes[$name]['help'] : null; - if (null !== $scriptName) { + if ($scriptName !== null) { $commandName = $scriptName . ' ' . $commandName; } @@ -430,11 +445,11 @@ public function getCommandsFromRoutes(Match\Cli $routeMatch, $scriptName = null) /** * Add commands from routes * - * @param Match\Cli $routeMatch - * @param string $scriptName + * @param Cli $routeMatch + * @param ?string $scriptName * @return Console */ - public function addCommandsFromRoutes(Match\Cli $routeMatch, $scriptName = null) + public function addCommandsFromRoutes(Cli $routeMatch, ?string $scriptName = null): Console { $commands = $this->getCommandsFromRoutes($routeMatch, $scriptName); @@ -448,15 +463,16 @@ public function addCommandsFromRoutes(Match\Cli $routeMatch, $scriptName = null) /** * Get a help * - * @param string $command - * @return string + * @param ?string $command + * @return string|null */ - public function help($command = null) + public function help(?string $command = null): string|null { - if (null !== $command) { - return (isset($this->commands[$command])) ? $this->commands[$command]->getHelp() : null; + if ($command !== null) { + return $this->commands[$command]?->getHelp(); } else { $this->displayHelp(); + return null; } } @@ -464,11 +480,11 @@ public function help($command = null) * Colorize a string for output * * @param string $string - * @param int $fg - * @param int $bg + * @param ?int $fg + * @param ?int $bg * @return string */ - public function colorize($string, $fg = null, $bg = null) + public function colorize(string $string, ?int $fg = null, ?int $bg = null): string { if (stripos(PHP_OS, 'win') === false) { $fgColor = $this->getColorCode($fg, 'foreground'); @@ -483,16 +499,18 @@ public function colorize($string, $fg = null, $bg = null) /** * Get input from the prompt * - * @param string $prompt - * @param array $options - * @param boolean $caseSensitive - * @param int $length - * @param boolean $withHeaders + * @param string $prompt + * @param ?array $options + * @param bool $caseSensitive + * @param int $length + * @param bool $withHeaders * @return string */ - public function prompt($prompt, array $options = null, $caseSensitive = false, $length = 500, $withHeaders = true) + public function prompt( + string $prompt, ?array $options = null, bool $caseSensitive = false, int $length = 500, bool $withHeaders = true + ): string { - if (($withHeaders) && (null !== $this->header)) { + if (($withHeaders) && ($this->header !== null)) { $this->headerSent = true; echo $this->formatTemplate($this->header) . $this->indent . $prompt; } else { @@ -501,7 +519,10 @@ public function prompt($prompt, array $options = null, $caseSensitive = false, $ $input = null; - if (null !== $options) { + /** + * $_SERVER['X_POP_CONSOLE_INPUT'] is for testing purposes only + */ + if ($options !== null) { $length = 0; foreach ($options as $key => $value) { $options[$key] = ($caseSensitive) ? $value : strtolower((string)$value); @@ -511,20 +532,14 @@ public function prompt($prompt, array $options = null, $caseSensitive = false, $ } while (!in_array($input, $options)) { - if (null !== $input) { + if ($input !== null) { echo $this->indent . $prompt; } - $promptInput = fopen('php://stdin', 'r'); - $input = fgets($promptInput, strlen((string)$prompt) . $length); - $input = ($caseSensitive) ? rtrim($input) : strtolower(rtrim($input)); - fclose($promptInput); + $input = $this->getPromptInput($prompt, $length, $caseSensitive); } } else { - while (null === $input) { - $promptInput = fopen('php://stdin', 'r'); - $input = fgets($promptInput, strlen((string)$prompt) + $length); - $input = ($caseSensitive) ? rtrim($input) : strtolower(rtrim($input)); - fclose($promptInput); + while ($input === null) { + $input = $this->getPromptInput($prompt, $length, $caseSensitive); } } @@ -534,11 +549,11 @@ public function prompt($prompt, array $options = null, $caseSensitive = false, $ /** * Append a string of text to the response body * - * @param string $text - * @param boolean $newline + * @param ?string $text + * @param bool $newline * @return Console */ - public function append($text = null, $newline = true) + public function append(?string $text = null, bool $newline = true): Console { if ($this->width != 0) { $lines = (strlen((string)$text) > $this->width) ? @@ -548,20 +563,21 @@ public function append($text = null, $newline = true) } foreach ($lines as $line) { - $this->response .= $this->indent . $line . (($newline) ? PHP_EOL : null); + $this->response .= $this->indent . $line . (($newline) ? PHP_EOL : null); } + return $this; } /** * Write a string of text to the response body and send the response * - * @param string $text - * @param boolean $newline - * @param boolean $withHeaders + * @param ?string $text + * @param bool $newline + * @param bool $withHeaders * @return Console */ - public function write($text = null, $newline = true, $withHeaders = true) + public function write(?string $text = null, bool $newline = true, bool $withHeaders = true): Console { $this->append($text, $newline); $this->send($withHeaders); @@ -571,16 +587,16 @@ public function write($text = null, $newline = true, $withHeaders = true) /** * Send the response * - * @param boolean $withHeaders + * @param bool $withHeaders * @return Console */ - public function send($withHeaders = true) + public function send(bool $withHeaders = true): Console { if ($withHeaders) { - if ((null !== $this->header) && !($this->headerSent)) { + if (($this->header !== null) && !($this->headerSent)) { $this->response = $this->formatTemplate($this->header) . $this->response; } - if (null !== $this->footer) { + if ($this->footer !== null) { $this->response .= $this->formatTemplate($this->footer); } } @@ -595,13 +611,13 @@ public function send($withHeaders = true) * * @return void */ - public function displayHelp() + public function displayHelp(): void { $this->response = null; $commands = []; $commandLengths = []; - if (null !== $this->header) { + if ($this->header !== null) { $this->response .= $this->formatTemplate($this->header); } @@ -611,7 +627,7 @@ public function displayHelp() $length = strlen((string)$name); if (count($this->helpColors) > 0) { - if (strpos((string)$name, ' ') !== false) { + if (str_contains((string)$name, ' ')) { $name1 = substr($name, 0, strpos($name, ' ')); $name2 = substr($name, strpos($name, ' ') + 1); if (isset($this->helpColors[0])) { @@ -626,9 +642,23 @@ public function displayHelp() } } - if (null !== $params) { + if ($params !== null) { $length += (strlen((string)$params) + 1); - $name .= ' ' . ((isset($this->helpColors[2])) ? $this->colorize($params, $this->helpColors[2]) : $params); + if (str_contains($params, '-') && str_contains($params, '<')) { + $pars = explode(' ', $params); + if (count($pars) > 0) { + $optionFirst = str_contains($pars[0], '-'); + $colorIndex = 2; + foreach ($pars as $p) { + if ((($optionFirst) && str_contains($p, '<')) || ((!$optionFirst) && str_contains($p, '-'))) { + $colorIndex = 3; + } + $name .= ' ' . ((isset($this->helpColors[$colorIndex])) ? $this->colorize($p, $this->helpColors[$colorIndex]) : $p); + } + } + } else { + $name .= ' ' . ((isset($this->helpColors[2])) ? $this->colorize($params, $this->helpColors[2]) : $params); + } } $commands[$key] = $this->indent . $name; @@ -672,7 +702,7 @@ public function displayHelp() $i++; } - if (null !== $this->footer) { + if ($this->footer !== null) { $this->response .= $this->formatTemplate($this->footer); } @@ -684,7 +714,7 @@ public function displayHelp() * * @return void */ - public function clear() + public function clear(): void { echo chr(27) . "[2J" . chr(27) . "[;H"; } @@ -692,13 +722,13 @@ public function clear() /** * Get the color code from the color map * - * @param int $color + * @param ?int $color * @param string $type * @return mixed */ - protected function getColorCode($color, $type = 'foreground') + protected function getColorCode(?int $color = null, string $type = 'foreground'): mixed { - if (isset(static::$colorMap[$type]) && isset(static::$colorMap[$type][$color])) { + if (!empty($color) && isset(static::$colorMap[$type]) && isset(static::$colorMap[$type][$color])) { return static::$colorMap[$type][$color]; } return null; @@ -710,14 +740,14 @@ protected function getColorCode($color, $type = 'foreground') * @param string $template * @return string */ - protected function formatTemplate($template) + protected function formatTemplate(string $template): string { $format = null; - if (strpos($template, "\n") !== false) { + if (str_contains($template, "\n")) { $templateLines = explode("\n", $template); foreach ($templateLines as $line) { - $line = trim($line); + $line = trim($line); $format .= $this->indent . $line . PHP_EOL; } } else { @@ -727,4 +757,27 @@ protected function formatTemplate($template) return $format; } + /** + * Get prompt input + * + * @param string $prompt + * @param int $length + * @param bool $caseSensitive + * @return string + */ + protected function getPromptInput(string $prompt, int $length = 500, bool $caseSensitive = false): string + { + if (isset($_SERVER['X_POP_CONSOLE_INPUT'])) { + $input = ($caseSensitive) ? + rtrim($_SERVER['X_POP_CONSOLE_INPUT']) : strtolower(rtrim($_SERVER['X_POP_CONSOLE_INPUT'])); + } else { + $promptInput = fopen('php://stdin', 'r'); + $input = fgets($promptInput, strlen((string)$prompt) + $length); + $input = ($caseSensitive) ? rtrim($input) : strtolower(rtrim($input)); + fclose($promptInput); + } + + return $input; + } + } diff --git a/src/Exception.php b/src/Exception.php index 75e8aae..4b534ad 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -19,8 +19,8 @@ * @category Pop * @package Pop\Console * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.2.0 + * @version 4.0.0 */ class Exception extends \Exception {} diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index a4d16c6..4e2f4b7 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -39,11 +39,22 @@ public function testSetAndGetHeaderSent() $this->assertTrue($console->getHeaderSent()); } + public function testGetAvailableColors() + { + $console = new Console(); + $colors = $console->getAvailableColors(); + $this->assertEquals(17, count($colors)); + $this->assertTrue(isset($colors['MAGENTA'])); + $this->assertEquals(6, $colors['MAGENTA']); + $this->assertTrue(isset($colors['BOLD_WHITE'])); + $this->assertEquals(16, $colors['BOLD_WHITE']); + } + public function testSetAndGetHelpColors() { $console = new Console(); - $console->setHelpColors(Console::RED, Console::WHITE, Console::BLUE); - $this->assertEquals(3, count($console->getHelpColors())); + $console->setHelpColors(Console::RED, Console::WHITE, Console::BLUE, Console::MAGENTA); + $this->assertEquals(4, count($console->getHelpColors())); } public function testGetServer() @@ -186,7 +197,7 @@ public function testColor() public function testBadColor() { $console = new Console(); - $string = $console->colorize('Hello World', 'BAD_FG', 'BAD_BG'); + $string = $console->colorize('Hello World', 400, 500); $this->assertStringContainsString('Hello World', $string); } @@ -233,4 +244,57 @@ public function testClear() $this->assertNotNull($result); } + public function testPrompt() + { + $_SERVER['X_POP_CONSOLE_INPUT'] = 'y'; + + ob_start(); + $console = new Console(); + $answer = $console->prompt('Test prompt: '); + $result = ob_get_clean(); + + $this->assertEquals('y', $answer); + } + + public function testPromptWithIndent() + { + $_SERVER['X_POP_CONSOLE_INPUT'] = 'y'; + + ob_start(); + $console = new Console(); + $console->setHeader('Test Header:'); + $console->setIndent(' '); + $answer = $console->prompt('Test prompt: '); + $result = ob_get_clean(); + + $this->assertTrue(str_contains($result, ' Test prompt: ')); + } + + public function testPromptWithHeader() + { + $_SERVER['X_POP_CONSOLE_INPUT'] = 'y'; + + ob_start(); + $console = new Console(); + $console->setHeader('Test Header:'); + $answer = $console->prompt('Test prompt: '); + $result = ob_get_clean(); + + $this->assertTrue(str_contains($result, 'Test Header:')); + } + + public function testPromptWithOptions() + { + $_SERVER['X_POP_CONSOLE_INPUT'] = 'n'; + + ob_start(); + $console = new Console(); + $console->setIndent(' '); + $answer = $console->prompt('Test prompt: ', ['Y', 'N']); + $result = ob_get_clean(); + + $this->assertEquals('n', $answer); + $this->assertTrue(str_contains($result, ' Test prompt: ')); + } + } \ No newline at end of file