Add unit tests for concatCSS() and concatJS()

This commit is contained in:
Kijin Sung 2016-05-05 13:45:57 +09:00
parent 7363f71d4a
commit b5a10e850f
10 changed files with 120 additions and 2 deletions

View file

@ -337,7 +337,7 @@ class Formatter
unset($path_converter);
if ($media !== null)
{
$content = "@media $media {\n\n$content\n\n}";
$content = "@media $media {\n\n" . trim($content) . "\n\n}";
}
$original_filename = starts_with(\RX_BASEDIR, $filename) ? substr($filename, strlen(\RX_BASEDIR)) : $filename;
$result .= '/* Original file: ' . $original_filename . ' */' . "\n\n" . trim($content) . "\n\n";
@ -374,7 +374,7 @@ class Formatter
$content = utf8_clean(file_get_contents($filename));
if ($targetie !== null)
{
$content = 'if (' . self::convertIECondition($targetie) . ') {' . "\n\n" . $content . "\n\n" . '}';
$content = 'if (' . self::convertIECondition($targetie) . ') {' . "\n\n" . trim($content) . ";\n\n" . '}';
}
$original_filename = starts_with(\RX_BASEDIR, $filename) ? substr($filename, strlen(\RX_BASEDIR)) : $filename;
$result .= '/* Original file: ' . $original_filename . ' */' . "\n\n" . trim($content) . ";\n\n";

View file

@ -0,0 +1,4 @@
@charset "UTF-8";
.rhymix {
background: url("foo/bar.jpg");
}

View file

@ -0,0 +1,6 @@
(function($) {
$(".foo").click(function(event) {
event.preventDefault();
$(this).attr("bar", "baz");
});
})(jQuery);

View file

@ -0,0 +1,7 @@
@charset "UTF-8";
.wordpress {
border-radius: 4px;
}
.xpressengine {
margin: 320px;
}

View file

@ -0,0 +1,3 @@
(function($) {
$(".xe").hide();
})(jQuery);

View file

@ -0,0 +1,16 @@
/* Original file: tests/_data/formatter/concat.source1.css */
@charset "UTF-8";
.rhymix {
background: url("../_data/formatter/foo/bar.jpg");
}
/* Original file: tests/_data/formatter/concat.source2.css */
@charset "UTF-8";
.wordpress {
border-radius: 4px;
}
.xpressengine {
margin: 320px;
}

View file

@ -0,0 +1,14 @@
/* Original file: tests/_data/formatter/concat.source1.js */
(function($) {
$(".foo").click(function(event) {
event.preventDefault();
$(this).attr("bar", "baz");
});
})(jQuery);;
/* Original file: tests/_data/formatter/concat.source2.js */
(function($) {
$(".xe").hide();
})(jQuery);;

View file

@ -0,0 +1,20 @@
/* Original file: tests/_data/formatter/concat.source1.css */
@media screen and (max-width: 640px) {
@charset "UTF-8";
.rhymix {
background: url("../_data/formatter/foo/bar.jpg");
}
}
/* Original file: tests/_data/formatter/concat.source2.css */
@charset "UTF-8";
.wordpress {
border-radius: 4px;
}
.xpressengine {
margin: 320px;
}

View file

@ -0,0 +1,18 @@
/* Original file: tests/_data/formatter/concat.source1.js */
(function($) {
$(".foo").click(function(event) {
event.preventDefault();
$(this).attr("bar", "baz");
});
})(jQuery);;
/* Original file: tests/_data/formatter/concat.source2.js */
if ((/MSIE (\d+)/.exec(window.navigator.userAgent) && /MSIE (\d+)/.exec(window.navigator.userAgent)[1] >= 6) && (/MSIE (\d+)/.exec(window.navigator.userAgent) && /MSIE (\d+)/.exec(window.navigator.userAgent)[1] <= 8)) {
(function($) {
$(".xe").hide();
})(jQuery);;
};

View file

@ -132,6 +132,36 @@ class FormatterTest extends \Codeception\TestCase\Test
unlink($test_target);
}
public function testConcatCSS()
{
$source1 = \RX_BASEDIR . 'tests/_data/formatter/concat.source1.css';
$source2 = \RX_BASEDIR . 'tests/_data/formatter/concat.source2.css';
$real_target1 = \RX_BASEDIR . 'tests/_data/formatter/concat.target1.css';
$real_target2 = \RX_BASEDIR . 'tests/_data/formatter/concat.target2.css';
$test_target = \RX_BASEDIR . 'tests/_output/concat.target.css';
$test_without_media_query = Rhymix\Framework\Formatter::concatCSS(array($source1, $source2), $test_target);
$this->assertEquals(trim(file_get_contents($real_target1)), trim($test_without_media_query));
$test_with_media_query = Rhymix\Framework\Formatter::concatCSS(array(array($source1, 'screen and (max-width: 640px)'), $source2), $test_target);
$this->assertEquals(trim(file_get_contents($real_target2)), trim($test_with_media_query));
}
public function testConcatJS()
{
$source1 = \RX_BASEDIR . 'tests/_data/formatter/concat.source1.js';
$source2 = \RX_BASEDIR . 'tests/_data/formatter/concat.source2.js';
$real_target1 = \RX_BASEDIR . 'tests/_data/formatter/concat.target1.js';
$real_target2 = \RX_BASEDIR . 'tests/_data/formatter/concat.target2.js';
$test_target = \RX_BASEDIR . 'tests/_output/concat.target.js';
$test_without_targetie = Rhymix\Framework\Formatter::concatJS(array($source1, $source2), $test_target);
$this->assertEquals(trim(file_get_contents($real_target1)), trim($test_without_targetie));
$test_with_targetie = Rhymix\Framework\Formatter::concatJS(array($source1, array($source2, '(gte IE 6) & (lte IE 8)')), $test_target);
$this->assertEquals(trim(file_get_contents($real_target2)), trim($test_with_targetie));
}
public function testConvertIECondition()
{
$this->assertEquals('window.navigator.userAgent.match(/MSIE\s/)', Rhymix\Framework\Formatter::convertIECondition('IE'));