Tester.php

<?php

namespace Phad;

require_once(__DIR__.'/TestHelper.php');

class Tester extends \Tlf\Tester {

    protected array $blog_data = [
        ['title'=>'Bear', 'description'=>'The best animal', 'type'=>'bear'],
        ['title'=>'Dog', 'description'=>'Ruff', 'type'=>'dog'],
        ['title'=>'Cat', 'description'=>'Pitty Paws', 'type'=>'cat'],
        ['title'=>'Bird', 'description'=>'Can Fly', 'type'=>'bird'],
    ];

    protected ?\PDO $pdo = null;
    public ?\Phad $phad = null;

    public function bootstrap(){
        require(__DIR__.'/bootstrap-server.php');
    }

    public function helper($initCompo = true){
        return new TestHelper($initCompo);
    }
    //
    // protected function has_node($textToTest,$tagName, $content){
    //     return $this->str_contains($textToTest,"<$tagName>$content</$tagName>");
    // }
    // protected function not_has_node($textToTest,$tagName, $content){
    //     return $this->str_not_contains($textToTest,"<$tagName>$content</$tagName>");
    // }

    protected function viewSrc($viewName){
        $viewPath = $this->file('test/input/views/').$viewName.'.php';
        if (file_exists($viewPath))return file_get_contents($viewPath);

        throw new \Exception("View '$viewName' does not exist at '$viewPath'");
    }

    protected function item($viewName, $args=[]){
        $this->phad = $this->phad ?? $this->phad();
        return $this->phad->item($viewName, $args);
    }

    protected function makeBlogTable(&$ListOfBlogs){
        $this->itemList($ListOfBlogs);
        $pdo = $this->pdo;
        $pdo->exec("DROP TABLE IF EXISTS blog");
        $pdo->exec("CREATE TABLE blog(title VARCHAR(254), description VARCHAR(1024), type VARCHAR(254))");
        $stmt = $pdo->prepare("INSERT INTO blog (title, description, type) VALUES(:title, :description, :type)");
        foreach ($ListOfBlogs as $OneBlog){
            $OneBlog = (array)$OneBlog;
            $stmt->execute($OneBlog);
        }
        return $ListOfBlogs;
    }

    /**
     * Create the blog table and hydrate it with the standard set of rows
     */
    protected function makeStandardBlogTable(): array {
        $ListOfBlogs = $this->blog_data;
        $pdo = $this->pdo;
        $pdo->exec("DROP TABLE IF EXISTS blog");
        $pdo->exec("CREATE TABLE blog(title VARCHAR(254), description VARCHAR(1024), type VARCHAR(254))");
        $stmt = $pdo->prepare("INSERT INTO blog (title, description, type) VALUES(:title, :description, :type)");
        foreach ($ListOfBlogs as $OneBlog){
            $OneBlog = (array)$OneBlog;
            $stmt->execute($OneBlog);
        }
        return $ListOfBlogs;
    }

    /**
     * Create the blog table and hydrate it with the standard set of rows, and add ids
     */
    protected function makeIDBlogTable(): array {
        $ListOfBlogs = $this->blog_data;
        $pdo = $this->pdo;
        $pdo->exec("DROP TABLE IF EXISTS blog");
        $pdo->exec("CREATE TABLE blog(id int PRIMARY KEY, title VARCHAR(254), description VARCHAR(1024), type VARCHAR(254))");
        $stmt = $pdo->prepare("INSERT INTO blog (id, title, description, type) VALUES(:id, :title, :description, :type)");
        foreach ($ListOfBlogs as $index=>$OneBlog){
            $OneBlog = (array)$OneBlog;
            $OneBlog['id'] = $index;
            $stmt->execute($OneBlog);
        }
        return $ListOfBlogs;
    }

    protected function itemList(&$BlogList){
        $BlogList = [
            (object)[
                'title'=>'Cats',
                'description'=>'Cats are furry and cute and maybe a little evil',
                'type'=>'pet',
            ],
            (object)[
                'title'=>'Dogs',
                'description'=>'Dogs are furry and cute and maybe a little dumb',
                'type'=>'pet',
            ],
            (object)[
                'title'=>'Bears',
                'description'=>'Bears are the best, but they aren\'t trained for cuddles :(',
                'type'=>'wild',
            ]
        ];
    }

    public function rows(&$Blog = null, &$Song = null){
        $Blog = [
            'title'=>'What about wind turbine waste?',
            'description'=>'Its a real problem, and we should look for ways to solve it while still providing sustainable energy.',
            'type'=> 'static-post',
        ];

        $Song = [
            'title'=>'Losing Interest',
            'description'=> 'An absolutely lovely lofi song I found recently. Did you even know lo-fi is fantastic? I didn\'t... til recently'
        ];

        return ['Blog'=>$Blog, 'Song'=>$Song];
    }


    protected function phad() {

        $th = new \Phad\TestHelper();
        $root_dir = __DIR__.'/input/';  
        $pdo = $th->pdo(true);
        $this->pdo = $pdo;
        $lia = new \Lia();    
          
        // Add the built-in Package, which provides all the web-server features.    
        $server_package = \Lia\Package\Server::main($lia); // same as: new \Lia\Package\Server($lia, $fqn='lia:server');  
  

        //$configs = [...]; // configs are optional  
        $configs = [
            'dir.templates' => 'views',
            'compile.force' => true,
        ];
          
        $phad = \Phad::custom(  
            $pdo,  
            $root_dir, // $dir will contain sub-dirs `phad/`, `sitemap/`, and `cache/` by default  
            new \Phad\Integration\LiaisonRouter($lia),  
            new \Phad\Integration\DynamicAccess(),  
            $configs  
        ); 
        $phad->exit_on_redirect = false;

        $this->phad = $phad;
        return $phad;
    }
}