Skip to content

Commit f1eab3e

Browse files
author
Raphael Schweikert
committed
added test for color parsing
1 parent b9bd23d commit f1eab3e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tests/CSSParserTests.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,45 @@ function testCssFiles() {
2525
closedir($rHandle);
2626
}
2727
}
28+
29+
/**
30+
* @depends testCssFiles
31+
*/
32+
function testColorParsing() {
33+
$oDoc = $this->parsedStructureForFile('colortest');
34+
foreach($oDoc->getAllRuleSets() as $oRuleSet) {
35+
if(!$oRuleSet instanceof CSSSelector) {
36+
continue;
37+
}
38+
$aSelector = $oRuleSet->getSelector();
39+
if($aSelector[0] === '#mine') {
40+
$aColorRule = $oRuleSet->getRules('color');
41+
$aValues = $aColorRule['color']->getValues();
42+
$this->assertSame('red', $aValues[0][0]);
43+
$aColorRule = $oRuleSet->getRules('background-');
44+
$aValues = $aColorRule['background-color']->getValues();
45+
$this->assertEquals(array('r' => new CSSSize(35.0), 'g' => new CSSSize(35.0), 'b' => new CSSSize(35.0)), $aValues[0][0]->getColor());
46+
$aColorRule = $oRuleSet->getRules('border-color');
47+
$aValues = $aColorRule['border-color']->getValues();
48+
$this->assertEquals(array('r' => new CSSSize(10.0), 'g' => new CSSSize(100.0), 'b' => new CSSSize(230.0), 'a' => new CSSSize(0.3)), $aValues[0][0]->getColor());
49+
$aColorRule = $oRuleSet->getRules('outline-color');
50+
$aValues = $aColorRule['outline-color']->getValues();
51+
$this->assertEquals(array('r' => new CSSSize(34.0), 'g' => new CSSSize(34.0), 'b' => new CSSSize(34.0)), $aValues[0][0]->getColor());
52+
}
53+
}
54+
foreach($oDoc->getAllValues('background-') as $oColor) {
55+
if($oColor->getColorDescription() === 'hsl') {
56+
$this->assertEquals(array('h' => new CSSSize(220.0), 's' => new CSSSize(10.0), 'l' => new CSSSize(220.0)), $oColor->getColor());
57+
}
58+
}
59+
foreach($oDoc->getAllValues('color') as $sColor) {
60+
$this->assertSame('red', $sColor);
61+
}
62+
}
63+
64+
function parsedStructureForFile($sFileName) {
65+
$sFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR."$sFileName.css";
66+
$oParser = new CSSParser(file_get_contents($sFile));
67+
return $oParser->parse();
68+
}
2869
}

tests/files/colortest.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#mine {
2+
color: red;
3+
border-color: rgba(10, 100, 230, 0.3);
4+
outline-color: #222;
5+
background-color: #232323;
6+
}
7+
8+
#yours {
9+
background-color: hsl(220, 10, 220);
10+
}

0 commit comments

Comments
 (0)