View.php

<?php

namespace Liaison\Test\Compo;

class View extends \Taeluf\Tester {

    public function testAddViewCallableWithNamespace(){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        new \Lia\Compo\GlobalParam($package);
        $view = new \Lia\Compo\View($package);

        // This approach is extremely rudimentary and not recommended.
        $phrase = "Fight for your right to vote.";
        $lia->addViewCallable('lia:theme',
            $liaTheme=function($name, $args) use ($phrase){
                return $phrase;
            }
        );
        $lia->addViewCallable('theme',$liaTheme);
        $lia->addViewCallable('wrong:theme',
            function($name, $args) use ($phrase){
                return 'view with wrong namespace';
            }
        );
        $phraseAny = 'view with no namespace';
        $lia->addViewCallable('theme',
            function($name, $args) use ($phraseAny){
                return $phraseAny;
            }
        );
        $anyNamespace = $lia->view('theme').'';
        $this->compare($phraseAny, $anyNamespace);

        $specificNamespace = $lia->view('lia:theme').'';
        $this->compare($phrase, $specificNamespace);
    }

    public function testAddViewCallable(){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        new \Lia\Compo\GlobalParam($package);
        $view = new \Lia\Compo\View($package);

        //@export_start(Usage.View.AddViewCallable)
        $phrase = "Fight for your right to vote.";
        $lia->addViewCallable('theme',
            function($name, $args) use ($phrase){
                return $phrase;
            }
        );
        $content = $lia->view('theme').'';
        //@export_end(Usage.View.AddViewCallable)
        return true
        &&  $this->compare($phrase, $content);
    }

    public function testSubdirResourcesAndApiCalls(){
        $lia = new \Liaison(['bare'=>true]);
        $lia->addMethod('addResourceFile', function(){});
        $lia->addMethod('getResourcesHtml', function(){return '';});
        $package = new \Liaison\Test\Mock\Package($lia);
        new \Lia\Compo\GlobalParam($package);
        $vc = new \Lia\Compo\View($package);
        $dir = realpath($this->viewDir);
        $lia->api('lia:view.add','\\Lia\\Obj\\View', $dir, 'theme', ['lia'=>$lia]);
        $args = [
            'content' => 'Just a placeholder',
            'lia' => $lia
        ];
        $view = $lia->api('lia:view.get', 'theme', $args);
        $aContent = $view.'';
        $aResources = $view->resources();
        $resKeys = array_keys($aResources);
        sort($resKeys);

        $tContent = $this->getBlogContent($dir.'/theme.php', $args);
        return true
        &&  $this->compare($tContent, $aContent)
        &&  $this->compare(['/main.css','/main.js'], $resKeys)
        &&  $this->compare($dir.'/theme/main.css', $aResources['/main.css'])
        &&  $this->compare($dir.'/theme/main.js', $aResources['/main.js'])
        ;
    }

    public function testSiblingResourcesAndMethodCalls(){
        $lia = new \Liaison(['bare'=>true]);
        $lia->addMethod('addResourceFile', function(){});
        $package = new \Liaison\Test\Mock\Package($lia);
        new \Lia\Compo\GlobalParam($package);
        $vc = new \Lia\Compo\View($package);
        $dir = realpath($this->viewDir);
        $lia->addView('\\Lia\\Obj\\View', $dir, 'blog', ['lia'=>$lia]);
        $args = [
            'title' => 'Citizen lobbying can reduce pollution',
            'body' => 'In addition to being a conscious consumer, write your elected officials to request regulations on industry that create pollution and subsequent tax-payer funded cleanups.',
            'summary' => 'Polluting corporations need to be regulated.',
            'lia' => $lia
        ];
        $view = $lia->view('blog',$args);
        $aContent = $view.'';
        $aResources = $view->resources();
        $resKeys = array_keys($aResources);
        sort($resKeys);

        $tContent = $this->getBlogContent($dir.'/blog.php', $args);
        return true
        &&  $this->compare($tContent, $aContent)
        &&  $this->compare(['.css','.js'], $resKeys)
        &&  $this->compare($dir.'/blog.css', $aResources['.css'])
        &&  $this->compare($dir.'/blog.js', $aResources['.js'])
        ;
    }
    
    protected function getBlogContent($path, $args){
        extract($args);
        ob_start();
        require($path);
        return ob_get_clean();
    }

    // protected $viewDir = __DIR__.'/../extra/Integration-Package/view/';
    protected $viewDir = __DIR__.'/../extra/Views/';
}

View::runAll();