Resources.php

<?php

namespace Lia\Test\Compo;

class Resources extends \Taeluf\Tester {

/**
 * TODO
 * - remove duplicate setup code
 * - implement commented-out tests
 * 
 * 
 */
    
    //
    // public function testSomething(){
    //     $this->disable();
    //     //add script file
    //     //add script file
    //     //add script file
    //     //sort script files
    //     //save them
    //     //get output html
    //     //deliver the cached javascript file
    //     // verify output matches sorted order
    //     //maybe test cache length?
    //     // definitely test explicit cache invalidation
    // }
    //
    // public function testIsUsingCacheInsteadOfRecompiling(){
    //
    //     $this->disable();
    //     return false;
    // }
    // public function testSeparateLargeFile(){
    //     $this->disable();
    // }
    //
    // public function testAddCode(){
    //     echo 'test not written yet';
    //     $this->disable();
    // }
    // public function testGetHtml(){
    //     //Check for seo html, compiled resource html, url html, and explicit code blocks
    //     echo "Between testSeoFunctions and testCompiledHtml, this is basically being done.";
    //     $this->disable();
    // }
    // public function testSeoImageRequiresAlt(){
    //     $this->disable();
    // }
    // public function testSeoArray(){
    //     $this->disable();
    // }

    public function testAddUrl(){

        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $cache = new \Lia\Compo\Cache($package);
        $res = new \Lia\Compo\Resources($package);
        for ($i=0;$i<15;$i++){
            $ext = $i%2 ? 'css' : 'js';
            $lia->addResourceUrl('https://test.taeluf.com/'.$i.'.'.$ext);
        }
        
        $scriptTags = $res->getUrlTag('js');
        $cssTags = $res->getUrlTag('css');
        
        $tScripts = 
<<<HTML
    <script type="text/javascript" src="https://test.taeluf.com/0.js"></script>
    <script type="text/javascript" src="https://test.taeluf.com/2.js"></script>
    <script type="text/javascript" src="https://test.taeluf.com/4.js"></script>
    <script type="text/javascript" src="https://test.taeluf.com/6.js"></script>
    <script type="text/javascript" src="https://test.taeluf.com/8.js"></script>
    <script type="text/javascript" src="https://test.taeluf.com/10.js"></script>
    <script type="text/javascript" src="https://test.taeluf.com/12.js"></script>
    <script type="text/javascript" src="https://test.taeluf.com/14.js"></script>
HTML;    

        $tStyles = 
<<<HTML
    <link rel="stylesheet" href="https://test.taeluf.com/1.css" />
    <link rel="stylesheet" href="https://test.taeluf.com/3.css" />
    <link rel="stylesheet" href="https://test.taeluf.com/5.css" />
    <link rel="stylesheet" href="https://test.taeluf.com/7.css" />
    <link rel="stylesheet" href="https://test.taeluf.com/9.css" />
    <link rel="stylesheet" href="https://test.taeluf.com/11.css" />
    <link rel="stylesheet" href="https://test.taeluf.com/13.css" />
HTML;    

        return true
        &&  $this->compare($tScripts, $scriptTags)
        &&  $this->compare($tStyles, $cssTags)

        ;
        $html = $lia->getHeadHtml();

        $aHtml = explode("\n",$html);
        $aHtml = array_map('trim',$aHtml);
        $aHtml = array_filter($aHtml);
        sort($aHtml);
        $tHtml = [$scriptTag, $cssTag];
        sort($tHtml);

        return true 
        &&  $this->compare($tHtml, $aHtml)

        ;
    }

    public function testCompiledHtml(){
        // $lia = new \Liaison(['bare'=>true]);
        // $package = new \Liaison\Test\Mock\Package($lia);
        // $cache = new \Lia\Compo\Cache($package);
        // $lia->set('lia:cache.dir', $this->cacheDir);
        // $lia->set('lia:resource.forceRecompile', true);
        // $res = new \Lia\Compo\Resources($package);
        // $seo = new \Lia\Compo\Seo($package);
        $lia = new \Liaison();
        $res = $lia->compo('resources');
        $lia->set('lia:cache.dir', $this->cacheDir);
        $lia->set('lia:resource.forceRecompile', true);

        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            $ext = pathinfo($file,PATHINFO_EXTENSION);
            if ($ext!='js'&&$ext!='css')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
        }
        $jsFile = $res->compileFilesToCache('js');
        $cssFile = $res->compileFilesToCache('css');
        
