Skip to content

Commit 1240f98

Browse files
committed
[TASK] Implement CSSNamespace::getArrayRepresentation()
Part of #1440.
1 parent 5937304 commit 1240f98

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

src/Property/CSSNamespace.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sabberworm\CSS\OutputFormat;
99
use Sabberworm\CSS\Position\Position;
1010
use Sabberworm\CSS\Position\Positionable;
11+
use Sabberworm\CSS\ShortClassNameProvider;
1112
use Sabberworm\CSS\Value\CSSString;
1213
use Sabberworm\CSS\Value\URL;
1314

@@ -18,6 +19,7 @@ class CSSNamespace implements AtRule, Positionable
1819
{
1920
use CommentContainer;
2021
use Position;
22+
use ShortClassNameProvider;
2123

2224
/**
2325
* @var CSSString|URL
@@ -102,6 +104,10 @@ public function atRuleArgs(): array
102104
*/
103105
public function getArrayRepresentation(): array
104106
{
105-
throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
107+
return [
108+
'class' => $this->getShortClassName(),
109+
'url' => $this->url->getArrayRepresentation(),
110+
'prefix' => $this->prefix,
111+
];
106112
}
107113
}

tests/Unit/Property/CSSNamespaceTest.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sabberworm\CSS\CSSList\CSSListItem;
99
use Sabberworm\CSS\Property\CSSNamespace;
1010
use Sabberworm\CSS\Value\CSSString;
11+
use Sabberworm\CSS\Value\URL;
1112

1213
/**
1314
* @covers \Sabberworm\CSS\Property\CSSNamespace
@@ -35,10 +36,79 @@ public function implementsCSSListItem(): void
3536
/**
3637
* @test
3738
*/
38-
public function getArrayRepresentationThrowsException(): void
39+
public function getArrayRepresentationIncludesClassName(): void
3940
{
40-
$this->expectException(\BadMethodCallException::class);
41+
$subject = new CSSNamespace(new CSSString('http://www.w3.org/2000/svg'));
4142

42-
$this->subject->getArrayRepresentation();
43+
$result = $subject->getArrayRepresentation();
44+
45+
self::assertSame('CSSNamespace', $result['class']);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function getArrayRepresentationIncludesUrlProvidedAsCssString(): void
52+
{
53+
$uri = 'http://www.w3.org/2000/svg';
54+
$subject = new CSSNamespace(new CSSString($uri));
55+
56+
$result = $subject->getArrayRepresentation();
57+
58+
self::assertSame(
59+
[
60+
'class' => 'CSSString',
61+
'contents' => $uri,
62+
],
63+
$result['url']
64+
);
65+
}
66+
67+
/**
68+
* @test
69+
*/
70+
public function getArrayRepresentationIncludesUrlProvidedAsUrl(): void
71+
{
72+
$uri = 'http://www.w3.org/2000/svg';
73+
$subject = new CSSNamespace(new URL(new CSSString($uri)));
74+
75+
$result = $subject->getArrayRepresentation();
76+
77+
self::assertSame(
78+
[
79+
'class' => 'URL',
80+
'uri' => [
81+
'class' => 'CSSString',
82+
'contents' => $uri,
83+
],
84+
],
85+
$result['url']
86+
);
87+
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function getArrayRepresentationIncludesPrefixForNullPrefix(): void
93+
{
94+
$subject = new CSSNamespace(new CSSString('http://www.w3.org/2000/svg'), null);
95+
96+
$result = $subject->getArrayRepresentation();
97+
98+
self::assertNull($result['prefix']);
99+
}
100+
101+
/**
102+
* @test
103+
*/
104+
public function getArrayRepresentationIncludesPrefixForStringPrefix(): void
105+
{
106+
$prefix = 'hello';
107+
108+
$subject = new CSSNamespace(new CSSString('http://www.w3.org/2000/svg'), $prefix);
109+
110+
$result = $subject->getArrayRepresentation();
111+
112+
self::assertSame($prefix, $result['prefix']);
43113
}
44114
}

0 commit comments

Comments
 (0)