Main.php

<?php

namespace Lia\Addon\MdBlog\Test;

class Main extends \Tlf\Tester {

    public function check_hljs($content){
        $this->str_contains($content, ['hljs-function', 'language-php hljs php', 'hljs-keyword', 'hljs-title', 'hljs-params', 'hljs-string']);
    }

    public function prepare(){

    }

    public function testBlogHtmlCaching(){
        $this->disable();
    }
    public function testBlogPage(){
        $page = $this->get('/blog/test/header-with-php/');

        echo $page;
        $this->str_contains($page,['<h1>Header</h1>','<pre><code ','</code></pre>']);
        $this->check_hljs($page);
    }

    public function testRoutes(){
        $lia = new \Lia();
        $server = new \Lia\Package\Server($lia);
        $mdblog = \Lia\App\MdBlog::enable($lia);
        // $mdblog->set('blog.dir2', dirname(__DIR__).'/input/blogs/');
        $mdblog->addons['blog']->dir = dirname(__DIR__).'/input/blogs/';
        $lia->call_hook('ServerStart');

        $routes = $server->addons['router']->routeMap['GET'];

        $this->compare(
            ['/blog/', '/blog/nest/one/', '/blog/nest/two/', 
            '/blog/nest/sub/four/', '/blog/nest/sub/three/', '/blog/nest/sub/deep/five/',
            '/blog/test/header-with-php/', '/blog/test/nother-one/',
            ],
            array_keys($routes)
        );

    }
    public function testBlogHomePageGrid(){
        //this is a server test & should probably be setup as such
        $page = $this->get('/blog/');
        echo $page;
        $this->str_contains(
            $page,
            [
            '<section class="BlogCardGrid">',
            '<h3>Nest</h3>',
            '<h3>Nest/sub</h3>',
            '<h3>Nest/sub/deep</h3>',
            '<h3>Test</h3>',
            ]
        );


    }

    public function testGetAllDirs(){
        $blog = new \Lia\App\MdBlog\Main();
        $dirs = $blog->get_all_dirs('', $this->file('test/input/blogs/'));
        $this->compare_arrays(
            ['nest/','nest/sub/','nest/sub/deep/', 'test/'],
            $dirs
        );
    }

    public function testGridSorter(){
        $sorter = [];

        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $package->set('blog.dir', $this->file('test/input/blogs/'));

        $sorter = 
            function($blogs){
                $blogs = array_reverse($blogs);
                return $blogs;
            };
        ;
        $unsorted = $blog->get_all_blogs();
        $sorted = $blog->get_all_blogs([], $sorter);

        $this->compare(array_keys($unsorted), array_keys(array_reverse($sorted)));
    }

    public function testBlogsFiltering(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $blog->dir = $this->file('test/input/blogs/');
        $blogs = $blog->get_all_blogs(['test','nest/sub']);

        print_r($blogs);

        $this->test('category: nest/sub');
        $this->is_true(isset($blogs['nest/sub'][0]));
        $this->is_true(isset($blogs['nest/sub'][1]));
        $this->is_false(isset($blogs['nest/sub'][2]));


        $this->test('category: test');
        $this->is_true(isset($blogs['test'][0]));
        $this->is_true(isset($blogs['test'][1]));
        $this->is_false(isset($blogs['test'][2]));
    }

    public function testGetBlogGrid(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $package->set('blog.dir', $this->file('test/input/blogs/'));
        $grid = $blog->get_blog_grid();


        echo $grid;


        $this->str_contains(
            $grid,
            [
            '<h3>Nest</h3>',
            '<a href="/blog/nest/one/">no-title</a>',
            '<a href="/blog/nest/two/">no-title</a>',
            ]
        );

        $this->str_contains(
            $grid,
            [
            '<h3>Nest/sub</h3>',
            '<a href="/blog/nest/sub/four/">no-title</a>',
            '<a href="/blog/nest/sub/three/">no-title</a>',
            ]
        );

        $this->str_contains(
            $grid,
            [
            '<h3>Nest/sub/deep</h3>',
            '<a href="/blog/nest/sub/deep/five/">Deep Five</a>',
            ]
        );

        $this->str_contains(
            $grid,
            [
            '<section class="BlogCard">',
            '<h3>Test</h3>',
            '<nav>',
            '<a href="/blog/test/header-with-php/">Header</a>',
            '<a href="/blog/test/nother-one/">Another one</a>',
            '</nav>',
            '</section>',
            ]
        );

    }

    public function testGetBlogCard(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $package->set('blog.dir', $this->file('test/input/blogs/'));
        $card = $blog->get_blog_card('test');

        echo $card;
        $this->str_contains(
            $card,
            [
            '<section class="BlogCard">',
            '<h3>Test</h3>',
            '<nav>',
            '<a href="/blog/test/header-with-php/">Header</a>',
            '<a href="/blog/test/nother-one/">Another one</a>',
            '</nav>',
            '</section>',
            ]
        );
    }

    public function testGetBlogsInCategory(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $blog->dir = $this->file('test/input/blogs/');

        $blogs = $blog->get_blog_entries('test');
        $this->compare_arrays(
            [ 
                [
                    'title'=> 'Header',
                    'url'=>'/blog/test/header-with-php/',
                    'path'=>$blog->dir.'/test/header-with-php.md',
                ],
                [
                    'title'=>'Another one',
                    'url'=>'/blog/test/nother-one/',
                    'path'=>$blog->dir.'/test/nother-one.md',
                ]
            ],
            $blogs
        );
    }


    public function testGetTitleUsingCache(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];

        $package->cache('mdblog.title:doesnt matter', 'cached title');

        $title = $blog->get_title('doesnt matter', $this->file('test/input/blogs/test/header-with-php.md'));
        $this->compare('cached title', $title);

    }

    public function testGetTitle(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $title = $blog->get_title('doesnt matter', $this->file('test/input/blogs/test/header-with-php.md'));
        $this->compare('Header', $title);


        $title = $blog->get_title('doesnt matter 2', $this->file('test/input/blogs/test/nother-one.md'));
        $this->compare('Another one', $title);
    }

    /**
     * @test html for the blogpost itself
     * @note this test takes a long time to run (25ms or so), whereas MdToHtml is very quick ... that's because this test is run before md to html & I think commonmark does some caching
     */
    public function testGetBlogHtml(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $package->set('blog.dir', $this->file('test/input/blogs/'));
        $html = $blog->get_blog_html('test/header-with-php');

        $this->str_contains($html,['<h1>Header</h1>','<pre><code ','</code></pre>']);
        $this->check_hljs($html);
    }

    public function testMdToHtml(){
        $package = \Lia\App\MdBlog::enable();
        $blog = $package->addons['blog'];
        $markdown = file_get_contents($this->file('test/input/blogs/test/header-with-php.md'));
        $html = $blog->md_to_html($markdown);

        echo $html;
        $this->str_contains($html,['<h1>Header</h1>','<pre><code ','</code></pre>']);
        $this->check_hljs($html);
    }

    public function testStructure(){
        $package = \Lia\App\MdBlog::enable();

        $this->is_a($package, '\\Lia\\Package\\Server');
        $this->is_a($package->lia, '\\Lia');

        $this->invert();
        $this->is_a($package->lia, '\\Lia\\Package');
        $this->invert();

        $this->is_a($package->addons['blog'], '\\Lia\\Addon');

        $this->compare($package, $package->addons['package'], true);

    }
}