From b2f380a8a179c9484ccba12386d8e1a23df84b18 Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Tue, 1 May 2018 07:30:51 +0200 Subject: [PATCH 1/7] test multiple, required operand without value This fails in #124 (cherry picked from commit 325f3c9) --- src/GetOpt.php | 2 +- test/Operands/CommonTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/GetOpt.php b/src/GetOpt.php index da653c2..0ec27c3 100644 --- a/src/GetOpt.php +++ b/src/GetOpt.php @@ -189,7 +189,7 @@ public function process($arguments = null) $arguments->process($this, $setOption, $setCommand, $addOperand); if (($operand = $this->nextOperand()) && $operand->isRequired() && - (!$operand->isMultiple() || count($this->getOperand($operand->getName())) === 0) + (!$operand->isMultiple() || count($operand->getValue()) === 0) ) { throw new Missing(sprintf('Operand %s is required', $operand->getName())); } diff --git a/test/Operands/CommonTest.php b/test/Operands/CommonTest.php index 1f7d795..3036386 100644 --- a/test/Operands/CommonTest.php +++ b/test/Operands/CommonTest.php @@ -2,6 +2,7 @@ namespace GetOpt\Test\Operands; +use GetOpt\ArgumentException\Missing; use GetOpt\Command; use GetOpt\GetOpt; use GetOpt\Operand; @@ -195,4 +196,19 @@ public function multipleFalse() self::assertFalse($operand->isMultiple()); } + + /** @test */ + public function requiredMultipleThrowsMissing() + { + $operand = new Operand('port'); + $operand->multiple(true); + $operand->required(true); + + $getOpt = new GetOpt(); + $getOpt->addOperand($operand); + + $this->setExpectedException('GetOpt\ArgumentException\Missing', 'Operand port is required'); + + $getOpt->process(''); + } } From b1fc5971d5f3a04112c07020047b39103485cca9 Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Tue, 1 May 2018 07:40:50 +0200 Subject: [PATCH 2/7] return empty array for multiple operands when no default value is given This solves #119 and #124 --- src/Operand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Operand.php b/src/Operand.php index e85d70a..b129878 100644 --- a/src/Operand.php +++ b/src/Operand.php @@ -115,7 +115,7 @@ public function getValue() } if ($this->isMultiple()) { - return $this->default !== null ? [ $this->default ] : null; + return $this->default !== null ? [ $this->default ] : []; } return $this->default; From 7245e4c210734578706d6e54ea3903da9da5fd1b Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Tue, 1 May 2018 07:43:57 +0200 Subject: [PATCH 3/7] remove unused import statement --- test/Operands/CommonTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Operands/CommonTest.php b/test/Operands/CommonTest.php index 3036386..9a3916e 100644 --- a/test/Operands/CommonTest.php +++ b/test/Operands/CommonTest.php @@ -2,7 +2,6 @@ namespace GetOpt\Test\Operands; -use GetOpt\ArgumentException\Missing; use GetOpt\Command; use GetOpt\GetOpt; use GetOpt\Operand; From fbc18adb9430511814edfe5bc4f42ecd559ad082 Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Tue, 15 May 2018 07:03:30 +0200 Subject: [PATCH 4/7] create coverage report only with 7.0 in travis ci --- .travis.yml | 8 +++++--- composer.json | 4 +++- phpunit.xml | 11 ----------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2c8141b..d7948e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,9 +17,11 @@ cache: before_script: - composer install --no-interaction - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then composer require satooshi/php-coveralls:~0.6@stable; fi;' - - mkdir -p build/logs + - mkdir -p build script: - composer code-style - - vendor/bin/phpunit -c phpunit.xml --coverage-clover=build/logs/clover.xml --coverage-text - - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then php vendor/bin/coveralls -v; fi;' + - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then composer coverage; else composer test; fi;' + +after_script: + - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then php vendor/bin/coveralls -v -x build/coverage.xml; fi;' diff --git a/composer.json b/composer.json index db536aa..16c6ad1 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,8 @@ } }, "scripts": { - "code-style": "phpcs --standard=PSR2 src && phpcs --standard=PSR2 test" + "code-style": "vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpcs --standard=PSR2 test", + "test": "vendor/bin/phpunit -c phpunit.xml", + "coverage": "vendor/bin/phpunit -c phpunit.xml --coverage-clover=build/coverage.xml --coverage-html=build/coverage --coverage-text" } } diff --git a/phpunit.xml b/phpunit.xml index 9c3d087..dc9954f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,15 +21,4 @@ - - - - From 0edf11d0ab564527d4e36076385d28b2b4c94512 Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Tue, 15 May 2018 07:46:20 +0200 Subject: [PATCH 5/7] fix coverage report in ci --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d7948e3..5ef6eb7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ cache: before_script: - composer install --no-interaction - sh -c 'if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then composer require satooshi/php-coveralls:~0.6@stable; fi;' - - mkdir -p build + - mkdir -p build/logs script: - composer code-style From f1af8cc92c337490c3070b2de19e25376a4e8e50 Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Tue, 15 May 2018 08:53:12 +0200 Subject: [PATCH 6/7] fix infinite loop with long words in descriptions This solves #127 (cherry picked from commit 8ebb4c9) --- src/Help.php | 11 ++++++++++- test/Options/HelpTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Help.php b/src/Help.php index 2ba6bdd..bb8419c 100644 --- a/src/Help.php +++ b/src/Help.php @@ -325,7 +325,16 @@ protected function renderColumns($columnWidth, $data) while (mb_strlen($row) > $screenWidth) { $p = strrpos(substr($row, 0, $screenWidth), ' '); - $text .= substr($row, 0, $p) . PHP_EOL; + if ($p < $columnWidth+4) { + // no space - check for dash + $p = strrpos(substr($row, 0, $screenWidth), '-'); + if ($p < $columnWidth+4) { + // break at screen width + $p = $screenWidth-1; + } + } + $c = substr($row, $p, 1); + $text .= substr($row, 0, $p) . ($c !== ' ' ? $c : '') . PHP_EOL; $row = sprintf(' %s %s', str_repeat(' ', $columnWidth), substr($row, $p+1)); } diff --git a/test/Options/HelpTest.php b/test/Options/HelpTest.php index e737e3f..d375564 100644 --- a/test/Options/HelpTest.php +++ b/test/Options/HelpTest.php @@ -3,6 +3,7 @@ namespace GetOpt\Test\Options; use GetOpt\GetOpt; +use GetOpt\Help; use GetOpt\Option; use PHPUnit\Framework\TestCase; @@ -77,6 +78,32 @@ public function helpTextWithLongDescriptions() ); } + /** @test */ + public function longWordsInDescription() + { + defined('COLUMNS') || define('COLUMNS', 90); + + $getopt = new GetOpt([ + ['a', 'alpha', GetOpt::OPTIONAL_ARGUMENT, 'This-is-a-long-word-with-dashes-what-should-not-cause-an-issue'], + ['b', 'beta', GetOpt::OPTIONAL_ARGUMENT, 'ThisWillCauseAnIssueBecauseWeDontKnowWhereToBreakInThisLongWord'], + ]); + + $script = $_SERVER['PHP_SELF']; + self::assertSame( + 'Usage: ' . $script . ' [options] [operands]' . PHP_EOL . PHP_EOL . + 'Options:' . PHP_EOL . + ' -a, --alpha [] This-is-a-long-' . PHP_EOL . + ' word-with-dashes-' . PHP_EOL . + ' what-should-not-' . PHP_EOL . + ' cause-an-issue' . PHP_EOL . + ' -b, --beta [] ThisWillCauseAnIs' . PHP_EOL . + ' sueBecauseWeDontK' . PHP_EOL . + ' nowWhereToBreakIn' . PHP_EOL . + ' ThisLongWord' . PHP_EOL . PHP_EOL, + $getopt->getHelpText([Help::MAX_WIDTH => 40]) + ); + } + /** @test */ public function helpTextWithArgumentName() { From 7acdfd07023c52b91fa426d1929736fbfd173e36 Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Tue, 29 May 2018 21:36:34 +0200 Subject: [PATCH 7/7] add ext-mbstring requirement to composer.json (cherry picked from commit dca7a4b) --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 16c6ad1..1994a45 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ } ], "require": { - "php": ">=5.4.0" + "php": ">=5.4.0", + "ext-mbstring": "*" }, "require-dev": { "phpunit/phpunit": "^4.8",