From 25c5f59e08eb907b539b9f090d79420fd6baca77 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 20 Jul 2020 22:51:02 +0200 Subject: [PATCH 1/5] PHPUnit setup: rename and update the config file It is recommended to use the `phpunit.xml.dist` file name for the PHPUnit configuration file to allow individual developers to overload it with a `phpunit.xml` file. To this end, I've: * Renamed the PHPUnit configuration file. * Adjusted the corresponding `export-ignore` in `.gitattributes`. * Added `phpunit.xml` to the `.gitignore` file so overload files from individual developers do not get committed. * Adjusted the references to the file in the Ant `build.xml` file. As for the config file itself: * Added a name for the testsuite as is required in later PHPUnit versions. * Changed the coverage `filter` directive to be more precise. * Added an explicit `backupGlobals` setting as the default value has changed across PHPUnit versions. --- .gitattributes | 2 +- .gitignore | 1 + build.xml | 4 ++-- phpunit.xml => phpunit.xml.dist | 11 ++++++----- 4 files changed, 10 insertions(+), 8 deletions(-) rename phpunit.xml => phpunit.xml.dist (54%) diff --git a/.gitattributes b/.gitattributes index aa522ca..b640645 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,4 +3,4 @@ .gitignore export-ignore .travis.yml export-ignore build.xml export-ignore -phpunit.xml export-ignore +phpunit.xml.dist export-ignore diff --git a/.gitignore b/.gitignore index 05ab16b..50f8d9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build vendor composer.lock +phpunit.xml diff --git a/build.xml b/build.xml index 6db22b7..7970339 100644 --- a/build.xml +++ b/build.xml @@ -65,7 +65,7 @@ - + @@ -79,7 +79,7 @@ - + diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 54% rename from phpunit.xml rename to phpunit.xml.dist index f1105cc..c694fb2 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -1,16 +1,17 @@ + - + tests - - - vendor - + + src + From 91d70ff21f91ae7b2cb4cee50072f6436b111a7b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 20 Jul 2020 22:53:09 +0200 Subject: [PATCH 2/5] PHPUnit: use a type-safe assertion `assertEquals()` is to `assertSame()` as `==` is to `===`, so always use `assertSame()` unless you _need_ a loose comparison. --- tests/ConsoleColorTest.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/ConsoleColorTest.php b/tests/ConsoleColorTest.php index aaa1a22..d500ca9 100644 --- a/tests/ConsoleColorTest.php +++ b/tests/ConsoleColorTest.php @@ -41,13 +41,13 @@ protected function setUp() public function testNone() { $output = $this->uut->apply('none', 'text'); - $this->assertEquals("text", $output); + $this->assertSame("text", $output); } public function testBold() { $output = $this->uut->apply('bold', 'text'); - $this->assertEquals("\033[1mtext\033[0m", $output); + $this->assertSame("\033[1mtext\033[0m", $output); } public function testBoldColorsAreNotSupported() @@ -55,7 +55,7 @@ public function testBoldColorsAreNotSupported() $this->uut->setIsSupported(false); $output = $this->uut->apply('bold', 'text'); - $this->assertEquals("text", $output); + $this->assertSame("text", $output); } public function testBoldColorsAreNotSupportedButAreForced() @@ -64,25 +64,25 @@ public function testBoldColorsAreNotSupportedButAreForced() $this->uut->setForceStyle(true); $output = $this->uut->apply('bold', 'text'); - $this->assertEquals("\033[1mtext\033[0m", $output); + $this->assertSame("\033[1mtext\033[0m", $output); } public function testDark() { $output = $this->uut->apply('dark', 'text'); - $this->assertEquals("\033[2mtext\033[0m", $output); + $this->assertSame("\033[2mtext\033[0m", $output); } public function testBoldAndDark() { $output = $this->uut->apply(array('bold', 'dark'), 'text'); - $this->assertEquals("\033[1;2mtext\033[0m", $output); + $this->assertSame("\033[1;2mtext\033[0m", $output); } public function test256ColorForeground() { $output = $this->uut->apply('color_255', 'text'); - $this->assertEquals("\033[38;5;255mtext\033[0m", $output); + $this->assertSame("\033[38;5;255mtext\033[0m", $output); } public function test256ColorWithoutSupport() @@ -90,47 +90,47 @@ public function test256ColorWithoutSupport() $this->uut->setAre256ColorsSupported(false); $output = $this->uut->apply('color_255', 'text'); - $this->assertEquals("text", $output); + $this->assertSame("text", $output); } public function test256ColorBackground() { $output = $this->uut->apply('bg_color_255', 'text'); - $this->assertEquals("\033[48;5;255mtext\033[0m", $output); + $this->assertSame("\033[48;5;255mtext\033[0m", $output); } public function test256ColorForegroundAndBackground() { $output = $this->uut->apply(array('color_200', 'bg_color_255'), 'text'); - $this->assertEquals("\033[38;5;200;48;5;255mtext\033[0m", $output); + $this->assertSame("\033[38;5;200;48;5;255mtext\033[0m", $output); } public function testSetOwnTheme() { $this->uut->setThemes(array('bold_dark' => array('bold', 'dark'))); $output = $this->uut->apply(array('bold_dark'), 'text'); - $this->assertEquals("\033[1;2mtext\033[0m", $output); + $this->assertSame("\033[1;2mtext\033[0m", $output); } public function testAddOwnTheme() { $this->uut->addTheme('bold_own', 'bold'); $output = $this->uut->apply(array('bold_own'), 'text'); - $this->assertEquals("\033[1mtext\033[0m", $output); + $this->assertSame("\033[1mtext\033[0m", $output); } public function testAddOwnThemeArray() { $this->uut->addTheme('bold_dark', array('bold', 'dark')); $output = $this->uut->apply(array('bold_dark'), 'text'); - $this->assertEquals("\033[1;2mtext\033[0m", $output); + $this->assertSame("\033[1;2mtext\033[0m", $output); } public function testOwnWithStyle() { $this->uut->addTheme('bold_dark', array('bold', 'dark')); $output = $this->uut->apply(array('bold_dark', 'italic'), 'text'); - $this->assertEquals("\033[1;2;3mtext\033[0m", $output); + $this->assertSame("\033[1;2;3mtext\033[0m", $output); } public function testHasAndRemoveTheme() From 540b99936e51c11af3be354dec1b98fb61642494 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 20 Jul 2020 22:55:10 +0200 Subject: [PATCH 3/5] PHPUnit: widen the version requirements Made the unit tests compatible with PHPUnit 4.x - 7.x. --- composer.json | 2 +- tests/ConsoleColorTest.php | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index b65462c..94aba0f 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "php": ">=5.4.0" }, "require-dev": { - "phpunit/phpunit": "~4.3", + "phpunit/phpunit": "^4.8.36 || ^5.0 || ^6.0 || ^7.0", "php-parallel-lint/php-parallel-lint": "^1.0", "php-parallel-lint/php-var-dump-check": "0.*", "squizlabs/php_codesniffer": "1.*", diff --git a/tests/ConsoleColorTest.php b/tests/ConsoleColorTest.php index d500ca9..f9a5398 100644 --- a/tests/ConsoleColorTest.php +++ b/tests/ConsoleColorTest.php @@ -1,5 +1,6 @@ setExpectedException('\InvalidArgumentException'); + $this->exceptionHelper('\InvalidArgumentException'); $this->uut->apply(new stdClass(), 'text'); } public function testApplyInvalidStyleName() { - $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException'); + $this->exceptionHelper('\JakubOnderka\PhpConsoleColor\InvalidStyleException'); $this->uut->apply('invalid', 'text'); } public function testApplyInvalid256Color() { - $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException'); + $this->exceptionHelper('\JakubOnderka\PhpConsoleColor\InvalidStyleException'); $this->uut->apply('color_2134', 'text'); } public function testThemeInvalidStyle() { - $this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException'); + $this->exceptionHelper('\JakubOnderka\PhpConsoleColor\InvalidStyleException'); $this->uut->addTheme('invalid', array('invalid')); } @@ -180,5 +181,23 @@ public function testGetPossibleStyles() $this->assertInternalType('array', $this->uut->getPossibleStyles()); $this->assertNotEmpty($this->uut->getPossibleStyles()); } + + public function exceptionHelper($exception, $msg = null) + { + if (\method_exists($this, 'expectException')) { + // PHPUnit 5+. + $this->expectException($exception); + if (isset($msg)) { + $this->expectExceptionMessage($msg); + } + } else { + // PHPUnit 4. + if (isset($msg)) { + $this->setExpectedException($exception, $msg); + } else { + $this->setExpectedException($exception); + } + } + } } From 5ed7f8b9ab2d5e3b94a4272109112eed8aab2f86 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 20 Jul 2020 22:56:06 +0200 Subject: [PATCH 4/5] Travis: allow for installing PHPUnit 7 on PHP 8 --- .travis.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c775c11..56519fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,13 @@ cache: - $HOME/.composer/cache before_script: - - composer install --no-interaction --prefer-source + - | + if [[ $TRAVIS_PHP_VERSION != "nightly" ]]; then + composer install --no-suggest --no-interaction + else + # Ignore platform requirements to allow PHPUnit to install on PHP 8. + composer install --no-suggest --no-interaction --ignore-platform-reqs + fi script: - ant phplint From 451245b7b4cc85a0233862bfb1906a0e1654ed68 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 20 Jul 2020 22:42:29 +0200 Subject: [PATCH 5/5] Ant: fail the build on unit test failures --- build.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.xml b/build.xml index 7970339..9d95c2e 100644 --- a/build.xml +++ b/build.xml @@ -64,7 +64,7 @@ - + @@ -78,7 +78,7 @@ - +