Skip to content

Commit b6e4d25

Browse files
authored
[TASK] Implement getArrayRepresentation() for Comment (#1454)
Part of #1440.
1 parent 80cb4c4 commit b6e4d25

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/Comment/Comment.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use Sabberworm\CSS\Position\Position;
99
use Sabberworm\CSS\Position\Positionable;
1010
use Sabberworm\CSS\Renderable;
11+
use Sabberworm\CSS\ShortClassNameProvider;
1112

1213
class Comment implements Positionable, Renderable
1314
{
1415
use Position;
16+
use ShortClassNameProvider;
1517

1618
/**
1719
* @var string
@@ -54,6 +56,11 @@ public function render(OutputFormat $outputFormat): string
5456
*/
5557
public function getArrayRepresentation(): array
5658
{
57-
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
59+
return [
60+
'class' => $this->getShortClassName(),
61+
// "contents" is the term used in the W3C specs:
62+
// https://www.w3.org/TR/CSS22/syndata.html#comments
63+
'contents' => $this->commentText,
64+
];
5865
}
5966
}

tests/Unit/Comment/CommentTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,27 @@ public function getLineNumberReturnsLineNumberProvidedToConstructor(): void
8181
/**
8282
* @test
8383
*/
84-
public function getArrayRepresentationThrowsException(): void
84+
public function getArrayRepresentationIncludesClassName(): void
8585
{
86-
$this->expectException(\BadMethodCallException::class);
87-
8886
$subject = new Comment();
8987

90-
$subject->getArrayRepresentation();
88+
$result = $subject->getArrayRepresentation();
89+
90+
self::assertArrayHasKey('class', $result);
91+
self::assertSame('Comment', $result['class']);
92+
}
93+
94+
/**
95+
* @test
96+
*/
97+
public function getArrayRepresentationIncludesContents(): void
98+
{
99+
$contents = 'War. War never changes.';
100+
$subject = new Comment($contents);
101+
102+
$result = $subject->getArrayRepresentation();
103+
104+
self::assertArrayHasKey('contents', $result);
105+
self::assertSame($contents, $result['contents']);
91106
}
92107
}

0 commit comments

Comments
 (0)