Skip to content

Commit 641e391

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

2 files changed

Lines changed: 83 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: 76 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,82 @@ 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+
$uriAsCssString = new CSSString($uri);
55+
$subject = new CSSNamespace($uriAsCssString);
56+
57+
$result = $subject->getArrayRepresentation();
58+
59+
self::assertSame(
60+
[
61+
'class' => 'CSSString',
62+
'contents' => $uri,
63+
],
64+
$result['url']
65+
);
66+
}
67+
68+
/**
69+
* @test
70+
*/
71+
public function getArrayRepresentationIncludesUrlProvidedAsUrl(): void
72+
{
73+
$uri = 'http://www.w3.org/2000/svg';
74+
$uriAsCssString = new CSSString($uri);
75+
$url = new URL($uriAsCssString);
76+
$subject = new CSSNamespace($url);
77+
78+
$result = $subject->getArrayRepresentation();
79+
80+
self::assertSame(
81+
[
82+
'class' => 'URL',
83+
'uri' => [
84+
'class' => 'CSSString',
85+
'contents' => $uri,
86+
],
87+
],
88+
$result['url']
89+
);
90+
}
91+
92+
/**
93+
* @test
94+
*/
95+
public function getArrayRepresentationIncludesPrefixForNullPrefix(): void
96+
{
97+
$subject = new CSSNamespace(new CSSString('http://www.w3.org/2000/svg'), null);
98+
99+
$result = $subject->getArrayRepresentation();
100+
101+
self::assertNull($result['prefix']);
102+
}
103+
104+
/**
105+
* @test
106+
*/
107+
public function getArrayRepresentationIncludesPrefixForStringPrefix(): void
108+
{
109+
$prefix = 'hello';
110+
111+
$subject = new CSSNamespace(new CSSString('http://www.w3.org/2000/svg'), $prefix);
112+
113+
$result = $subject->getArrayRepresentation();
114+
115+
self::assertSame($prefix, $result['prefix']);
43116
}
44117
}

0 commit comments

Comments
 (0)