From 75bccb789dac540d504a80437f85d282ccf0e303 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Wed, 22 May 2024 22:47:20 -0400 Subject: [PATCH] Add some global variables for the templates and test them in the controller. --- app/src/Bootstrap.php | 3 +++ app/tests/Controller/ControllerTest.php | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 7d49681..81e49d7 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -72,6 +72,9 @@ class Bootstrap $templatePath = __DIR__ . "/../templates"; $templateCache = false; $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)); } diff --git a/app/tests/Controller/ControllerTest.php b/app/tests/Controller/ControllerTest.php index 349b887..55f936c 100644 --- a/app/tests/Controller/ControllerTest.php +++ b/app/tests/Controller/ControllerTest.php @@ -75,4 +75,26 @@ class ControllerTest extends TestCase $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("

{{ site_name }}

"); + } + }; + + $app = $this->getAppInstance(); + $app->get('/test', $testController); + + $request = $this->createRequest('GET', '/test'); + $response = $app->handle($request); + + $this->assertEquals('

Slovocast

', $response->getBody()); + } }