mirror of
https://github.com/matthiasmullie/minify.git
synced 2025-02-24 06:43:05 +01:00
I admit, having just this 1 Exception class in the root of the project was a bad idea... But now I can't take it away without breaking backwards compatibility: people could be catching errors of this class. So all new Exception classes have to inherit from this old one (it's not worth breaking BC over, imo) Also renamed the new ones to be "Exceptions" (plural), to avoid confusing that namespace with the original exception class name.
184 lines
4.7 KiB
PHP
184 lines
4.7 KiB
PHP
<?php
|
|
|
|
use MatthiasMullie\Minify;
|
|
use MatthiasMullie\Scrapbook\Adapters\MemoryStore;
|
|
use MatthiasMullie\Scrapbook\Psr6\Pool;
|
|
|
|
/**
|
|
* Tests common functions of abstract Minify class by using JS implementation.
|
|
*/
|
|
class CommonTest extends PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* @test
|
|
*/
|
|
public function construct()
|
|
{
|
|
$path1 = __DIR__.'/sample/source/script1.js';
|
|
$path2 = __DIR__.'/sample/source/script2.js';
|
|
$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();
|
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
|
|
|
// 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();
|
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function add()
|
|
{
|
|
$path1 = __DIR__.'/sample/source/script1.js';
|
|
$path2 = __DIR__.'/sample/source/script2.js';
|
|
$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();
|
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
|
|
|
// 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();
|
|
|
|
$this->assertEquals($content1.';'.$content2, $result);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function load()
|
|
{
|
|
// 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);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function save()
|
|
{
|
|
$path = __DIR__.'/sample/source/script1.js';
|
|
$content = file_get_contents($path);
|
|
$savePath = __DIR__.'/sample/target/script1.js';
|
|
|
|
$minifier = new Minify\JS($path);
|
|
$minifier->minify($savePath);
|
|
|
|
$this->assertEquals(file_get_contents($savePath), $content);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*
|
|
* @expectedException MatthiasMullie\Minify\Exceptions\IOException
|
|
*/
|
|
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
|
|
*
|
|
* @expectedException MatthiasMullie\Minify\Exceptions\IOException
|
|
*/
|
|
public function checkFileWriteFail()
|
|
{
|
|
$minifier = new Minify\JS();
|
|
$wrongPath = '';
|
|
|
|
$object = new ReflectionObject($minifier);
|
|
$method = $object->getMethod('writeToFile');
|
|
$method->setAccessible(true);
|
|
|
|
$method->invokeArgs($minifier, array($wrongPath, ''));
|
|
}
|
|
|
|
/**
|
|
* @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));
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function cache()
|
|
{
|
|
$path = __DIR__.'/sample/source/script1.js';
|
|
$content = file_get_contents($path);
|
|
|
|
$cache = new MemoryStore();
|
|
$pool = new Pool($cache);
|
|
$item = $pool->getItem('cache-script1');
|
|
|
|
$minifier = new Minify\JS($path);
|
|
$item = $minifier->cache($item);
|
|
|
|
$this->assertEquals($item->get(), $content);
|
|
}
|
|
}
|