Skip to content

[TASK] Add some tests for OutputFormatter (part 1) #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ parameters:
count: 4
path: ../src/OutputFormat.php

-
message: '#^Parameters should have "string" types as the only types passed to this method$#'
identifier: typePerfect.narrowPublicClassMethodParamType
count: 2
path: ../src/OutputFormatter.php

-
message: '#^Default value of the parameter \#2 \$bIncludeEnd \(false\) of method Sabberworm\\CSS\\Parsing\\ParserState\:\:consumeUntil\(\) is incompatible with type string\.$#'
identifier: parameter.defaultValue
Expand Down
300 changes: 300 additions & 0 deletions tests/Unit/OutputFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\OutputFormatter;
use Sabberworm\CSS\Renderable;

/**
* @covers \Sabberworm\CSS\OutputFormatter
*/
final class OutputFormatterTest extends TestCase
{
/**
* @var OutputFormatter
*/
private $subject;

/**
* @var OutputFormat
*/
private $outputFormat;

protected function setUp(): void
{
$this->outputFormat = new OutputFormat();
$this->subject = new OutputFormatter($this->outputFormat);
}

/**
* @test
*/
public function spaceAfterRuleNameReturnsSpaceAfterRuleNameFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceAfterRuleName($space);

self::assertSame($space, $this->subject->spaceAfterRuleName());
}

/**
* @test
*/
public function spaceBeforeRulesReturnsSpaceBeforeRulesFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceBeforeRules($space);

self::assertSame($space, $this->subject->spaceBeforeRules());
}

/**
* @test
*/
public function spaceAfterRulesReturnsSpaceAfterRulesFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceAfterRules($space);

self::assertSame($space, $this->subject->spaceAfterRules());
}

/**
* @test
*/
public function spaceBetweenRulesReturnsSpaceBetweenRulesFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceBetweenRules($space);

self::assertSame($space, $this->subject->spaceBetweenRules());
}

/**
* @test
*/
public function spaceBeforeBlocksReturnsSpaceBeforeBlocksFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceBeforeBlocks($space);

self::assertSame($space, $this->subject->spaceBeforeBlocks());
}

/**
* @test
*/
public function spaceAfterBlocksReturnsSpaceAfterBlocksFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceAfterBlocks($space);

self::assertSame($space, $this->subject->spaceAfterBlocks());
}

/**
* @test
*/
public function spaceBetweenBlocksReturnsSpaceBetweenBlocksFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceBetweenBlocks($space);

self::assertSame($space, $this->subject->spaceBetweenBlocks());
}

/**
* @test
*/
public function spaceBeforeSelectorSeparatorReturnsSpaceBeforeSelectorSeparatorFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceBeforeSelectorSeparator($space);

self::assertSame($space, $this->subject->spaceBeforeSelectorSeparator());
}

/**
* @test
*/
public function spaceAfterSelectorSeparatorReturnsSpaceAfterSelectorSeparatorFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceAfterSelectorSeparator($space);

self::assertSame($space, $this->subject->spaceAfterSelectorSeparator());
}

/**
* @test
*/
public function spaceBeforeListArgumentSeparatorReturnsSpaceSetForSpecificSeparator(): void
{
$separator = ',';
$space = ' ';
$this->outputFormat->setSpaceBeforeListArgumentSeparators([$separator => $space]);
$defaultSpace = "\t\t\t\t";
$this->outputFormat->setSpaceBeforeListArgumentSeparator($defaultSpace);

self::assertSame($space, $this->subject->spaceBeforeListArgumentSeparator($separator));
}

/**
* @test
*/
public function spaceBeforeListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace(
): void {
$space = ' ';
$this->outputFormat->setSpaceBeforeListArgumentSeparators([',' => $space]);
$defaultSpace = "\t\t\t\t";
$this->outputFormat->setSpaceBeforeListArgumentSeparator($defaultSpace);

self::assertSame($defaultSpace, $this->subject->spaceBeforeListArgumentSeparator(';'));
}

/**
* @test
*/
public function spaceAfterListArgumentSeparatorReturnsSpaceSetForSpecificSeparator(): void
{
$separator = ',';
$space = ' ';
$this->outputFormat->setSpaceAfterListArgumentSeparators([$separator => $space]);
$defaultSpace = "\t\t\t\t";
$this->outputFormat->setSpaceAfterListArgumentSeparator($defaultSpace);

self::assertSame($space, $this->subject->spaceAfterListArgumentSeparator($separator));
}

