Router.php

<?php

namespace Liaison\Test\Addon;

class Router extends \Tlf\Tester {

    public function testCacheRoutes(){
        $dir = $this->file('test/Server');
        $cache_dir = $this->file('test/Server/cache/');

        // how to test (each step is a new lia instance)
        // 1: get the route map without cached routes
        // 2: enable cache & run the server so routes are loaded & then cached. add some routes & get the modified (uncached) route map
        // 3: enable cache, add several routes (which will do nothing), and get the cached routes
        // 4: compare #1 route map to #3 route map (should be same)
        // 5: compare #2 route map to #3 route map (should be different)

        // 1: no cache
        $lia = new \Lia();
        $main = new \Lia\Package\Server($lia, 'server');
        $test = new \Lia\Package\Server($lia, 'site', $dir);
        $lia->cache->dir = $cache_dir;
        $cached_routes = $lia->cache_file('lia:server.router.cached_routes_map','def', -1);
        $lia->router->cache_routes = false;
        $lia->ready();
        $uncached_routes = $lia->router->routeMap;

        // 2: Create cache
        $lia = new \Lia();
        $main = new \Lia\Package\Server($lia, 'server');
        $test = new \Lia\Package\Server($lia, 'site', $dir);
        $lia->cache->dir = $cache_dir;
        $lia->router->cache_routes = true;
        $lia->ready();
        // this prepares the cache
        $response = $lia->getResponse('/', 'GET');
        $f = function(){};
        $lia->addRoute('abc', $f);
        $lia->addRoute('abcd', $f);
        $lia->addRoute('abcde', $f);
        $lia->addRoute('abcdef', $f);
        $modified_routes = $lia->router->routeMap;

        // 3: Use the cache
        $lia = new \Lia();
        $main = new \Lia\Package\Server($lia, 'server');
        $test = new \Lia\Package\Server($lia, 'site', $dir);
        $lia->cache->dir = $cache_dir;
        $lia->router->cache_routes = true;
        $lia->ready();
        // this prepares the cache
        $response = $lia->getResponse('/', 'GET');
        $f = function(){};
        $lia->addRoute('abc', $f);
        $lia->addRoute('abcd', $f);
        $lia->addRoute('abcde', $f);
        $lia->addRoute('abcdef', $f);
        $cached_routes = $lia->router->routeMap;


        // $lia->dump($cached_routes);
        // exit;
        $this->compare_dump($uncached_routes, $cached_routes);
        $this->invert();
        $this->compare_dump($cached_routes, $modified_routes);
        $this->compare_dump($uncached_routes, $modified_routes);
        $this->invert();
    }

    public function testPatternAndParamatersToUrl(){
        $router = new \Lia\Addon\Router();
        $pattern = '/blog/{category}/';
        $params = ['category'=>'some-category'];
        $target = '/blog/some-category/';

        $decoded = $router->decode_pattern($pattern);
        $filled = $router->decoded_pattern_to_url($decoded, $params);

        $this->compare(
            $target,
            $filled
        );
    }

    public function testExtractUrlParamaters(){
        $router = new \Lia\Addon\Router();
        $url = '/blog/some-category/';
        $pattern = '/blog/{category}/';
        $decoded = $router->decode_pattern($pattern);
        $extracted = $router->extract_url_paramaters($decoded, $url);

        $this->compare(
            ['category'=>'some-category'],
            $extracted
        );
    }

    public function testDecodePatternWithOptionalParamaters(){
        $router = new \Lia\Addon\Router();
        $pattern = '/blog/{?category}/';
        $decoded = $router->decode_pattern($pattern);

        $actual_decoded = $router->separate_optional_from_decoded_pattern($decoded);

        
        // $all = array_map(function($a){unset($a['extraParsedPattern']);unset($a['optionalParams']); return $a;},$all);
        // var_export($actual_decoded);
        // exit;

        $target = [
            [
                'pattern' => '/blog/{?category}/',
                'parsedPattern' => '/blog/?/',
                'params' => ['category'],
                'methods' => ['GET' => 'GET'],
            ],
            [
                'pattern' => '/blog/{?category}/',
                'parsedPattern' => '/blog/',
                'params' => [],
                'methods' => ['GET' => 'GET'],
            ],
        ];

        $this->compare($target, $actual_decoded);
    }

    /**
     * Example of `$router->url_to_regex()`
     */
    public function testUrlToRegex(){
        $router = new \Lia\Addon\Router();
        $url = '/one/two/';
        $test = $router->url_to_regex($url);

        $target = '^\/(?:one|\?)\/(?:two|\?)\/$';
        $this->compare($target ,$test);
    }

    /**
     * Example of `$router->decode_pattern()`
     */
    public function testDecodePattern(){
        $router = new \Lia\Addon\Router();

        $actual_parsed = $router->decode_pattern("/blog/{slug}/{id}/");

        $target_parsed = [
            "pattern" => "/blog/{slug}/{id}/",
            "parsedPattern" => "/blog/?/?/",
            "params" => [
                    0 => "slug",
                    1 => "id",
                ],
            "methods" => [
                "GET" => "GET",
            ],
        ];

        $this->compare($target_parsed, $actual_parsed);
    }

    /**
     * Example of `$router->decoded_pattern_to_url()`. @see(testParsePatternExample) to see what the `$decoded` array looks like
     *
     */
    public function testDecodedPatternToUrl(){
        $router = new \Lia\Addon\Router();
        $decoded = $router->decode_pattern("/blog/{slug}/{id}/");

        $fill = ['slug'=>'cats', 'id'=>33];
        $filled = $router->decoded_pattern_to_url($decoded, $fill);

        $this->compare('/blog/cats/33/', $filled);
    }

}