3 namespace Pelago\Tests\Unit\Emogrifier;
5 use Pelago\Emogrifier\CssConcatenator;
10 * @author Jake Hotson <jake.github@qzdesign.co.uk>
12 class CssConcatenatorTest extends \PHPUnit_Framework_TestCase
15 * @var CssConcatenator
17 private $subject = null;
22 protected function setUp()
24 $this->subject = new CssConcatenator();
30 public function getCssInitiallyReturnsEmptyString()
32 $result = $this->subject->getCss();
34 static::assertSame('', $result);
40 public function appendSetsFirstRule()
42 $this->subject->append(['p'], 'color: green;');
44 $result = $this->subject->getCss();
46 static::assertSame('p{color: green;}', $result);
52 public function appendWithMediaQuerySetsFirstRuleInMediaRule()
54 $this->subject->append(['p'], 'color: green;', '@media screen');
56 $result = $this->subject->getCss();
58 static::assertSame('@media screen{p{color: green;}}', $result);
64 public function equivalentSelectorsDataProvider()
67 'one selector' => [['p'], ['p']],
72 'two selectors in different order' => [
82 * @param string[] $selectors1
83 * @param string[] $selectors2
85 * @dataProvider equivalentSelectorsDataProvider
87 public function appendCombinesRulesWithEquivalentSelectors(array $selectors1, array $selectors2)
89 $this->subject->append($selectors1, 'color: green;');
90 $this->subject->append($selectors2, 'font-size: 16px;');
92 $result = $this->subject->getCss();
94 $expectedResult = \implode(',', $selectors1) . '{color: green;font-size: 16px;}';
96 static::assertSame($expectedResult, $result);
102 public function appendInsertsSemicolonCombiningRulesWithoutTrailingSemicolon()
104 $this->subject->append(['p'], 'color: green');
105 $this->subject->append(['p'], 'font-size: 16px');
107 $result = $this->subject->getCss();
109 static::assertSame('p{color: green;font-size: 16px}', $result);
115 public function differentSelectorsDataProvider()
118 'single selectors' => [
123 'single selector and an entirely different pair' => [
128 'single selector and a superset pair' => [
133 'pair of selectors and an entirely different single' => [
138 'pair of selectors and a subset single' => [
143 'entirely different pairs of selectors' => [
146 ['p', 'ul', 'ol', 'h1'],
148 'pairs of selectors with one common' => [
159 * @param string[] $selectors1
160 * @param string[] $selectors2
161 * @param string[] $combinedSelectors
163 * @dataProvider differentSelectorsDataProvider
165 public function appendCombinesSameRulesWithDifferentSelectors(
168 array $combinedSelectors
170 $this->subject->append($selectors1, 'color: green;');
171 $this->subject->append($selectors2, 'color: green;');
173 $result = $this->subject->getCss();
175 $expectedResult = \implode(',', $combinedSelectors) . '{color: green;}';
177 static::assertSame($expectedResult, $result);
183 * @param string[] $selectors1
184 * @param string[] $selectors2
186 * @dataProvider differentSelectorsDataProvider
188 public function appendNotCombinesDifferentRulesWithDifferentSelectors(array $selectors1, array $selectors2)
190 $this->subject->append($selectors1, 'color: green;');
191 $this->subject->append($selectors2, 'font-size: 16px;');
193 $result = $this->subject->getCss();
195 $expectedResult = \implode(',', $selectors1) . '{color: green;}'
196 . \implode(',', $selectors2) . '{font-size: 16px;}';
198 static::assertSame($expectedResult, $result);
204 public function appendCombinesRulesForSameMediaQueryInMediaRule()
206 $this->subject->append(['p'], 'color: green;', '@media screen');
207 $this->subject->append(['ul'], 'font-size: 16px;', '@media screen');
209 $result = $this->subject->getCss();
211 static::assertSame('@media screen{p{color: green;}ul{font-size: 16px;}}', $result);
217 * @param string[] $selectors1
218 * @param string[] $selectors2
220 * @dataProvider equivalentSelectorsDataProvider
222 public function appendCombinesRulesWithEquivalentSelectorsWithinMediaRule(array $selectors1, array $selectors2)
224 $this->subject->append($selectors1, 'color: green;', '@media screen');
225 $this->subject->append($selectors2, 'font-size: 16px;', '@media screen');
227 $result = $this->subject->getCss();
229 $expectedResult = '@media screen{' . \implode(',', $selectors1) . '{color: green;font-size: 16px;}}';
231 static::assertSame($expectedResult, $result);
237 * @param string[] $selectors1
238 * @param string[] $selectors2
239 * @param string[] $combinedSelectors
241 * @dataProvider differentSelectorsDataProvider
243 public function appendCombinesSameRulesWithDifferentSelectorsWithinMediaRule(
246 array $combinedSelectors
248 $this->subject->append($selectors1, 'color: green;', '@media screen');
249 $this->subject->append($selectors2, 'color: green;', '@media screen');
251 $result = $this->subject->getCss();
253 $expectedResult = '@media screen{' . \implode(',', $combinedSelectors) . '{color: green;}}';
255 static::assertSame($expectedResult, $result);
261 public function appendNotCombinesRulesForDifferentMediaQueryInMediaRule()
263 $this->subject->append(['p'], 'color: green;', '@media screen');
264 $this->subject->append(['p'], 'color: green;', '@media print');
266 $result = $this->subject->getCss();
268 static::assertSame('@media screen{p{color: green;}}@media print{p{color: green;}}', $result);
274 public function combinableRulesDataProvider()
277 'same selectors' => [['p'], 'color: green;', ['p'], 'font-size: 16px;', ''],
278 'same declarations block' => [['p'], 'color: green;', ['ul'], 'color: green;', ''],
279 'same media query' => [['p'], 'color: green;', ['ul'], 'font-size: 16px;', '@media screen'],
286 * @param array $rule1Selectors
287 * @param string $rule1DeclarationsBlock
288 * @param array $rule2Selectors
289 * @param string $rule2DeclarationsBlock
290 * @param string $media
292 * @dataProvider combinableRulesDataProvider
294 public function appendNotCombinesNonadjacentRules(
295 array $rule1Selectors,
296 $rule1DeclarationsBlock,
297 array $rule2Selectors,
298 $rule2DeclarationsBlock,
301 $this->subject->append($rule1Selectors, $rule1DeclarationsBlock, $media);
302 $this->subject->append(['.intervening'], '-intervening-property: 0;');
303 $this->subject->append($rule2Selectors, $rule2DeclarationsBlock, $media);
305 $result = $this->subject->getCss();
307 $expectedRule1Css = \implode(',', $rule1Selectors) . '{' . $rule1DeclarationsBlock . '}';
308 $expectedRule2Css = \implode(',', $rule2Selectors) . '{' . $rule2DeclarationsBlock . '}';
310 $expectedRule1Css = $media . '{' . $expectedRule1Css . '}';
311 $expectedRule2Css = $media . '{' . $expectedRule2Css . '}';
313 $expectedResult = $expectedRule1Css . '.intervening{-intervening-property: 0;}' . $expectedRule2Css;
315 static::assertSame($expectedResult, $result);