minify/tests/css/CSSTest.php

305 lines
8.3 KiB
PHP
Raw Normal View History

<?php
use MatthiasMullie\Minify;
/**
* CSS minifier test case.
*/
class CSSTest extends PHPUnit_Framework_TestCase
{
2014-10-12 21:10:12 +02:00
/**
* @var Minify\CSS
*/
private $minifier;
/**
* Prepares the environment before running a test.
*/
protected function setUp()
{
parent::setUp();
$this->minifier = new Minify\CSS();
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown()
{
$this->minifier = null;
parent::tearDown();
}
/**
2014-10-12 21:10:12 +02:00
* Test CSS minifier rules, provided by dataProvider.
*
* @test
* @dataProvider dataProvider
*/
public function minify($input, $expected)
{
$this->minifier->add($input);
2014-10-07 18:21:22 +02:00
$result = $this->minifier->minify();
$this->assertEquals($expected, $result);
}
/**
2014-10-12 21:10:12 +02:00
* Test conversion of relative paths, provided by dataProviderPaths.
*
* @test
* @dataProvider dataProviderPaths
*/
public function convertRelativePath($source, $target, $expected)
2014-10-09 08:56:14 +02:00
{
$this->minifier->add($source);
$result = $this->minifier->minify($target);
$this->assertEquals($expected, $result);
}
/**
2014-10-12 21:10:12 +02:00
* Test minifier import configuration methods.
*
2014-10-12 21:10:12 +02:00
* @test
*/
public function setConfig() {
$this->minifier->setMaxImportSize(10);
$this->minifier->setImportExtensions(array('gif' => 'data:image/gif'));
$object = new ReflectionObject($this->minifier);
$property = $object->getProperty('maxImportSize');
$property->setAccessible(true);
$this->assertEquals($property->getValue($this->minifier), 10);
$property = $object->getProperty('importExtensions');
$property->setAccessible(true);
$this->assertEquals($property->getValue($this->minifier), array('gif' => 'data:image/gif'));
}
/**
* @return array [input, expected result]
*/
public function dataProvider()
{
$tests = array();
2014-10-12 21:55:30 +02:00
// try importing, with both @import syntax types & media queries
$tests[] = array(
__DIR__ . '/sample/combine_imports/index.css',
'body{color:red}',
);
2014-10-12 21:10:12 +02:00
$tests[] = array(
__DIR__ . '/sample/combine_imports/index2.css',
'body{color:red}',
);
$tests[] = array(
__DIR__ . '/sample/combine_imports/index3.css',
'body{color:red}body{color:red}',
);
2014-10-12 21:55:30 +02:00
$tests[] = array(
__DIR__ . '/sample/combine_imports/index4.css',
'@media only screen{body{color:red}}@media only screen{body{color:red}}',
);
2014-10-20 11:08:51 +02:00
$tests[] = array(
__DIR__ . '/sample/combine_imports/index5.css',
'body{color:red}body{color:red}',
);
$tests[] = array(
__DIR__ . '/sample/combine_imports/index6a.css',
'body{color:red}',
);
// shorthand hex color codes
$tests[] = array(
'color:#FF00FF;',
'color:#F0F;',
);
// import files
$tests[] = array(
__DIR__ . '/sample/import_files/index.css',
'body{background:url(data:image/png;base64,' . base64_encode(file_get_contents(__DIR__ . '/sample/import_files/file.png')) . ')}',
);
// strip comments
$tests[] = array(
'/* This is a CSS comment */',
'',
);
// strip whitespace
$tests[] = array(
'body { color: red; }',
'body{color:red}',
);
// whitespace inside strings shouldn't be replaced
$tests[] = array(
'content:"preserve whitespace"',
'content:"preserve whitespace"',
);
$tests[] = array(
'html
body {
color: red;
}',
'html body{color:red}'
);
$tests[] = array(
'
p * i , html
/* remove spaces */
/* " comments have no escapes \*/
body/* keep */ /* space */p,
p [ remove ~= " spaces " ] :nth-child( 3 + 2n ) > b span i , div::after
{
/* comment */
background : url( " /* string */ " ) blue !important ;
content : " escapes \" allowed \\" ;
width: calc( 100% - 3em + 5px ) ;
margin-top : 0;
margin-bottom : 0;
margin-left : 10px;
margin-right : 10px;
}
',
'p * i,html body p,p [remove~=" spaces "] :nth-child(3+2n)>b span i,div::after{background:url(" /* string */ ") blue!important;content:" escapes \" allowed \\";width:calc(100% - 3em + 5px);margin-top:0;margin-bottom:0;margin-left:10px;margin-right:10px}'
);
2013-04-22 10:19:35 +02:00
/*
* https://github.com/forkcms/forkcms/issues/387
*
* CSS backslash.
* * Backslash escaped by backslash in CSS
* * Double CSS backslashed escaped twice for in PHP string
*/
$tests[] = array(
'.iconic.map-pin:before { content: "\\\\"; }',
'.iconic.map-pin:before{content:"\\\\"}',
);
2014-10-20 11:08:51 +02:00
// strip BOM
$tests[] = array(
__DIR__ . '/sample/bom/bom.css',
'body{color:red}',
);
// https://github.com/matthiasmullie/minify/issues/22
$tests[] = array(
'p { background-position: -0px -64px; }',
2014-11-20 21:42:49 +01:00
'p{background-position:0 -64px}',
);
2014-12-04 20:08:57 +01:00
// https://github.com/matthiasmullie/minify/issues/23
$tests[] = array(
'ul.pagination {
display: block;
min-height: 1.5rem;
margin-left: -0.3125rem;
}',
'ul.pagination{display:block;min-height:1.5rem;margin-left:-0.3125rem}',
);
// edge cases for stripping zeroes
$tests[] = array(
'p { margin: -0.0rem; }',
'p{margin:0}',
);
$tests[] = array(
'p { margin: -0.01rem; }',
'p{margin:-0.01rem}',
2014-12-04 20:08:57 +01:00
);
$tests[] = array(
'p { margin: .0; }',
'p{margin:0}',
);
2014-12-07 18:35:07 +01:00
$tests[] = array(
'p { margin: 00px; }',
'p{margin:0}',
);
$tests[] = array(
'p.class00 { background-color: #000000; color: #000; }',
'p.class00{background-color:#000;color:#000}',
);
2014-12-04 20:08:57 +01:00
// https://github.com/matthiasmullie/minify/issues/24
$tests[] = array(
'.col-1-1 { width: 100.00%; }',
'.col-1-1{width:100%}',
);
// https://github.com/matthiasmullie/minify/issues/25
$tests[] = array(
'p { background-color: #000000; color: #000; }',
'p{background-color:#000;color:#000}',
);
return $tests;
}
2014-10-12 21:10:12 +02:00
/**
* @return array [input, expected result]
*/
2014-10-09 08:56:14 +02:00
public function dataProviderPaths()
{
$tests = array();
$source = __DIR__ . '/sample/convert_relative_path/source';
$target = __DIR__ . '/sample/convert_relative_path/target';
// external link
$tests[] = array(
$source . '/external.css',
$target . '/external.css',
file_get_contents($source . '/external.css'),
);
// absolute path
$tests[] = array(
$source . '/absolute.css',
$target . '/absolute.css',
file_get_contents($source . '/absolute.css'),
);
2014-10-12 22:07:59 +02:00
// relative paths
$tests[] = array(
$source . '/relative.css',
$target . '/relative.css',
2014-10-13 13:46:16 +02:00
'@import url(image.jpg);',
2014-10-12 22:07:59 +02:00
);
$tests[] = array(
$source . '/../source/relative.css',
2014-10-12 22:07:59 +02:00
$target . '/target/relative.css',
2014-10-13 13:46:16 +02:00
'@import url(../image.jpg);',
);
$sourceRelative = 'tests/css/sample/convert_relative_path/source';
$targetRelative = 'tests/css/sample/convert_relative_path/target';
// from and/or to are relative links
$tests[] = array(
$sourceRelative . '/relative.css',
$target . '/relative.css',
'@import url(image.jpg);',
);
$tests[] = array(
$source . '/relative.css',
$targetRelative . '/relative.css',
'@import url(image.jpg);',
);
$tests[] = array(
$sourceRelative . '/relative.css',
$targetRelative . '/relative.css',
'@import url(image.jpg);',
2014-10-12 22:07:59 +02:00
);
return $tests;
}
}