From a467b0289a57e4f5731bc9fe41192575c5fd8dcb Mon Sep 17 00:00:00 2001 From: Denis Brumann Date: Sun, 4 Feb 2018 16:34:45 +0100 Subject: [PATCH] Add TemplateController for even more effortless templating. --- README.md | 43 +++++++++++- composer.json | 3 +- composer.lock | 6 +- src/Controller/TemplateController.php | 42 ++++++++++++ tests/Controller/TemplateControllerTest.php | 74 +++++++++++++++++++++ 5 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 src/Controller/TemplateController.php create mode 100644 tests/Controller/TemplateControllerTest.php diff --git a/README.md b/README.md index ff3f6e0..cff255a 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,12 @@ The bundle can be installed via Composer: composer require jaem3l/template-bundle -Usage Examples --------------- +When using this bundle inside a Symfony 4 application bootstrapped with Flex, +make sure you have installed the twig-bundle and set it up with its recipe, e.g. +by using `composer req twig`. + +Usage examples for Route-annotation +----------------------------------- twig = $twig; + } + + public function templateAction(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response + { + $twigTemplate = $this->twig->createTemplate($template); + + $response = new Response($twigTemplate->render([])); + + if ($maxAge) { + $response->setMaxAge($maxAge); + } + if ($sharedMaxAge) { + $response->setSharedMaxAge($sharedMaxAge); + } + if ($private) { + $response->setPrivate(); + } elseif (false === $private || (null === $private && ($maxAge || $sharedMaxAge))) { + $response->setPublic(); + } + + return $response; + } + + public function __invoke(string $template, ?int $maxAge, ?int $sharedMaxAge, ?bool $private): Response + { + return $this->templateAction($template, $maxAge, $sharedMaxAge, $private); + } +} diff --git a/tests/Controller/TemplateControllerTest.php b/tests/Controller/TemplateControllerTest.php new file mode 100644 index 0000000..a59842a --- /dev/null +++ b/tests/Controller/TemplateControllerTest.php @@ -0,0 +1,74 @@ +controller = new TemplateController($twig); + } + + public function test_it_renders_basic_template() + { + $template = 'Hello World!'; + + $response = $this->controller->templateAction($template, null, null, null); + + $this->assertSame($template, $response->getContent()); + $this->assertSame('no-cache, private', $response->headers->get('Cache-Control')); + } + + public function test_it_sets_max_age_on_response() + { + $template = 'Hello World!'; + + $response = $this->controller->templateAction($template, 360, null, null); + + $this->assertContains('max-age=360', $response->headers->get('Cache-Control')); + $this->assertContains('public', $response->headers->get('Cache-Control')); + } + + public function test_it_sets_shared_max_age_on_response() + { + $template = 'Hello World!'; + + $response = $this->controller->templateAction($template, null, 360, null); + + $this->assertContains('s-maxage=360', $response->headers->get('Cache-Control')); + $this->assertContains('public', $response->headers->get('Cache-Control')); + } + + public function test_it_sets_cache_private_on_response() + { + $template = 'Hello World!'; + + $response = $this->controller->templateAction($template, null, null, true); + + $this->assertContains('private', $response->headers->get('Cache-Control')); + } + + public function test_it_sets_cache_public_on_response() + { + $template = 'Hello World!'; + + $response = $this->controller->templateAction($template, null, null, false); + + $this->assertContains('public', $response->headers->get('Cache-Control')); + } +}