        $scriptTag = $res->getFileTag('js');
        $cssTag = $res->getFileTag('css');

        $html = $lia->getHeadHtml();

        $aHtml = explode("\n",$html);
        $aHtml = array_map('trim',$aHtml);
        $aHtml = array_filter($aHtml);
        sort($aHtml);
        $tHtml = [$scriptTag, $cssTag];
        sort($tHtml);

        return true 
        &&  $this->compare($tHtml, $aHtml)

        ;
    }

    public function testDuplicateCompile(){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $cache = new \Lia\Compo\Cache($package);
        $lia->set('lia:resource.forceRecompile', true);
        $lia->set('lia:cache.dir', $this->cacheDir);
        $res = new \Lia\Compo\Resources($package);

        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            $ext = pathinfo($file,PATHINFO_EXTENSION);
            if ($ext!='js'&&$ext!='css')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
        }

        $jsFile = $res->compileFilesToCache('js');
        $jsFile1 = $res->compileFilesToCache('js');
        $jsFile2 = $res->compileFilesToCache('js');
        $jsFile3 = $res->compileFilesToCache('js');

        $cssFile = $res->compileFilesToCache('css');
        $cssFile1 = $res->compileFilesToCache('css');
        $cssFile2 = $res->compileFilesToCache('css');
        $cssFile3 = $res->compileFilesToCache('css');
        
        return true
        &&  $this->compare($jsFile,$jsFile1)
        &&  $this->compare($jsFile1,$jsFile2)
        &&  $this->compare($jsFile2, $jsFile3)

        &&  $this->compare($cssFile,  $cssFile1)
        &&  $this->compare($cssFile1, $cssFile2)
        &&  $this->compare($cssFile2, $cssFile3)

        ;
    }

    public function testDeliverCss(){
        $lia = new \Liaison();
        $res = $lia->compo('lia:resources');
        $package = $lia->getPackage('lia');

        $lia->addViewCallable('theme',
            function($name, $args){
                return $args['content'];
            }
        );
        $lia->set('lia:cache.dir', $this->cacheDir);
        $lia->set('lia:resource.forceRecompile', true);
        $lia->set('lia.Server.bufferResponse', false);
        
        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            if (pathinfo($file,PATHINFO_EXTENSION)!='css')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
            $content[] = file_get_contents($filesDir.'/'.$file);
        }

        $aRoute = $res->getCompiledFilesUrl('css');

        ob_start();
        $lia->deliver($aRoute, "GET");
        $aContent = ob_get_clean();

        $tStyles = implode("\n", $content);

        return true
        &&  $this->compare(strlen($tStyles)>5, true, true)
        &&  $this->compare($tStyles, $aContent);
        
        ;
    }

    public function testDeliverJs(){
        $lia = new \Liaison();
        
        $res = $lia->compo('resources');
        $package = $lia->getPackage('lia');

        $lia->addViewCallable('theme',
            function($name, $args){
                return $args['content'];
            }
        );
        $lia->set('lia:cache.dir', $this->cacheDir);
        $lia->set('lia:resource.forceRecompile', true);
        $lia->set('lia.Server.bufferResponse', false);


        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            if (pathinfo($file,PATHINFO_EXTENSION)!='js')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
            $content[] = file_get_contents($filesDir.'/'.$file);
        }

        $aRoute = $res->getCompiledFilesUrl('js');

        ob_start();
        try {
            $lia->deliver($aRoute, "GET");
        } catch (\Exception $e){
            $aContent = ob_get_clean();
            throw $e;
        }
        $aContent = ob_get_clean();

        $tScripts = implode("\n", $content);

        return true
        &&  $this->compare(strlen($tScripts)>5, true, true)
        &&  $this->compare($tScripts, $aContent);
        
        ;
    }
    
    public function testCacheCompiledJSFile(){
//         return;
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $cache = new \Lia\Compo\Cache($package);

        $lia->set('lia:cache.dir', $this->cacheDir);
        $lia->set('lia:resource.forceRecompile', true);
        // $lia->set('lia:cache.dir.public', __DIR__.'/../../../test-extra/Compo/Resources/public/');

        $res = new \Lia\Compo\Resources($package);
        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            if (substr($file,-3)!='.js')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
            $content[] = file_get_contents($filesDir.'/'.$file);
        }

        $tFile = $res->compileFilesToCache('js');
        $tScripts = implode("\n", $content);
        return true
        &&  $this->compare(strlen($tScripts)>5, true, true)
        &&  $this->compare($tScripts, file_get_contents($tFile))
        
        ;
    }

    public function testCacheCompiledCSSFile(){
//         return;
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $cache = new \Lia\Compo\Cache($package);

        $lia->set('lia:cache.dir', $this->cacheDir);
        $lia->set('lia:resource.forceRecompile', true);
        // $lia->set('lia:cache.dir.public', __DIR__.'/../../../test-extra/Compo/Resources/public/');

        $res = new \Lia\Compo\Resources($package);
        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            if (substr($file,-4)!='.css')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
            $content[] = file_get_contents($filesDir.'/'.$file);
        }

        $tFile = $res->compileFilesToCache('css');
        $tStyles = implode("\n", $content);
        return true
        &&  $this->compare(strlen($tStyles)>5, true, true)
        &&  $this->compare($tStyles, file_get_contents($tFile))
        
        ;
    }

    public function testSortCss(){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $res = new \Lia\Compo\Resources($package);
        $filesDir = $this->outDir;
        $content = [];
        $files = [];
        foreach (scandir($filesDir) as $file){
            if (substr($file,-4)!='.css')continue;
            $files[$file] = $filesDir.'/'.$file;
            $lia->addResourceFile($filesDir.'/'.$file);
        }
        $files = [
            'b.css' => $files['b.css'],
            'a.css' => $files['a.css'],
            'c.css' => $files['c.css'],
        ];
        foreach ($files as $f){
            $content[] = file_get_contents($f);
        }

        $res->setSorter('css',
            function($files){
                $files = array_values($files);
                return
                [
                    'b.css' => $files[1],
                    'a.css' => $files[0],
                    'c.css' => $files[2],
                ];
            }
        );
        
        $aStyles = $res->concatenateFiles('css');
        $tStyles = implode("\n", $content);
        return true
        &&  $this->compare(strlen($tStyles)>5, true, true)
        &&  $this->compare($tStyles, $aStyles)

        ;
    }

    public function testCompileCss(){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $res = new \Lia\Compo\Resources($package);
        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            if (substr($file,-4)!='.css')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
            $content[] = file_get_contents($filesDir.'/'.$file);
        }
        $aStyles = $res->concatenateFiles('css');
        $tStyles = implode("\n", $content);
        return true
        &&  $this->compare(strlen($tStyles)>5, true, true)
        &&  $this->compare($tStyles, $aStyles)

        ;
    }


    public function testSortJs(){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $res = new \Lia\Compo\Resources($package);
        $filesDir = $this->outDir;
        $content = [];
        $files = [];
        foreach (scandir($filesDir) as $file){
            if (substr($file,-3)!='.js')continue;
            $files[$file] = $filesDir.'/'.$file;
            $lia->addResourceFile($filesDir.'/'.$file);
        }
        $files = [
            'b.js' => $files['b.js'],
            'a.js' => $files['a.js'],
            'c.js' => $files['c.js'],
        ];
        foreach ($files as $f){
            $content[] = file_get_contents($f);
        }

        $res->setSorter('js',
            function($files){
                $files = array_values($files);
                return
                [
                    'b.js' => $files[1],
                    'a.js' => $files[0],
                    'c.js' => $files[2],
                ];
            }
        );
        
        $aScripts = $res->concatenateFiles('js');
        $tScripts = implode("\n", $content);
        return true
        &&  $this->compare(strlen($tScripts)>5, true, true)
        &&  $this->compare($tScripts, $aScripts)

        ;
    }

    public function testCompileJs(){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $res = new \Lia\Compo\Resources($package);
        $filesDir = $this->outDir;
        $content = [];
        foreach (scandir($filesDir) as $file){
            if (substr($file,-3)!='.js')continue;
            $lia->addResourceFile($filesDir.'/'.$file);
            $content[] = file_get_contents($filesDir.'/'.$file);
        }
        $aScripts = $res->concatenateFiles('js');
        $tScripts = implode("\n", $content);
        return true
        &&  $this->compare(strlen($tScripts)>5, true, true)
        &&  $this->compare($tScripts, $aScripts)

        ;
    }

    protected $outDir = __DIR__.'/../extra/Resources/file/';
    protected $cacheDir = __DIR__.'/../extra/Resources/cache/';
    // public function sortCSS($key, )
}

Resources::runAll();

// Resources::runAllToFile(__FILE__.'.html');
// Resources::xdotoolRefreshFirefox();