2014-10-12 22:27:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MatthiasMullie\Minify;
|
2015-09-04 11:02:35 +02:00
|
|
|
use MatthiasMullie\Scrapbook\Adapters\MemoryStore;
|
|
|
|
use MatthiasMullie\Scrapbook\Psr6\Pool;
|
2014-10-12 22:27:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests common functions of abstract Minify class by using JS implementation.
|
|
|
|
*/
|
|
|
|
class CommonTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function construct()
|
|
|
|
{
|
2015-03-06 13:17:33 +01:00
|
|
|
$path1 = __DIR__.'/sample/source/script1.js';
|
|
|
|
$path2 = __DIR__.'/sample/source/script2.js';
|
2014-10-12 22:27:51 +02:00
|
|
|
$content1 = file_get_contents($path1);
|
|
|
|
$content2 = file_get_contents($path2);
|
|
|
|
|
|
|
|
// 1 source in constructor
|
|
|
|
$minifier = new Minify\JS($content1);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
|
|
|
$this->assertEquals($content1, $result);
|
|
|
|
|
|
|
|
// multiple sources in constructor
|
|
|
|
$minifier = new Minify\JS($content1, $content2);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
2015-03-06 13:17:33 +01:00
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
2014-10-12 22:27:51 +02:00
|
|
|
|
|
|
|
// file in constructor
|
|
|
|
$minifier = new Minify\JS($path1);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
|
|
|
$this->assertEquals($content1, $result);
|
|
|
|
|
|
|
|
// multiple files in constructor
|
|
|
|
$minifier = new Minify\JS($path1, $path2);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
2015-03-06 13:17:33 +01:00
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
2014-10-12 22:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function add()
|
|
|
|
{
|
2015-03-06 13:17:33 +01:00
|
|
|
$path1 = __DIR__.'/sample/source/script1.js';
|
|
|
|
$path2 = __DIR__.'/sample/source/script2.js';
|
2014-10-12 22:27:51 +02:00
|
|
|
$content1 = file_get_contents($path1);
|
|
|
|
$content2 = file_get_contents($path2);
|
|
|
|
|
|
|
|
// 1 source in add
|
|
|
|
$minifier = new Minify\JS();
|
|
|
|
$minifier->add($content1);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
|
|
|
$this->assertEquals($content1, $result);
|
|
|
|
|
|
|
|
// multiple sources in add
|
|
|
|
$minifier = new Minify\JS();
|
|
|
|
$minifier->add($content1);
|
|
|
|
$minifier->add($content2);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
2015-03-06 13:17:33 +01:00
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
2014-10-12 22:27:51 +02:00
|
|
|
|
|
|
|
// file in add
|
|
|
|
$minifier = new Minify\JS();
|
|
|
|
$minifier->add($path1);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
|
|
|
$this->assertEquals($content1, $result);
|
|
|
|
|
|
|
|
// multiple files in add
|
|
|
|
$minifier = new Minify\JS();
|
|
|
|
$minifier->add($path1);
|
|
|
|
$minifier->add($path2);
|
|
|
|
$result = $minifier->minify();
|
|
|
|
|
2015-03-06 13:17:33 +01:00
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
2014-10-12 22:27:51 +02:00
|
|
|
}
|
|
|
|
|
2016-01-04 14:15:31 -08:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2016-06-09 17:53:57 +02:00
|
|
|
public function loadBigString()
|
2016-01-04 14:15:31 -08:00
|
|
|
{
|
|
|
|
// content greater than PHP_MAXPATHLEN
|
|
|
|
// https://github.com/matthiasmullie/minify/issues/90
|
|
|
|
$content = rtrim(str_repeat('var a="b";', 500), ';');
|
|
|
|
|
|
|
|
$minifier = new Minify\JS($content);
|
|
|
|
|
|
|
|
$this->assertEquals($minifier->minify(), $content);
|
|
|
|
}
|
|
|
|
|
2014-10-12 22:27:51 +02:00
|
|
|
/**
|
|
|
|
* @test
|
2016-06-09 17:53:57 +02:00
|
|
|
*/
|
|
|
|
public function loadOpenBaseDirRestricted()
|
|
|
|
{
|
2016-06-09 18:05:40 +02:00
|
|
|
if (defined('HHVM_VERSION')) {
|
|
|
|
$this->markTestSkipped("Can't run tests that require forking on HHVM.");
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:53:57 +02:00
|
|
|
/*
|
|
|
|
* Testing open_basedir restrictions is rather annoying, since they can
|
|
|
|
* not be relaxed at runtime (if we tighten open_basedir for 1 test,
|
|
|
|
* it'll also apply for all the others)
|
|
|
|
* I'll run the open_basedir restriction in a separate thread instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
$pid = pcntl_fork();
|
|
|
|
if ($pid === -1) {
|
|
|
|
// can't fork, ignore this test...
|
|
|
|
} elseif ($pid === 0) {
|
|
|
|
// https://github.com/matthiasmullie/minify/issues/111
|
|
|
|
ini_set('open_basedir', __DIR__.'/../..');
|
|
|
|
|
|
|
|
// instead of displaying warnings & moving to the next test, just
|
|
|
|
// quit with the error code; the other thread will pick it up
|
|
|
|
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
|
|
|
|
// only exit if it is an error that is being reported (we don't
|
|
|
|
// want suppressed errors to register as failures, that happens
|
|
|
|
// on purpose!)
|
|
|
|
if (error_reporting() & $errno) {
|
|
|
|
exit($errno);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$minifier = new Minify\JS('/tmp/bogus/path');
|
|
|
|
$minifier->minify();
|
|
|
|
|
|
|
|
// don't keep executing the rest of the tests in this thread!
|
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
pcntl_wait($status);
|
|
|
|
|
|
|
|
$this->assertEquals($status, 0, 'open_basedir restriction caused minifier to fail');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2014-10-12 22:27:51 +02:00
|
|
|
*/
|
2015-02-12 13:34:48 +01:00
|
|
|
public function save()
|
|
|
|
{
|
2015-04-07 15:47:30 +02:00
|
|
|
$path = __DIR__.'/sample/source/script1.js';
|
|
|
|
$content = file_get_contents($path);
|
2015-03-06 13:17:33 +01:00
|
|
|
$savePath = __DIR__.'/sample/target/script1.js';
|
2014-10-12 22:27:51 +02:00
|
|
|
|
2015-04-07 15:47:30 +02:00
|
|
|
$minifier = new Minify\JS($path);
|
2014-10-12 22:27:51 +02:00
|
|
|
$minifier->minify($savePath);
|
|
|
|
|
2015-04-07 15:47:30 +02:00
|
|
|
$this->assertEquals(file_get_contents($savePath), $content);
|
|
|
|
}
|
|
|
|
|
2016-02-09 15:53:15 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*
|
2016-02-17 11:42:59 +01:00
|
|
|
* @expectedException MatthiasMullie\Minify\Exceptions\IOException
|
2016-02-09 15:53:15 +03:00
|
|
|
*/
|
|
|
|
public function checkFileOpenFail()
|
|
|
|
{
|
|
|
|
$minifier = new Minify\JS();
|
|
|
|
$wrongPath = '';
|
|
|
|
|
|
|
|
$object = new ReflectionObject($minifier);
|
|
|
|
$method = $object->getMethod('openFileForWriting');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$method->invokeArgs($minifier, array($wrongPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*
|
2016-02-17 11:42:59 +01:00
|
|
|
* @expectedException MatthiasMullie\Minify\Exceptions\IOException
|
2016-02-09 15:53:15 +03:00
|
|
|
*/
|
|
|
|
public function checkFileWriteFail()
|
|
|
|
{
|
|
|
|
$minifier = new Minify\JS();
|
|
|
|
$wrongPath = '';
|
|
|
|
|
|
|
|
$object = new ReflectionObject($minifier);
|
|
|
|
$method = $object->getMethod('writeToFile');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$method->invokeArgs($minifier, array($wrongPath, ''));
|
|
|
|
}
|
|
|
|
|
2015-04-07 15:47:30 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function gzip()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/sample/source/script1.js';
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$savePath = __DIR__.'/sample/target/script1.js.gz';
|
|
|
|
|
|
|
|
$minifier = new Minify\JS($path);
|
|
|
|
$minifier->gzip($savePath, 9);
|
|
|
|
|
|
|
|
$this->assertEquals(file_get_contents($savePath), gzencode($content, 9, FORCE_GZIP));
|
2014-10-12 22:27:51 +02:00
|
|
|
}
|
2015-05-05 16:43:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function cache()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/sample/source/script1.js';
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
|
2015-09-05 21:49:18 +02:00
|
|
|
$cache = new MemoryStore();
|
2015-05-05 16:43:08 +02:00
|
|
|
$pool = new Pool($cache);
|
|
|
|
$item = $pool->getItem('cache-script1');
|
|
|
|
|
|
|
|
$minifier = new Minify\JS($path);
|
|
|
|
$item = $minifier->cache($item);
|
|
|
|
|
|
|
|
$this->assertEquals($item->get(), $content);
|
|
|
|
}
|
2014-10-12 22:27:51 +02:00
|
|
|
}
|