Author: Stephen Clay (steve@mrclay.org)
function test_Minify_HTML_Helper()
{
    global $thisDir;
    $realDocRoot = $_SERVER['DOCUMENT_ROOT'];
    $_SERVER['DOCUMENT_ROOT'] = $thisDir;
    $file1 = $thisDir . '/_test_files/css/paths_prepend.css';
    $file2 = $thisDir . '/_test_files/css/styles.css';
    $maxTime = max(filemtime($file1), filemtime($file2));
    $uri1 = '//_test_files/css/paths_prepend.css';
    $uri2 = '//_test_files/css/styles.css';
    $expected = "/min/b=_test_files/css&f=paths_prepend.css,styles.css&{$maxTime}";
    $actual = Minify_HTML_Helper::getUri(array($uri1, $uri2));
    $passed = assertTrue($actual === $expected, 'Minify_HTML_Helper : given URIs');
    $expected = "/min/b=_test_files/css&f=paths_prepend.css,styles.css&{$maxTime}";
    $actual = Minify_HTML_Helper::getUri(array($file1, $file2));
    $passed = assertTrue($actual === $expected, 'Minify_HTML_Helper : given filepaths');
    $expected = "/min/g=notRealGroup&debug";
    $actual = Minify_HTML_Helper::getUri('notRealGroup', array('debug' => true));
    $passed = assertTrue($actual === $expected, 'Minify_HTML_Helper : non-existent group & debug');
    $expected = "/myApp/min/?g=css&{$maxTime}";
    $actual = Minify_HTML_Helper::getUri('css', array('rewriteWorks' => false, 'minAppUri' => '/myApp/min/', 'groupsConfigFile' => $thisDir . '/_test_files/htmlHelper_groupsConfig.php'));
    $passed = assertTrue($actual === $expected, 'Minify_HTML_Helper : existing group');
    $utilsFile = dirname(__DIR__) . '/min/utils.php';
    if (is_file($utilsFile)) {
        require_once $utilsFile;
        $fiveSecondsAgo = $_SERVER['REQUEST_TIME'] - 5;
        $obj = new Minify_Source(array('id' => '1', 'content' => '1', 'lastModified' => $fiveSecondsAgo));
        $output = Minify_mtime(array($uri1, $uri2, $obj));
        $passed = assertTrue($output === $fiveSecondsAgo, 'utils.php : Minify_mtime w/ files & obj');
        $obj = new Minify_Source(array('id' => '2', 'content' => '2', 'lastModified' => strtotime('2000-01-01')));
        $output = Minify_mtime(array($obj, 'css'), $thisDir . '/_test_files/htmlHelper_groupsConfig.php');
        $passed = assertTrue($output === $maxTime, 'utils.php : Minify_mtime w/ obj & group');
    }
    $_SERVER['DOCUMENT_ROOT'] = $realDocRoot;
}
Beispiel #2
0
/**
 * Get the last modification time of several source js/css files. If you're
 * caching the output of Minify_getUri(), you might want to know if one of the
 * dependent source files has changed so you can update the HTML.
 *
 * Since this makes a bunch of stat() calls, you might not want to check this
 * on every request.
 * 
 * @param array $keysAndFiles group keys and/or file paths/URIs.
 * @return int latest modification time of all given keys/files
 */
function Minify_mtime($keysAndFiles, $groupsConfigFile = null)
{
    $gc = null;
    if (!$groupsConfigFile) {
        $groupsConfigFile = dirname(__FILE__) . '/groupsConfig.php';
    }
    $sources = array();
    foreach ($keysAndFiles as $keyOrFile) {
        if (is_object($keyOrFile) || 0 === strpos($keyOrFile, '/') || 1 === strpos($keyOrFile, ':\\')) {
            // a file/source obj
            $sources[] = $keyOrFile;
        } else {
            if (!$gc) {
                $gc = (require $groupsConfigFile);
            }
            foreach ($gc[$keyOrFile] as $source) {
                $sources[] = $source;
            }
        }
    }
    return Minify_HTML_Helper::getLastModified($sources);
}