Skip to content

Commit

Permalink
Add test suite (Bug #14650).
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed May 26, 2017
1 parent 8c22a1a commit 3154844
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 2 deletions.
34 changes: 32 additions & 2 deletions framework/JavascriptMinify/package.xml
Expand Up @@ -10,7 +10,7 @@
<email>slusarz@horde.org</email>
<active>yes</active>
</lead>
<date>2016-03-08</date>
<date>2017-05-26</date>
<version>
<release>1.1.4</release>
<api>1.1.0</api>
Expand Down Expand Up @@ -47,6 +47,25 @@
<file name="JavascriptMinify.php" role="php" />
</dir> <!-- /lib/Horde -->
</dir> <!-- /lib -->
<dir name="test">
<dir name="Horde">
<dir name="JavascriptMinify">
<dir name="fixtures">
<file name="one.js" role="test" />
<file name="two.js" role="test" />
</dir> <!-- /test/Horde/JavascriptMinify/fixtures -->
<file name="AllTests.php" role="test" />
<file name="bootstrap.php" role="test" />
<file name="ClosureTest.php" role="test" />
<file name="conf.php.dist" role="test" />
<file name="NullTest.php" role="test" />
<file name="phpunit.xml" role="test" />
<file name="TestBase.php" role="test" />
<file name="UglifyjsTest.php" role="test" />
<file name="YuiTest.php" role="test" />
</dir> <!-- /test/Horde/JavascriptMinify -->
</dir> <!-- /test/Horde -->
</dir> <!-- /test -->
</dir> <!-- / -->
</contents>
<dependencies>
Expand Down Expand Up @@ -92,6 +111,17 @@
<install as="Horde/JavascriptMinify/Uglifyjs.php" name="lib/Horde/JavascriptMinify/Uglifyjs.php" />
<install as="Horde/JavascriptMinify/Yui.php" name="lib/Horde/JavascriptMinify/Yui.php" />
<install as="Horde/JavascriptMinify/Util/Cmdline.php" name="lib/Horde/JavascriptMinify/Util/Cmdline.php" />
<install as="Horde/JavascriptMinify/AllTests.php" name="test/Horde/JavascriptMinify/AllTests.php" />
<install as="Horde/JavascriptMinify/bootstrap.php" name="test/Horde/JavascriptMinify/bootstrap.php" />
<install as="Horde/JavascriptMinify/ClosureTest.php" name="test/Horde/JavascriptMinify/ClosureTest.php" />
<install as="Horde/JavascriptMinify/conf.php.dist" name="test/Horde/JavascriptMinify/conf.php.dist" />
<install as="Horde/JavascriptMinify/NullTest.php" name="test/Horde/JavascriptMinify/NullTest.php" />
<install as="Horde/JavascriptMinify/phpunit.xml" name="test/Horde/JavascriptMinify/phpunit.xml" />
<install as="Horde/JavascriptMinify/TestBase.php" name="test/Horde/JavascriptMinify/TestBase.php" />
<install as="Horde/JavascriptMinify/UglifyjsTest.php" name="test/Horde/JavascriptMinify/UglifyjsTest.php" />
<install as="Horde/JavascriptMinify/YuiTest.php" name="test/Horde/JavascriptMinify/YuiTest.php" />
<install as="Horde/JavascriptMinify/fixtures/one.js" name="test/Horde/JavascriptMinify/fixtures/one.js" />
<install as="Horde/JavascriptMinify/fixtures/two.js" name="test/Horde/JavascriptMinify/fixtures/two.js" />
</filelist>
</phprelease>
<changelog>
Expand Down Expand Up @@ -169,7 +199,7 @@
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2016-03-08</date>
<date>2017-05-26</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
Expand Down
@@ -0,0 +1,3 @@
<?php
require_once 'Horde/Test/AllTests.php';
Horde_Test_AllTests::init(__FILE__)->run();
@@ -0,0 +1,67 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/

/**
* Tests the Closure backend.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/
class Horde_JavascriptMinify_ClosureTest
extends Horde_JavascriptMinify_TestBase
{
protected $_config;

public function setUp()
{
$this->_config = self::getConfig(
'JAVASCRIPTMINIFY_CLOSURE_TEST_CONFIG',
__DIR__
);
if (!$this->_config ||
empty($this->_config['javascriptminify']['closure'])) {
$this->markTestSkipped('Closure compressor not configured');
}
}

public function testMinify()
{
$this->_minify();
}

public function testSourcemap()
{
$this->_sourcemap();
}

public function testToString()
{
$this->_toString();
}

protected function _getMinifier($files = false)
{
$opts = $this->_config['javascriptminify']['closure'];
if ($files) {
$opts['sourcemap'] = 'https://www.example.com/js/sourcemap';
}
return new Horde_JavascriptMinify_Closure(
$this->_getFixture($files), $opts
);
}
}
@@ -0,0 +1,37 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/

/**
* Tests the Null backend.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/
class Horde_JavascriptMinify_NullTest
extends Horde_JavascriptMinify_TestBase
{
public function testToString()
{
$this->_toString();
}

protected function _getMinifier()
{
return new Horde_JavascriptMinify_Null($this->_getFixture());
}
}
100 changes: 100 additions & 0 deletions framework/JavascriptMinify/test/Horde/JavascriptMinify/TestBase.php
@@ -0,0 +1,100 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/

/**
* Base class for backend unit tests.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/
abstract class Horde_JavascriptMinify_TestBase extends Horde_Test_Case
{
public function setUp()
{
}

protected function _minify()
{
$minifier = $this->_getMinifier();
$minified = $minifier->minify();
$this->assertNotEmpty($minified);
$this->assertLessThan(
strlen($this->_getFixture()),
strlen($minified)
);

$minifier = $this->_getMinifier(true);
$minified = $minifier->minify();
$this->assertNotEmpty($minified);
$this->assertLessThan(
strlen(implode('', array_map('file_get_contents', $this->_getFixture(true)))),
strlen($minified)
);
}

protected function _sourcemap()
{
$minifier = $this->_getMinifier(true, true);
$minifier->minify();
$sourcemap = $minifier->sourcemap();
$this->assertNotEmpty($sourcemap);
$this->assertNotEmpty(json_decode($sourcemap));
}

protected function _toString()
{
$minifier = $this->_getMinifier();
$this->assertEquals($minifier->minify(), (string)$minifier);
}

abstract protected function _getMinifier();

protected function _getFixture($files = false)
{
if ($files) {
return array(
'https://www.example.com/js/one.js' => __DIR__ . '/fixtures/one.js',
'https://www.example.com/js/two.js' => __DIR__ . '/fixtures/two.js',
);
}

return <<<JAVASCRIPT
/**
* Some example code.
*
*/
var Foo = {
doit: function(foo)
{
var test, xyz = 1;
this.callme();
xyz++;
test = 'Bar';
alert(test + foo);
}
};
Foo.doit("Boo");
JAVASCRIPT;
}

public function tearDown()
{
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/

/**
* Tests the UglifyJS backend.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package JavascriptMinify
* @subpackage UnitTests
*/
class Horde_JavascriptMinify_UglifyjsTest
extends Horde_JavascriptMinify_TestBase
{
protected $_config;

public function setUp()
{
$this->_config = self::getConfig(
'JAVASCRIPTMINIFY_UGLIFYJS_TEST_CONFIG',
__DIR__
);
if (!$this->_config ||
empty($this->_config['javascriptminify']['uglifyjs'])) {
$this->markTestSkipped('UglifyJS compressor not configured');
}
}

public function testMinify()
{
$this->_minify();
}

public function testSourcemap()
{
$this->_sourcemap();
}

public function testToString()
{
$this->_toString();
}

protected function _getMinifier($files = false)
{
$opts = $this->_config['javascriptminify']['uglifyjs'];
if ($files) {
$opts['sourcemap'] = 'https://www.example.com/js/sourcemap';
}
return new Horde_JavascriptMinify_Uglifyjs(
$this->_getFixture($files), $opts
);
}
}

0 comments on commit 3154844

Please sign in to comment.