Server.php
<?php
namespace Liaison\Test\Addon;
class Server extends \Tlf\Tester {
public function testJsonTheme(){
$response = $this->get('/add-css-and-js/',['theme'=>'json']);
$t = [
'content'=>'sample content',
'scripts'=> ['/lia-resource.*.min.js', '/some-url.js', '/some-url2.js'],
'stylesheets'=> ['/lia-resource.*.min.css', '/some-url.css', '/some-url2.css'],
];
$a = json_decode($response, true);
$this->test("HTML content");
$this->compare($t['content'], $a['content']);
$remove_nums = function($v){
return preg_replace("/lia-resource\.[a-zA-Z0-9]+\.min/", "lia-resource.*.min", $v);
};
$fixed_scripts = array_map($remove_nums, $a['scripts']);
$this->test("Scripts");
$this->compare_arrays($t['scripts'], $fixed_scripts);
$fixed_styles = array_map($remove_nums, $a['stylesheets']);
$this->test("Stylesheets");
$this->compare_arrays($t['stylesheets'], $fixed_styles);
//echo $response;
}
public function testGetDistinctRoute(){
$_SERVER['REQUEST_URI'] = 'na';
$lia = new \Lia();
// Add the built-in Package, which provides all the web-server features.
$server_package = new \Lia\Package\Server($lia, $fqn='lia:server'); // dir & base_url not required
$http = \Lia\Addon\Http::from($lia);
$http->useTheme = false;
$router = \Lia\Addon\Router::from($lia);
$dir = dirname(__DIR__,2).'/input/ConflictingRoutes/';
// example routes. We want to choose the static routes over the dynamic routes for this example.
$router->addRoute('/bear/sasha/', $dir.'/sasha-bear.php');
$router->addRoute('/bear/t/', $dir.'/jimbo-bear.php');
$router->addRoute('/bear/{bearname}/', $dir.'/dynamic-bear.php');
$router->addRoute('/bear/l/', $dir.'/jimbo-bear.php');
$router->addRoute('/bear/{dynamictwo}/', $dir.'/dynamic-bear2.php');
$router->addRoute('/butterfly/callabletest/', $dir.'/dynamic-bear2.php');
$router->addRoute('/butterfly/callabletest/', function(){});
$this->test("Static route returned when it was added first");
$route_list = $router->route(new \Lia\Obj\Request('/bear/t/'));
$distinct_route = $http->getDistinctRoute($route_list);
$this->compare('/bear/t/', $distinct_route->placeholderPattern());
$this->test("Static route returned when it was added second");
$route_list = $router->route(new \Lia\Obj\Request('/bear/l/'));
$distinct_route = $http->getDistinctRoute($route_list);
$this->compare('/bear/l/', $distinct_route->placeholderPattern());
$this->test("First added dynamic route is returned");
$route_list = $router->route(new \Lia\Obj\Request('/bear/some_bear_name/'));
$distinct_route = $http->getDistinctRoute($route_list);
$this->compare('/bear/{bearname}/', $distinct_route->paramaterizedPattern());
$this->test("Callable returned first when added second");
$route_list = $router->route(new \Lia\Obj\Request('/butterfly/callabletest/'));
$distinct_route = $http->getDistinctRoute($route_list);
$this->compare(true, $distinct_route->isCallable());
$this->test("Callable returned first when added first");
$router->removeRoute('/butterfly/callabletest/');
$router->addRoute('/butterfly/callabletest/', function(){});
$router->addRoute('/butterfly/callabletest/', $dir.'/dynamic-bear2.php');
$route_list = $router->route(new \Lia\Obj\Request('/butterfly/callabletest/'));
$distinct_route = $http->getDistinctRoute($route_list);
$this->compare(true, $distinct_route->isCallable());
$this->test("Static file route returned over dynamic callable route");
$router->addRoute('/static/z/', $dir.'/dynamic-bear2.php');
$router->addRoute('/static/{zed}/', function(){});
$route_list = $router->route(new \Lia\Obj\Request('/static/z/'));
$distinct_route = $http->getDistinctRoute($route_list);
$this->compare($dir.'/dynamic-bear2.php',$distinct_route->target());
}
public function testResolveConflictingRoutes(){
$_SERVER['REQUEST_URI'] = 'na';
$lia = new \Lia();
// Add the built-in Package, which provides all the web-server features.
$server_package = new \Lia\Package\Server($lia, $fqn='lia:server'); // dir & base_url not required
\Lia\Addon\Http::from($lia)->useTheme = false;
$router = \Lia\Addon\Router::from($lia);
$dir = dirname(__DIR__,2).'/input/ConflictingRoutes/';
// @export_start(Router.ResolveConflictingRoutes)
// example routes. We want to choose the static routes over the dynamic routes for this example.
$router->addRoute('/bear/sasha/', $dir.'/sasha-bear.php');
$router->addRoute('/bear/jimbo/', $dir.'/jimbo-bear.php');
$router->addRoute('/bear/{bearname}/', $dir.'/dynamic-bear.php');
\Lia\Addon\Hook::from($lia)
->add(\Lia\Hooks::FILTER_ROUTE,
function (\Lia\Obj\Route $route, array $routeList): bool {
if (count($routeList)==1)return true; // there is only one route, allow it
else if ($route->paramaterizedPattern() == '/bear/{bearname}/'){
// this removes the dynamic route and allows the static routes
return false;
}
// always return true by default
return true;
}
);
// @export_end(Router.ResolveConflictingRoutes)
$sasha_response = \Lia\Addon\Http::from($lia)->getResponse('/bear/sasha/');
$jimbo_response = \Lia\Addon\Http::from($lia)->getResponse('/bear/jimbo/');
$dynamic_response_tommy = \Lia\Addon\Http::from($lia)->getResponse('/bear/tommy/');
$dynamic_response_strawbeary = \Lia\Addon\Http::from($lia)->getResponse('/bear/strawbeary/');
$this->compare('static-sasha', $sasha_response->content);
$this->compare('static-jimbo', $jimbo_response->content);
$this->compare('dynamic-tommy', $dynamic_response_tommy->content);
$this->compare('dynamic-strawbeary', $dynamic_response_strawbeary->content);
}
public function testSetTheme(){
$_SERVER['REQUEST_URI'] = '/';
$dir = $this->file('test/input/Themes/');
$lia = new \Lia();
$main = new \Lia\Package\Server($lia);
$site = new \Lia\Package\Server($lia, 'lia:test', $dir);
$response = \Lia\Addon\Http::from($lia)->getResponse();
$this->str_contains($response->content, "--main theme--", "content", );
\Lia\Addon\Http::from($lia)->setTheme('test:one');
$response = \Lia\Addon\Http::from($lia)->getResponse();
$this->str_contains($response->content, "--one--", "content", );
\Lia\Addon\Http::from($lia)->setTheme('test:two');
$response = \Lia\Addon\Http::from($lia)->getResponse();
$this->str_contains($response->content, "--two--", "content", );
}
public function testResponseContentNoTheme(){
$lia = new \Lia();
$package = new \Lia\Package($lia, 'test:idk');
$server = new \Lia\Addon\Http($package);
$server->useTheme = false;
$hook = new \Lia\Addon\Hook($package);
$router = new \Lia\Addon\Router($package);
$router->addRoute('/test/', [$this, 'sample_content']);
$response = $server->getResponse('/test/');
$this->compare('test-content', $response->content);
}
public function testResponseContentWithTheme(){
$lia = new \Lia();
$package = new \Lia\Package($lia, 'test:idk');
$server = new \Lia\Addon\Http($package);
$view = new \Lia\Addon\View($package);
$view->addViewCallable('theme', [$this, 'theme_view']);
$hook = new \Lia\Addon\Hook($package);
$router = new \Lia\Addon\Router($package);
$router->addRoute('/test/', [$this, 'sample_content']);
$response = $server->getResponse('/test/');
$this->compare('<theme>test-content</theme>', $response->content);
}
public function theme_view($name, $args){
$response = $args['response'];
return '<theme>'.$response->content.'</theme>';
}
public function sample_content($route, $response){
$response->content = 'test-content';
}
}