From 66f0ad398af41289856115a80819116684599996 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Mon, 17 Feb 2025 22:35:53 +0100 Subject: [PATCH 01/11] [TASK] Add some tests for `OutputFormatter` (part 2) Also autoformat the test class. The new PHPStan warning will go away once we switch the tested class to native type declarations. Part of #757 --- config/phpstan-baseline.neon | 2 +- tests/Unit/OutputFormatterTest.php | 263 ++++++++++++++++++++++++++++- 2 files changed, 260 insertions(+), 5 deletions(-) diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index f783902b..4fdf7a84 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -123,7 +123,7 @@ parameters: - message: '#^Parameters should have "string" types as the only types passed to this method$#' identifier: typePerfect.narrowPublicClassMethodParamType - count: 2 + count: 3 path: ../src/OutputFormatter.php - diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 94c967bd..a8d65772 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -5,6 +5,8 @@ namespace Sabberworm\CSS\Tests\Unit; use PHPUnit\Framework\TestCase; +use Sabberworm\CSS\Comment\Comment; +use Sabberworm\CSS\Comment\Commentable; use Sabberworm\CSS\OutputFormat; use Sabberworm\CSS\OutputFormatter; use Sabberworm\CSS\Renderable; @@ -146,8 +148,8 @@ public function spaceBeforeListArgumentSeparatorReturnsSpaceSetForSpecificSepara /** * @test */ - public function spaceBeforeListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace( - ): void { + public function spaceBeforeListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace(): void + { $space = ' '; $this->outputFormat->setSpaceBeforeListArgumentSeparators([',' => $space]); $defaultSpace = "\t\t\t\t"; @@ -173,8 +175,8 @@ public function spaceAfterListArgumentSeparatorReturnsSpaceSetForSpecificSeparat /** * @test */ - public function spaceAfterListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace( - ): void { + public function spaceAfterListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace(): void + { $space = ' '; $this->outputFormat->setSpaceAfterListArgumentSeparators([',' => $space]); $defaultSpace = "\t\t\t\t"; @@ -297,4 +299,257 @@ public function implodeWithIncreaseLevelTrueIncreasesIndentationLevelForRenderin self::assertSame($renderedRenderable, $result); } + + /** + * @return array + */ + public function provideUnchangedStringForRemoveLastSemicolon(): array + { + return [ + 'empty string' => [''], + 'string without semicolon' => ['Tea or coffee?'], + 'string with trailing semicolon' => ['Tea or coffee;'], + 'string with semicolon in the middle' => ['Tea; coffee'], + 'string with semicolons in the middle and trailing' => ['Tea; coffee;'], + ]; + } + + /** + * @test + * @dataProvider provideUnchangedStringForRemoveLastSemicolon + */ + public function removeLastSemicolonWithSemicolonAfterLastRuleEnabledUnchangedArgument(string $string): void + { + $this->outputFormat->setSemicolonAfterLastRule(true); + + $result = $this->subject->removeLastSemicolon($string); + + self::assertSame($string, $result); + } + + /** + * @return array + */ + public function provideChangedStringForRemoveLastSemicolon(): array + { + return [ + 'empty string' => ['', ''], + 'non-empty string without semicolon' => ['Tea or coffee?', 'Tea or coffee?'], + 'just 1 semicolon' => [';', ''], + 'just 2 semicolons' => [';;', ';'], + 'string with trailing semicolon' => ['tea or coffee;', 'tea or coffee'], + 'string with semicolon in the middle' => ['tea; coffee', 'tea coffee'], + 'string with semicolon in the middle and trailing' => ['tea; coffee;', 'tea; coffee'], + 'string with 2 semicolons in the middle' => ['tea; coffee; Club-Mate', 'tea; coffee Club-Mate'], + 'string with 2 semicolons in the middle surrounded by spaces' => [ + 'tea ; coffee ; Club-Mate', + 'tea ; coffee Club-Mate', + ], + 'string with 2 adjacent semicolons in the middle' => ['tea ;; coffee', 'tea ; coffee'], + 'string with 3 adjacent semicolons in the middle' => ['tea ;;; coffee', 'tea ;; coffee'], + ]; + } + + /** + * @test + * @dataProvider provideChangedStringForRemoveLastSemicolon + */ + public function removeLastSemicolonWithSemicolonAfterLastRuleDisabledRemovesTheLastSemicolon( + string $input, + string $expected + ): void { + $this->outputFormat->setSemicolonAfterLastRule(false); + + $result = $this->subject->removeLastSemicolon($input); + + self::assertSame($expected, $result); + } + + /** + * @test + */ + public function commentsWithEmptyCommentableAndRenderCommentsDisabledReturnsEmptyString(): void + { + $this->outputFormat->setRenderComments(false); + $this->outputFormat->setSpaceBetweenBlocks(' between-space '); + $this->outputFormat->setSpaceAfterBlocks(' after-space '); + + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([]); + + $result = $this->subject->comments($commentable); + + self::assertSame('', $result); + } + + /** + * @test + */ + public function commentsWithEmptyCommentableAndRenderCommentsEnabledReturnsEmptyString(): void + { + $this->outputFormat->setRenderComments(true); + $this->outputFormat->setSpaceBetweenBlocks(' between-space '); + $this->outputFormat->setSpaceAfterBlocks(' after-space '); + + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([]); + + $result = $this->subject->comments($commentable); + + self::assertSame('', $result); + } + + /** + * @test + */ + public function commentsWithCommentableWithOneCommentAndRenderCommentsDisabledReturnsEmptyString(): void + { + $this->outputFormat->setRenderComments(false); + $this->outputFormat->setSpaceBetweenBlocks(' between-space '); + $this->outputFormat->setSpaceAfterBlocks(' after-space '); + + $commentText = 'This is a comment.'; + $comment = new Comment($commentText); + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([$comment]); + + $result = $this->subject->comments($commentable); + + self::assertSame('', $result); + } + + /** + * @test + */ + public function commentsWithCommentableWithOneCommentRendersComment(): void + { + $this->outputFormat->setRenderComments(true); + $this->outputFormat->setSpaceBetweenBlocks(' between-space '); + $this->outputFormat->setSpaceAfterBlocks(' after-space '); + + $commentText = 'This is a comment.'; + $comment = new Comment($commentText); + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([$comment]); + + $result = $this->subject->comments($commentable); + + self::assertStringContainsString('/*' . $commentText . '*/', $result); + } + + /** + * @test + */ + public function commentsWithCommentableWithOneCommentPutsSpaceAfterBlocksAfterRenderedComment(): void + { + $this->outputFormat->setRenderComments(true); + $this->outputFormat->setSpaceBetweenBlocks(' between-space '); + $afterSpace = ' after-space '; + $this->outputFormat->setSpaceAfterBlocks($afterSpace); + + $commentText = 'This is a comment.'; + $comment = new Comment($commentText); + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([$comment]); + + $result = $this->subject->comments($commentable); + + self::assertSame('/*' . $commentText . '*/' . $afterSpace, $result); + } + + /** + * @test + */ + public function commentsWithCommentableWithTwoCommentsPutsSpaceAfterBlocksAfterLastRenderedComment(): void + { + $this->outputFormat->setRenderComments(true); + $this->outputFormat->setSpaceBetweenBlocks(' between-space '); + $afterSpace = ' after-space '; + $this->outputFormat->setSpaceAfterBlocks($afterSpace); + + $commentText1 = 'This is a comment 1.'; + $comment1 = new Comment($commentText1); + $commentText2 = 'This is a comment 2.'; + $comment2 = new Comment($commentText2); + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([$comment1, $comment2]); + + $result = $this->subject->comments($commentable); + + self::assertStringContainsString('/*' . $commentText2 . '*/' . $afterSpace, $result); + } + + /** + * @test + */ + public function commentsWithCommentableWithTwoCommentsSeparatesCommentsBySpaceBetweenBlocks(): void + { + $this->outputFormat->setRenderComments(true); + $betweenSpace = ' between-space '; + $this->outputFormat->setSpaceBetweenBlocks($betweenSpace); + $this->outputFormat->setSpaceAfterBlocks(' after-space '); + + $commentText1 = 'This is a comment 1.'; + $comment1 = new Comment($commentText1); + $commentText2 = 'This is a comment 2.'; + $comment2 = new Comment($commentText2); + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([$comment1, $comment2]); + + $result = $this->subject->comments($commentable); + + $expected = '/*' . $commentText1 . '*/' . $betweenSpace . '/*' . $commentText2 . '*/'; + self::assertStringContainsString($expected, $result); + } + + /** + * @test + */ + public function commentsWithCommentableWithMoreThanTwoCommentsPutsSpaceAfterBlocksAfterLastRenderedComment(): void + { + $this->outputFormat->setRenderComments(true); + $this->outputFormat->setSpaceBetweenBlocks(' between-space '); + $afterSpace = ' after-space '; + $this->outputFormat->setSpaceAfterBlocks($afterSpace); + + $commentText1 = 'This is a comment 1.'; + $comment1 = new Comment($commentText1); + $commentText2 = 'This is a comment 2.'; + $comment2 = new Comment($commentText2); + $commentText3 = 'This is a comment 3.'; + $comment3 = new Comment($commentText3); + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([$comment1, $comment2, $comment3]); + + $result = $this->subject->comments($commentable); + + self::assertStringContainsString('/*' . $commentText3 . '*/' . $afterSpace, $result); + } + + /** + * @test + */ + public function commentsWithCommentableWithMoreThanTwoCommentsSeparatesCommentsBySpaceBetweenBlocks(): void + { + $this->outputFormat->setRenderComments(true); + $betweenSpace = ' between-space '; + $this->outputFormat->setSpaceBetweenBlocks($betweenSpace); + $this->outputFormat->setSpaceAfterBlocks(' after-space '); + + $commentText1 = 'This is a comment 1.'; + $comment1 = new Comment($commentText1); + $commentText2 = 'This is a comment 2.'; + $comment2 = new Comment($commentText2); + $commentText3 = 'This is a comment 3.'; + $comment3 = new Comment($commentText3); + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([$comment1, $comment2, $comment3]); + + $result = $this->subject->comments($commentable); + + $expected = '/*' . $commentText1 . '*/' + . $betweenSpace . '/*' . $commentText2 . '*/' + . $betweenSpace . '/*' . $commentText3 . '*/'; + self::assertStringContainsString($expected, $result); + } } From 10d3c6adccad8ab381d09be4ab5270e99b22c0f4 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 18 Feb 2025 11:49:16 +0100 Subject: [PATCH 02/11] Update tests/Unit/OutputFormatterTest.php Co-authored-by: JakeQZ --- tests/Unit/OutputFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index a8d65772..18112187 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -318,7 +318,7 @@ public function provideUnchangedStringForRemoveLastSemicolon(): array * @test * @dataProvider provideUnchangedStringForRemoveLastSemicolon */ - public function removeLastSemicolonWithSemicolonAfterLastRuleEnabledUnchangedArgument(string $string): void + public function removeLastSemicolonWithSemicolonAfterLastRuleEnabledReturnsUnchangedArgument(string $string): void { $this->outputFormat->setSemicolonAfterLastRule(true); From 07cc65b2603cdbd4c0498a10e055cec51305bba8 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 18 Feb 2025 11:49:28 +0100 Subject: [PATCH 03/11] Update tests/Unit/OutputFormatterTest.php Co-authored-by: JakeQZ --- tests/Unit/OutputFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 18112187..942ec04d 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -354,7 +354,7 @@ public function provideChangedStringForRemoveLastSemicolon(): array * @test * @dataProvider provideChangedStringForRemoveLastSemicolon */ - public function removeLastSemicolonWithSemicolonAfterLastRuleDisabledRemovesTheLastSemicolon( + public function removeLastSemicolonWithSemicolonAfterLastRuleDisabledRemovesLastSemicolon( string $input, string $expected ): void { From 2bf874362b9fd31a91755cdba7cf2b145e1649da Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Tue, 18 Feb 2025 11:49:43 +0100 Subject: [PATCH 04/11] Update tests/Unit/OutputFormatterTest.php Co-authored-by: JakeQZ --- tests/Unit/OutputFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 942ec04d..bd91fa6b 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -491,7 +491,7 @@ public function commentsWithCommentableWithTwoCommentsSeparatesCommentsBySpaceBe $commentText1 = 'This is a comment 1.'; $comment1 = new Comment($commentText1); - $commentText2 = 'This is a comment 2.'; + $commentText2 = 'You like pears.'; $comment2 = new Comment($commentText2); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment1, $comment2]); From 25aeb1f6a416b4aa62c71300d062b4f08af78517 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 19 Feb 2025 10:38:52 +0100 Subject: [PATCH 05/11] Make the formatter test data look more like declaration blocks --- tests/Unit/OutputFormatterTest.php | 36 ++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index bd91fa6b..70443ac0 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -307,10 +307,10 @@ public function provideUnchangedStringForRemoveLastSemicolon(): array { return [ 'empty string' => [''], - 'string without semicolon' => ['Tea or coffee?'], - 'string with trailing semicolon' => ['Tea or coffee;'], - 'string with semicolon in the middle' => ['Tea; coffee'], - 'string with semicolons in the middle and trailing' => ['Tea; coffee;'], + 'string without semicolon' => ['Earl Grey: hot'], + 'string with trailing semicolon' => ['Earl Grey: hot;'], + 'string with semicolon in the middle' => ['Earl Grey: hot; Coffee: Americano'], + 'string with semicolons in the middle and trailing' => ['Earl Grey: hot; Coffee: Americano;'], ]; } @@ -334,19 +334,31 @@ public function provideChangedStringForRemoveLastSemicolon(): array { return [ 'empty string' => ['', ''], - 'non-empty string without semicolon' => ['Tea or coffee?', 'Tea or coffee?'], + 'non-empty string without semicolon' => ['Earl Grey: hot', 'Earl Grey: hot'], 'just 1 semicolon' => [';', ''], 'just 2 semicolons' => [';;', ';'], - 'string with trailing semicolon' => ['tea or coffee;', 'tea or coffee'], - 'string with semicolon in the middle' => ['tea; coffee', 'tea coffee'], - 'string with semicolon in the middle and trailing' => ['tea; coffee;', 'tea; coffee'], + 'string with trailing semicolon' => ['Earl Grey: hot;', 'Earl Grey: hot'], + 'string with semicolon in the middle' => [ + 'Earl Grey: hot; Coffee: Americano', + 'Earl Grey: hot Coffee: Americano', + ], + 'string with semicolon in the middle and trailing' => [ + 'Earl Grey: hot; Coffee: Americano;', + 'Earl Grey: hot; Coffee: Americano', + ], 'string with 2 semicolons in the middle' => ['tea; coffee; Club-Mate', 'tea; coffee Club-Mate'], 'string with 2 semicolons in the middle surrounded by spaces' => [ - 'tea ; coffee ; Club-Mate', - 'tea ; coffee Club-Mate', + 'Earl Grey: hot ; Coffee: Americano ; Club-Mate: cold', + 'Earl Grey: hot ; Coffee: Americano Club-Mate: cold', + ], + 'string with 2 adjacent semicolons in the middle' => [ + 'Earl Grey: hot;; Coffee: Americano', + 'Earl Grey: hot; Coffee: Americano', + ], + 'string with 3 adjacent semicolons in the middle' => [ + 'Earl Grey: hot;;; Coffee: Americano', + 'Earl Grey: hot;; Coffee: Americano', ], - 'string with 2 adjacent semicolons in the middle' => ['tea ;; coffee', 'tea ; coffee'], - 'string with 3 adjacent semicolons in the middle' => ['tea ;;; coffee', 'tea ;; coffee'], ]; } From ee93f6e561c3f9697608d496ed380b2e984b6898 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 19 Feb 2025 10:43:40 +0100 Subject: [PATCH 06/11] Fun with comments. --- tests/Unit/OutputFormatterTest.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 70443ac0..37b91066 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -420,7 +420,7 @@ public function commentsWithCommentableWithOneCommentAndRenderCommentsDisabledRe $this->outputFormat->setSpaceBetweenBlocks(' between-space '); $this->outputFormat->setSpaceAfterBlocks(' after-space '); - $commentText = 'This is a comment.'; + $commentText = 'I am a teapot.'; $comment = new Comment($commentText); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment]); @@ -439,7 +439,7 @@ public function commentsWithCommentableWithOneCommentRendersComment(): void $this->outputFormat->setSpaceBetweenBlocks(' between-space '); $this->outputFormat->setSpaceAfterBlocks(' after-space '); - $commentText = 'This is a comment.'; + $commentText = 'I am a teapot.'; $comment = new Comment($commentText); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment]); @@ -459,7 +459,7 @@ public function commentsWithCommentableWithOneCommentPutsSpaceAfterBlocksAfterRe $afterSpace = ' after-space '; $this->outputFormat->setSpaceAfterBlocks($afterSpace); - $commentText = 'This is a comment.'; + $commentText = 'I am a teapot.'; $comment = new Comment($commentText); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment]); @@ -479,9 +479,9 @@ public function commentsWithCommentableWithTwoCommentsPutsSpaceAfterBlocksAfterL $afterSpace = ' after-space '; $this->outputFormat->setSpaceAfterBlocks($afterSpace); - $commentText1 = 'This is a comment 1.'; + $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); - $commentText2 = 'This is a comment 2.'; + $commentText2 = 'But I am not.'; $comment2 = new Comment($commentText2); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment1, $comment2]); @@ -501,9 +501,9 @@ public function commentsWithCommentableWithTwoCommentsSeparatesCommentsBySpaceBe $this->outputFormat->setSpaceBetweenBlocks($betweenSpace); $this->outputFormat->setSpaceAfterBlocks(' after-space '); - $commentText1 = 'This is a comment 1.'; + $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); - $commentText2 = 'You like pears.'; + $commentText2 = 'But I am not.'; $comment2 = new Comment($commentText2); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment1, $comment2]); @@ -524,11 +524,11 @@ public function commentsWithCommentableWithMoreThanTwoCommentsPutsSpaceAfterBloc $afterSpace = ' after-space '; $this->outputFormat->setSpaceAfterBlocks($afterSpace); - $commentText1 = 'This is a comment 1.'; + $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); - $commentText2 = 'This is a comment 2.'; + $commentText2 = 'But I am what.'; $comment2 = new Comment($commentText2); - $commentText3 = 'This is a comment 3.'; + $commentText3 = 'So what am I then?'; $comment3 = new Comment($commentText3); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment1, $comment2, $comment3]); @@ -548,11 +548,11 @@ public function commentsWithCommentableWithMoreThanTwoCommentsSeparatesCommentsB $this->outputFormat->setSpaceBetweenBlocks($betweenSpace); $this->outputFormat->setSpaceAfterBlocks(' after-space '); - $commentText1 = 'This is a comment 1.'; + $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); - $commentText2 = 'This is a comment 2.'; + $commentText2 = 'But I am what.'; $comment2 = new Comment($commentText2); - $commentText3 = 'This is a comment 3.'; + $commentText3 = 'So what am I then?'; $comment3 = new Comment($commentText3); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([$comment1, $comment2, $comment3]); From 2a79bcb91492e8c444a0ef9502ec1cb083cd3817 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Wed, 19 Feb 2025 10:45:21 +0100 Subject: [PATCH 07/11] Make the test data more focused. --- tests/Unit/OutputFormatterTest.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 37b91066..af9e1640 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -436,8 +436,6 @@ public function commentsWithCommentableWithOneCommentAndRenderCommentsDisabledRe public function commentsWithCommentableWithOneCommentRendersComment(): void { $this->outputFormat->setRenderComments(true); - $this->outputFormat->setSpaceBetweenBlocks(' between-space '); - $this->outputFormat->setSpaceAfterBlocks(' after-space '); $commentText = 'I am a teapot.'; $comment = new Comment($commentText); @@ -455,7 +453,6 @@ public function commentsWithCommentableWithOneCommentRendersComment(): void public function commentsWithCommentableWithOneCommentPutsSpaceAfterBlocksAfterRenderedComment(): void { $this->outputFormat->setRenderComments(true); - $this->outputFormat->setSpaceBetweenBlocks(' between-space '); $afterSpace = ' after-space '; $this->outputFormat->setSpaceAfterBlocks($afterSpace); @@ -475,7 +472,6 @@ public function commentsWithCommentableWithOneCommentPutsSpaceAfterBlocksAfterRe public function commentsWithCommentableWithTwoCommentsPutsSpaceAfterBlocksAfterLastRenderedComment(): void { $this->outputFormat->setRenderComments(true); - $this->outputFormat->setSpaceBetweenBlocks(' between-space '); $afterSpace = ' after-space '; $this->outputFormat->setSpaceAfterBlocks($afterSpace); @@ -499,7 +495,6 @@ public function commentsWithCommentableWithTwoCommentsSeparatesCommentsBySpaceBe $this->outputFormat->setRenderComments(true); $betweenSpace = ' between-space '; $this->outputFormat->setSpaceBetweenBlocks($betweenSpace); - $this->outputFormat->setSpaceAfterBlocks(' after-space '); $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); @@ -520,7 +515,6 @@ public function commentsWithCommentableWithTwoCommentsSeparatesCommentsBySpaceBe public function commentsWithCommentableWithMoreThanTwoCommentsPutsSpaceAfterBlocksAfterLastRenderedComment(): void { $this->outputFormat->setRenderComments(true); - $this->outputFormat->setSpaceBetweenBlocks(' between-space '); $afterSpace = ' after-space '; $this->outputFormat->setSpaceAfterBlocks($afterSpace); @@ -546,7 +540,6 @@ public function commentsWithCommentableWithMoreThanTwoCommentsSeparatesCommentsB $this->outputFormat->setRenderComments(true); $betweenSpace = ' between-space '; $this->outputFormat->setSpaceBetweenBlocks($betweenSpace); - $this->outputFormat->setSpaceAfterBlocks(' after-space '); $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); From b0d434884eea77781bc52b7abf37c0a0bf650e0e Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 20 Feb 2025 09:18:30 +0100 Subject: [PATCH 08/11] Update tests/Unit/OutputFormatterTest.php Co-authored-by: JakeQZ --- tests/Unit/OutputFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index af9e1640..736e0ccc 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -520,7 +520,7 @@ public function commentsWithCommentableWithMoreThanTwoCommentsPutsSpaceAfterBloc $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); - $commentText2 = 'But I am what.'; + $commentText2 = 'But I am not.'; $comment2 = new Comment($commentText2); $commentText3 = 'So what am I then?'; $comment3 = new Comment($commentText3); From f337be0b897828920da6bc306dd9df3e28a52220 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 20 Feb 2025 09:18:38 +0100 Subject: [PATCH 09/11] Update tests/Unit/OutputFormatterTest.php Co-authored-by: JakeQZ --- tests/Unit/OutputFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 736e0ccc..82cfae55 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -543,7 +543,7 @@ public function commentsWithCommentableWithMoreThanTwoCommentsSeparatesCommentsB $commentText1 = 'I am a teapot.'; $comment1 = new Comment($commentText1); - $commentText2 = 'But I am what.'; + $commentText2 = 'But I am not.'; $comment2 = new Comment($commentText2); $commentText3 = 'So what am I then?'; $comment3 = new Comment($commentText3); From 4b95583d337656de6152bc18e8e4e1ac3ae42308 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 20 Feb 2025 09:18:48 +0100 Subject: [PATCH 10/11] Update tests/Unit/OutputFormatterTest.php Co-authored-by: JakeQZ --- tests/Unit/OutputFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 82cfae55..832de0f2 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -307,7 +307,7 @@ public function provideUnchangedStringForRemoveLastSemicolon(): array { return [ 'empty string' => [''], - 'string without semicolon' => ['Earl Grey: hot'], + 'string without semicolon' => ['earl-grey: hot'], 'string with trailing semicolon' => ['Earl Grey: hot;'], 'string with semicolon in the middle' => ['Earl Grey: hot; Coffee: Americano'], 'string with semicolons in the middle and trailing' => ['Earl Grey: hot; Coffee: Americano;'], From 7f71e800aa258ff56a65ebdcace20fe7bfb95738 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Thu, 20 Feb 2025 09:31:46 +0100 Subject: [PATCH 11/11] Make tests more explicit --- tests/Unit/OutputFormatterTest.php | 74 +++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/tests/Unit/OutputFormatterTest.php b/tests/Unit/OutputFormatterTest.php index 832de0f2..2caf30e4 100644 --- a/tests/Unit/OutputFormatterTest.php +++ b/tests/Unit/OutputFormatterTest.php @@ -377,14 +377,46 @@ public function removeLastSemicolonWithSemicolonAfterLastRuleDisabledRemovesLast self::assertSame($expected, $result); } + /** + * @test + */ + public function commentsWithEmptyCommentableAndRenderCommentsDisabledDoesNotReturnSpaceBetweenBlocks(): void + { + $this->outputFormat->setRenderComments(false); + $spaceBetweenBlocks = ' between-space '; + $this->outputFormat->setSpaceBetweenBlocks($spaceBetweenBlocks); + + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([]); + + $result = $this->subject->comments($commentable); + + self::assertStringNotContainsString($spaceBetweenBlocks, $result); + } + + /** + * @test + */ + public function commentsWithEmptyCommentableAndRenderCommentsDisabledDoesNotReturnSpaceAfterBlocks(): void + { + $this->outputFormat->setRenderComments(false); + $spaceAfterBlocks = ' after-space '; + $this->outputFormat->setSpaceAfterBlocks($spaceAfterBlocks); + + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([]); + + $result = $this->subject->comments($commentable); + + self::assertStringNotContainsString($spaceAfterBlocks, $result); + } + /** * @test */ public function commentsWithEmptyCommentableAndRenderCommentsDisabledReturnsEmptyString(): void { $this->outputFormat->setRenderComments(false); - $this->outputFormat->setSpaceBetweenBlocks(' between-space '); - $this->outputFormat->setSpaceAfterBlocks(' after-space '); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([]); @@ -394,14 +426,46 @@ public function commentsWithEmptyCommentableAndRenderCommentsDisabledReturnsEmpt self::assertSame('', $result); } + /** + * @test + */ + public function commentsWithEmptyCommentableAndRenderCommentsEnabledDoesNotReturnSpaceBetweenBlocks(): void + { + $this->outputFormat->setRenderComments(true); + $spaceBetweenBlocks = ' between-space '; + $this->outputFormat->setSpaceBetweenBlocks($spaceBetweenBlocks); + + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([]); + + $result = $this->subject->comments($commentable); + + self::assertStringNotContainsString($spaceBetweenBlocks, $result); + } + + /** + * @test + */ + public function commentsWithEmptyCommentableAndRenderCommentsEnabledDoesNotReturnSpaceAfterBlocks(): void + { + $this->outputFormat->setRenderComments(true); + $spaceAfterBlocks = ' after-space '; + $this->outputFormat->setSpaceAfterBlocks($spaceAfterBlocks); + + $commentable = $this->createMock(Commentable::class); + $commentable->method('getComments')->willReturn([]); + + $result = $this->subject->comments($commentable); + + self::assertStringNotContainsString($spaceAfterBlocks, $result); + } + /** * @test */ public function commentsWithEmptyCommentableAndRenderCommentsEnabledReturnsEmptyString(): void { $this->outputFormat->setRenderComments(true); - $this->outputFormat->setSpaceBetweenBlocks(' between-space '); - $this->outputFormat->setSpaceAfterBlocks(' after-space '); $commentable = $this->createMock(Commentable::class); $commentable->method('getComments')->willReturn([]); @@ -417,8 +481,6 @@ public function commentsWithEmptyCommentableAndRenderCommentsEnabledReturnsEmpty public function commentsWithCommentableWithOneCommentAndRenderCommentsDisabledReturnsEmptyString(): void { $this->outputFormat->setRenderComments(false); - $this->outputFormat->setSpaceBetweenBlocks(' between-space '); - $this->outputFormat->setSpaceAfterBlocks(' after-space '); $commentText = 'I am a teapot.'; $comment = new Comment($commentText);