diff --git a/app/composer.json b/app/composer.json index 9ac7c14..ab509b2 100644 --- a/app/composer.json +++ b/app/composer.json @@ -22,7 +22,8 @@ ], "autoload": { "psr-4": { - "Slovocast\\": "src/" + "Slovocast\\": "src/", + "Slovocast\\Tests\\": "tests/" } } } diff --git a/app/composer.lock b/app/composer.lock index 728f3b6..77addb7 100644 --- a/app/composer.lock +++ b/app/composer.lock @@ -281,16 +281,16 @@ }, { "name": "league/flysystem", - "version": "3.27.0", + "version": "3.28.0", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem.git", - "reference": "4729745b1ab737908c7d055148c9a6b3e959832f" + "reference": "e611adab2b1ae2e3072fa72d62c62f52c2bf1f0c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/4729745b1ab737908c7d055148c9a6b3e959832f", - "reference": "4729745b1ab737908c7d055148c9a6b3e959832f", + "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/e611adab2b1ae2e3072fa72d62c62f52c2bf1f0c", + "reference": "e611adab2b1ae2e3072fa72d62c62f52c2bf1f0c", "shasum": "" }, "require": { @@ -314,10 +314,13 @@ "composer/semver": "^3.0", "ext-fileinfo": "*", "ext-ftp": "*", + "ext-mongodb": "^1.3", "ext-zip": "*", "friendsofphp/php-cs-fixer": "^3.5", "google/cloud-storage": "^1.23", + "guzzlehttp/psr7": "^2.6", "microsoft/azure-storage-blob": "^1.1", + "mongodb/mongodb": "^1.2", "phpseclib/phpseclib": "^3.0.36", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^9.5.11|^10.0", @@ -355,32 +358,22 @@ ], "support": { "issues": "https://github.com/thephpleague/flysystem/issues", - "source": "https://github.com/thephpleague/flysystem/tree/3.27.0" + "source": "https://github.com/thephpleague/flysystem/tree/3.28.0" }, - "funding": [ - { - "url": "https://ecologi.com/frankdejonge", - "type": "custom" - }, - { - "url": "https://github.com/frankdejonge", - "type": "github" - } - ], - "time": "2024-04-07T19:17:50+00:00" + "time": "2024-05-22T10:09:12+00:00" }, { "name": "league/flysystem-local", - "version": "3.25.1", + "version": "3.28.0", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem-local.git", - "reference": "61a6a90d6e999e4ddd9ce5adb356de0939060b92" + "reference": "13f22ea8be526ea58c2ddff9e158ef7c296e4f40" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem-local/zipball/61a6a90d6e999e4ddd9ce5adb356de0939060b92", - "reference": "61a6a90d6e999e4ddd9ce5adb356de0939060b92", + "url": "https://api.github.com/repos/thephpleague/flysystem-local/zipball/13f22ea8be526ea58c2ddff9e158ef7c296e4f40", + "reference": "13f22ea8be526ea58c2ddff9e158ef7c296e4f40", "shasum": "" }, "require": { @@ -414,19 +407,9 @@ "local" ], "support": { - "source": "https://github.com/thephpleague/flysystem-local/tree/3.25.1" + "source": "https://github.com/thephpleague/flysystem-local/tree/3.28.0" }, - "funding": [ - { - "url": "https://ecologi.com/frankdejonge", - "type": "custom" - }, - { - "url": "https://github.com/frankdejonge", - "type": "github" - } - ], - "time": "2024-03-15T19:58:44+00:00" + "time": "2024-05-06T20:05:52+00:00" }, { "name": "league/mime-type-detection", @@ -1925,16 +1908,16 @@ }, { "name": "twig/twig", - "version": "v3.10.1", + "version": "v3.10.3", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "3af5ab2e52279e5e23dc192b1a26db3b8cffa4e7" + "reference": "67f29781ffafa520b0bbfbd8384674b42db04572" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/3af5ab2e52279e5e23dc192b1a26db3b8cffa4e7", - "reference": "3af5ab2e52279e5e23dc192b1a26db3b8cffa4e7", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/67f29781ffafa520b0bbfbd8384674b42db04572", + "reference": "67f29781ffafa520b0bbfbd8384674b42db04572", "shasum": "" }, "require": { @@ -1988,7 +1971,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.10.1" + "source": "https://github.com/twigphp/Twig/tree/v3.10.3" }, "funding": [ { @@ -2000,7 +1983,7 @@ "type": "tidelift" } ], - "time": "2024-05-12T06:16:18+00:00" + "time": "2024-05-16T10:04:27+00:00" } ], "packages-dev": [ diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 37f2637..2ff2ff2 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -70,8 +70,9 @@ class Bootstrap // Twig $templatePath = __DIR__ . "/../templates"; - $twig = Twig::create($templatePath, [ 'cache' => false ]); - $app->add(TwigMiddleware::create($app, $twig)); + $templateCache = false; + $twig = Twig::create($templatePath, [ 'cache' => $templateCache ]); + $app->add(TwigMiddleware::create($app)); } /** diff --git a/app/src/Controller/Controller.php b/app/src/Controller/Controller.php index 4fefc92..9364401 100644 --- a/app/src/Controller/Controller.php +++ b/app/src/Controller/Controller.php @@ -13,6 +13,7 @@ abstract class Controller protected Response $response; protected array $args; protected RouteContext $routeContext; + protected Twig $view; /** * Make the Class invokable and pass it into a route as its handler. @@ -31,6 +32,7 @@ abstract class Controller $this->response = $response; $this->args = $args; $this->routeContext = RouteContext::fromRequest($this->request); + $this->view = Twig::fromRequest($request); return $this->respond($request, $response); } @@ -51,7 +53,29 @@ abstract class Controller */ public function render(string $templateName, array $data = []): Response { - $view = Twig::fromRequest($this->request); - return $view->render($this->response, $templateName, $data); + return $this->view->render($this->response, $templateName, $data); + } + + /** + * @param string $html The raw Twig HTML to render + * @param array $data The data to injext into the HTML + * @return Response + */ + public function renderInline(string $html, array $data = []): Response + { + $renderedTemplate = $this->view->fetchFromString($html, $data); + $this->response->getBody()->write($renderedTemplate); + return $this->response; + } + + /** + * @param array|object $data The data to encode as JSON + * @return Response + */ + public function json(array|object $data): Response + { + $encodedData = json_encode($data); + $this->response->getBody()->write($encodedData); + return $this->response->withHeader('Content-Type', 'application/json'); } } diff --git a/app/tests/Controller/ControllerTest.php b/app/tests/Controller/ControllerTest.php new file mode 100644 index 0000000..7c68eae --- /dev/null +++ b/app/tests/Controller/ControllerTest.php @@ -0,0 +1,31 @@ +renderInline( + '
{{ name }}
', + [ 'name' => 'Dave' ] + ); + } + }; + + $app = $this->getAppInstance(); + $app->get('/test', $testController); + + $request = $this->createRequest('GET', '/test'); + $response = $app->handle($request); + + $this->assertEquals('Dave
', $response->getBody()); + } +} diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php index 3ddbe39..b1abbba 100644 --- a/app/tests/TestCase.php +++ b/app/tests/TestCase.php @@ -3,14 +3,13 @@ namespace Slovocast\Tests; use PHPUnit\Framework\TestCase as PHPUnit_TestCase; -use DI\ContainerBuilder; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\App; -use Slim\Factory\AppFactory; use Slim\Psr7\Factory\StreamFactory; use Slim\Psr7\Headers; use Slim\Psr7\Request as SlimRequest; use Slim\Psr7\Uri; +use Slovocast\Bootstrap; /** * This is the common test harness pattern lifted right out of the official @@ -23,22 +22,7 @@ class TestCase extends PHPUnit_TestCase */ protected function getAppInstance(): App { - $containerBuilder = new ContainerBuilder(); - - // add settings too container - - // add dependencies to container - - // add repositories - - $container = $containerBuilder->build(); - $app = AppFactory::setContainer($container); - - // add middleware - - // add routes - - return $app; + return Bootstrap::init(); } /** diff --git a/app/tests/bootstrap.php b/app/tests/bootstrap.php deleted file mode 100644 index a4abe2d..0000000 --- a/app/tests/bootstrap.php +++ /dev/null @@ -1,2 +0,0 @@ -