View.php

<?php

namespace Liaison\Test\Addon;

class View extends \Tlf\Tester {
    /**
     * @test view loads nested resources (view-name.php also loads view-name/*.css & view-name/*.js)
     */
    public function testNestedViewResources(){
        $lia = new \Lia();
        $package = new \Lia\Package($lia, 'test');
        $view_handler = new \Lia\Addon\View($package);
        $resources = new \Lia\Addon\Resources($package);
        $view_handler->init_lia();
        $resources->init_lia();
        $dir = $this->view_dir_2();

        $lia->addView('nested', $dir);
        $output = $lia->view('nested');

        $this->compare('nested', $output);


        $this->compare(
            [
                'css'=>[
                    $dir.'nested/one.css'=>$dir.'nested/one.css',
                    $dir.'nested/two.css'=>$dir.'nested/two.css'
                ],
                'js'=>[
                    $dir.'nested/one.js'=>$dir.'nested/one.js',
                    $dir.'nested/two.js'=>$dir.'nested/two.js'
                ],
            ]
            , $lia->resources->files
        );
    }

    /**
     * @test view loads sibling resources (view-name.php also loads view-name.css & view-name.js)
     */
    public function testSiblingViewResources(){
        $lia = new \Lia();
        $package = new \Lia\Package($lia, 'test');
        $view_handler = new \Lia\Addon\View($package);
        $resources = new \Lia\Addon\Resources($package);
        $view_handler->init_lia();
        $resources->init_lia();
        $dir = $this->view_dir_2();

        $lia->addView('sibling', $dir);
        $output = $lia->view('sibling');

        $this->compare('sibling', $output);


        $this->compare(
            [
                'js'=>[$dir.'sibling.js'=>$dir.'sibling.js'],
                'css'=>[$dir.'sibling.css'=>$dir.'sibling.css'],
            ]
            , $lia->resources->files
        );
    }


    public function testAddViewMain(){
        $view = new \Lia\Addon\View();
        $file = $this->view_dir().'/test.php';
        $view->addView('lia:test', $this->view_dir_2());

        $this->compare(
            'test-dir',
            $view->view('test'),
        );

        $this->compare(
            'test-dir',
            $view->view('lia:test'),
        );
    }

    public function testAddViewCallable(){
        $view = new \Lia\Addon\View();
        $file = $this->view_dir().'/test.php';
        $view->addViewCallable('lia:test', function($view_name,$args){return 'test-call';});

        $this->compare(
            'test-call',
            $view->view('test'),
        );

        $this->compare(
            'test-call',
            $view->view('lia:test'),
        );
    }

    public function testAddViewFile(){
        $view = new \Lia\Addon\View();
        $file = $this->view_dir().'/test.php';
        $view->addViewFile('lia:test', $file);


        $this->compare(
            file_get_contents($file),
            $view->view('test'),
        );

        $this->compare(
            file_get_contents($file),
            $view->view('lia:test'),
        );
    }

    public function view_dir(){
        return realpath($this->cli->pwd.'/test/input/Views/').'/';
    }
    public function view_dir_2(){
        return realpath($this->cli->pwd.'/test/input/Views2/').'/';
    }
}