From 24fed24f41e86ddb5ad719baeac524a5b1330141 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 5 Dec 2021 00:34:43 +0100 Subject: [PATCH 1/6] Composer: improve PHPUnit version constraints ... as PHPUnit < 5.7.21 doesn't contain the forward compatibility layer with namespaced classes. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cf77ef4..26ffde8 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "php": ">=5.3.2" }, "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0", + "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0", "php-parallel-lint/php-parallel-lint": "^1.0", "php-parallel-lint/php-var-dump-check": "0.*", "squizlabs/php_codesniffer": "3.*", From 3f5c0e05d8cf0462c7cfefb986f9764afc7d5bba Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 4 Dec 2021 21:11:43 +0100 Subject: [PATCH 2/6] PHPUnit: improve configuration * Add more detailed basic test configuration * Unless `junit` is used somewhere, generating this report for code coverage is unnecessary. * Generating the text report, however, is useful. --- phpunit.xml.dist | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 27c3447..9671209 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,18 @@ @@ -18,9 +28,9 @@ - + From 4b322449f0a9b5e4a841dca4eb8075b67dca0abc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 4 Dec 2021 21:47:52 +0100 Subject: [PATCH 3/6] ConsoleColor: mark two methods as untestable These methods cannot be tested as the result is system dependent and cannot be emulated. --- src/ConsoleColor.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ConsoleColor.php b/src/ConsoleColor.php index b7234ed..1f87326 100644 --- a/src/ConsoleColor.php +++ b/src/ConsoleColor.php @@ -196,6 +196,8 @@ public function removeTheme($name) } /** + * @codeCoverageIgnore + * * @return bool */ public function isSupported() @@ -213,6 +215,8 @@ public function isSupported() } /** + * @codeCoverageIgnore + * * @return bool */ public function are256ColorsSupported() From 69d6b1eec6a2156b97905a93fc779688e1a33256 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 4 Dec 2021 21:48:46 +0100 Subject: [PATCH 4/6] ConsoleColor::addTheme(): add test for exception --- tests/ConsoleColorTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/ConsoleColorTest.php b/tests/ConsoleColorTest.php index 1d2a520..4de7d73 100644 --- a/tests/ConsoleColorTest.php +++ b/tests/ConsoleColorTest.php @@ -106,6 +106,16 @@ public function testAddOwnThemeArray() $this->assertSame("\033[1;2mtext\033[0m", $output); } + /** + * @covers ::addTheme + */ + public function testAddOwnThemeInvalidInput() + { + $this->exceptionHelper('\InvalidArgumentException', 'Style must be string or array.'); + + $this->uut->addTheme('invalid', new \ArrayIterator(array('bold', 'dark'))); + } + public function testOwnWithStyle() { $this->uut->addTheme('bold_dark', array('bold', 'dark')); From 3a8ed08c88b10d8a9c8cd87e854dc21585c9a28d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 4 Dec 2021 21:49:07 +0100 Subject: [PATCH 5/6] ConsoleColor::getThemes(): add perfunctory unit test --- tests/ConsoleColorTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/ConsoleColorTest.php b/tests/ConsoleColorTest.php index 4de7d73..1b7cf21 100644 --- a/tests/ConsoleColorTest.php +++ b/tests/ConsoleColorTest.php @@ -123,6 +123,28 @@ public function testOwnWithStyle() $this->assertSame("\033[1;2;3mtext\033[0m", $output); } + /** + * @covers ::getThemes + */ + public function testGetThemes() + { + $this->assertSame(array(), $this->uut->getThemes()); + + $this->uut->addTheme('bold_dark', array('bold', 'dark')); + $this->uut->addTheme('dark_italic', array('dark', 'italic')); + + $themes = $this->uut->getThemes(); + if (\method_exists($this, 'assertIsArray')) { + // PHPUnit 7.5+. + $this->assertIsArray($themes); + } else { + // PHPUnit < 7.5. + $this->assertInternalType('array', $themes); + } + + $this->assertCount(2, $themes); + } + public function testHasAndRemoveTheme() { $this->assertFalse($this->uut->hasTheme('bold_dark')); From fdddebbc7bcb68d1edc73a833177da006d6377fc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 4 Dec 2021 21:12:49 +0100 Subject: [PATCH 6/6] Tests: add `@covers` tag ... to document what each test is testing. --- tests/ConsoleColorTest.php | 92 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/ConsoleColorTest.php b/tests/ConsoleColorTest.php index 1b7cf21..8acfdff 100644 --- a/tests/ConsoleColorTest.php +++ b/tests/ConsoleColorTest.php @@ -5,6 +5,10 @@ use PHP_Parallel_Lint\PhpConsoleColor\Test\Fixtures\ConsoleColorWithForceSupport; use PHPUnit\Framework\TestCase; +/** + * @coversDefaultClass \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor + * @covers ::__construct + */ class ConsoleColorTest extends TestCase { /** @var ConsoleColorWithForceSupport */ @@ -18,18 +22,28 @@ protected function setUpConsoleColor() $this->uut = new ConsoleColorWithForceSupport(); } + /** + * @covers ::apply + */ public function testNone() { $output = $this->uut->apply('none', 'text'); $this->assertSame("text", $output); } + /** + * @covers ::apply + * @covers ::escSequence + */ public function testBold() { $output = $this->uut->apply('bold', 'text'); $this->assertSame("\033[1mtext\033[0m", $output); } + /** + * @covers ::apply + */ public function testBoldColorsAreNotSupported() { $this->uut->setIsSupported(false); @@ -38,6 +52,10 @@ public function testBoldColorsAreNotSupported() $this->assertSame("text", $output); } + /** + * @covers ::apply + * @covers ::setForceStyle + */ public function testBoldColorsAreNotSupportedButAreForced() { $this->uut->setIsSupported(false); @@ -47,24 +65,41 @@ public function testBoldColorsAreNotSupportedButAreForced() $this->assertSame("\033[1mtext\033[0m", $output); } + /** + * @covers ::apply + * @covers ::escSequence + */ public function testDark() { $output = $this->uut->apply('dark', 'text'); $this->assertSame("\033[2mtext\033[0m", $output); } + /** + * @covers ::apply + * @covers ::escSequence + */ public function testBoldAndDark() { $output = $this->uut->apply(array('bold', 'dark'), 'text'); $this->assertSame("\033[1;2mtext\033[0m", $output); } + /** + * @covers ::apply + * @covers ::styleSequence + * @covers ::escSequence + */ public function test256ColorForeground() { $output = $this->uut->apply('color_255', 'text'); $this->assertSame("\033[38;5;255mtext\033[0m", $output); } + /** + * @covers ::apply + * @covers ::styleSequence + */ public function test256ColorWithoutSupport() { $this->uut->setAre256ColorsSupported(false); @@ -73,18 +108,32 @@ public function test256ColorWithoutSupport() $this->assertSame("text", $output); } + /** + * @covers ::apply + * @covers ::styleSequence + * @covers ::escSequence + */ public function test256ColorBackground() { $output = $this->uut->apply('bg_color_255', 'text'); $this->assertSame("\033[48;5;255mtext\033[0m", $output); } + /** + * @covers ::apply + * @covers ::styleSequence + * @covers ::escSequence + */ public function test256ColorForegroundAndBackground() { $output = $this->uut->apply(array('color_200', 'bg_color_255'), 'text'); $this->assertSame("\033[38;5;200;48;5;255mtext\033[0m", $output); } + /** + * @covers ::setThemes + * @covers ::themeSequence + */ public function testSetOwnTheme() { $this->uut->setThemes(array('bold_dark' => array('bold', 'dark'))); @@ -92,6 +141,10 @@ public function testSetOwnTheme() $this->assertSame("\033[1;2mtext\033[0m", $output); } + /** + * @covers ::addTheme + * @covers ::themeSequence + */ public function testAddOwnTheme() { $this->uut->addTheme('bold_own', 'bold'); @@ -99,6 +152,11 @@ public function testAddOwnTheme() $this->assertSame("\033[1mtext\033[0m", $output); } + /** + * @covers ::apply + * @covers ::addTheme + * @covers ::themeSequence + */ public function testAddOwnThemeArray() { $this->uut->addTheme('bold_dark', array('bold', 'dark')); @@ -116,6 +174,13 @@ public function testAddOwnThemeInvalidInput() $this->uut->addTheme('invalid', new \ArrayIterator(array('bold', 'dark'))); } + /** + * @covers ::apply + * @covers ::addTheme + * @covers ::themeSequence + * @covers ::styleSequence + * @covers ::isValidStyle + */ public function testOwnWithStyle() { $this->uut->addTheme('bold_dark', array('bold', 'dark')); @@ -145,6 +210,10 @@ public function testGetThemes() $this->assertCount(2, $themes); } + /** + * @covers ::hasTheme + * @covers ::removeTheme + */ public function testHasAndRemoveTheme() { $this->assertFalse($this->uut->hasTheme('bold_dark')); @@ -156,30 +225,50 @@ public function testHasAndRemoveTheme() $this->assertFalse($this->uut->hasTheme('bold_dark')); } + /** + * @covers ::apply + */ public function testApplyInvalidArgument() { $this->exceptionHelper('\InvalidArgumentException'); $this->uut->apply(new \stdClass(), 'text'); } + /** + * @covers ::apply + * @covers \PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException + */ public function testApplyInvalidStyleName() { $this->exceptionHelper('\PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException'); $this->uut->apply('invalid', 'text'); } + /** + * @covers ::apply + * @covers \PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException + */ public function testApplyInvalid256Color() { $this->exceptionHelper('\PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException'); $this->uut->apply('color_2134', 'text'); } + /** + * @covers ::addTheme + * @covers ::isValidStyle + * @covers \PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException + */ public function testThemeInvalidStyle() { $this->exceptionHelper('\PHP_Parallel_Lint\PhpConsoleColor\InvalidStyleException'); $this->uut->addTheme('invalid', array('invalid')); } + /** + * @covers ::setForceStyle + * @covers ::isStyleForced + */ public function testForceStyle() { $this->assertFalse($this->uut->isStyleForced()); @@ -187,6 +276,9 @@ public function testForceStyle() $this->assertTrue($this->uut->isStyleForced()); } + /** + * @covers ::getPossibleStyles + */ public function testGetPossibleStyles() { if (\method_exists($this, 'assertIsArray')) {