/**
* @test
*/
public function spaceAfterListArgumentSeparatorWithoutSpecificSettingReturnsDefaultSpace(
): void {
$space = ' ';
$this->outputFormat->setSpaceAfterListArgumentSeparators([',' => $space]);
$defaultSpace = "\t\t\t\t";
$this->outputFormat->setSpaceAfterListArgumentSeparator($defaultSpace);

self::assertSame($defaultSpace, $this->subject->spaceAfterListArgumentSeparator(';'));
}

/**
* @test
*/
public function spaceBeforeOpeningBraceReturnsSpaceBeforeOpeningBraceFromOutputFormat(): void
{
$space = ' ';
$this->outputFormat->setSpaceBeforeOpeningBrace($space);

self::assertSame($space, $this->subject->spaceBeforeOpeningBrace());
}

/**
* @test
*/
public function implodeForEmptyValuesReturnsEmptyString(): void
{
$values = [];

$result = $this->subject->implode(', ', $values);

self::assertSame('', $result);
}

/**
* @test
*/
public function implodeWithOneStringValueReturnsStringValue(): void
{
$value = 'tea';
$values = [$value];

$result = $this->subject->implode(', ', $values);

self::assertSame($value, $result);
}

/**
* @test
*/
public function implodeWithMultipleStringValuesReturnsValuesSeparatedBySeparator(): void
{
$value1 = 'tea';
$value2 = 'coffee';
$values = [$value1, $value2];
$separator = ', ';

$result = $this->subject->implode($separator, $values);

self::assertSame($value1 . $separator . $value2, $result);
}

/**
* @test
*/
public function implodeWithOneRenderableReturnsRenderedRenderable(): void
{
$renderable = $this->createMock(Renderable::class);
$renderedRenderable = 'tea';
$renderable->method('render')->with($this->outputFormat)->willReturn($renderedRenderable);
$values = [$renderable];

$result = $this->subject->implode(', ', $values);

self::assertSame($renderedRenderable, $result);
}

/**
* @test
*/
public function implodeWithMultipleRenderablesReturnsRenderedRenderablesSeparatedBySeparator(): void
{
$renderable1 = $this->createMock(Renderable::class);
$renderedRenderable1 = 'tea';
$renderable1->method('render')->with($this->outputFormat)->willReturn($renderedRenderable1);
$renderable2 = $this->createMock(Renderable::class);
$renderedRenderable2 = 'coffee';
$renderable2->method('render')->with($this->outputFormat)->willReturn($renderedRenderable2);
$values = [$renderable1, $renderable2];
$separator = ', ';

$result = $this->subject->implode($separator, $values);

self::assertSame($renderedRenderable1 . $separator . $renderedRenderable2, $result);
}

/**
* @test
*/
public function implodeWithIncreaseLevelFalseUsesDefaultIndentationLevelForRendering(): void
{
$renderable = $this->createMock(Renderable::class);
$renderedRenderable = 'tea';
$renderable->method('render')->with($this->outputFormat)->willReturn($renderedRenderable);
$values = [$renderable];

$result = $this->subject->implode(', ', $values, false);

self::assertSame($renderedRenderable, $result);
}

/**
* @test
*/
public function implodeWithIncreaseLevelTrueIncreasesIndentationLevelForRendering(): void
{
$renderable = $this->createMock(Renderable::class);
$renderedRenderable = 'tea';
$renderable->method('render')->with($this->outputFormat->nextLevel())->willReturn($renderedRenderable);
$values = [$renderable];

$result = $this->subject->implode(', ', $values, true);

self::assertSame($renderedRenderable, $result);
}
Comment on lines +286 to +299
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the level increment being asserted here. Shouldn't we be getting tea after some space?

Copy link
Collaborator Author

@oliverklee oliverklee Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's up to the Rederable instance how it uses the indentation level. (For example, Comments do not care about the indentation level at all.)

What we're testing here in this unit tests is that render() will get the next level output format passed in: The with() call asserts that the expected call has the corresponding argument (compared using ==, not ===, or like assertEquals() instead of assertSame()), and will fail the test otherwise.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed the change to the argument passed to with() compared with the previous test. I think I see and understand what's going on now. Reviewing this has been an educative process 👍

}