Add some global variables for the templates and test them in the controller.

This commit is contained in:
Dave Smith-Hayes 2024-05-22 22:47:20 -04:00
parent 207304b373
commit 75bccb789d
2 changed files with 25 additions and 0 deletions

View File

@ -72,6 +72,9 @@ class Bootstrap
$templatePath = __DIR__ . "/../templates"; $templatePath = __DIR__ . "/../templates";
$templateCache = false; $templateCache = false;
$twig = Twig::create($templatePath, [ 'cache' => $templateCache ]); $twig = Twig::create($templatePath, [ 'cache' => $templateCache ]);
// Add the global variables
$twig->getEnvironment()->addGlobal('site_name', "Slovocast");
$twig->getEnvironment()->addGlobal('site_description', "A no-bullshit podcast hosting service");
$app->add(TwigMiddleware::create($app, $twig)); $app->add(TwigMiddleware::create($app, $twig));
} }

View File

@ -75,4 +75,26 @@ class ControllerTest extends TestCase
$response->getHeaderLine('Content-Type') $response->getHeaderLine('Content-Type')
); );
} }
/**
* We have some Global variables set in the Configuration, for now its
* hardcoded in the Bootstrap class.
*/
public function testRenderSiteName(): void
{
$testController = new class extends Controller {
public function respond(): Response
{
return $this->renderInline("<h1>{{ site_name }}</h1>");
}
};
$app = $this->getAppInstance();
$app->get('/test', $testController);
$request = $this->createRequest('GET', '/test');
$response = $app->handle($request);
$this->assertEquals('<h1>Slovocast</h1>', $response->getBody());
}
} }