Add a controller test.

This commit is contained in:
Dave Smith-Hayes 2024-05-22 20:39:33 -04:00
parent 39018f78d9
commit 050eb87e83
7 changed files with 85 additions and 63 deletions

View File

@ -22,7 +22,8 @@
], ],
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Slovocast\\": "src/" "Slovocast\\": "src/",
"Slovocast\\Tests\\": "tests/"
} }
} }
} }

59
app/composer.lock generated
View File

@ -281,16 +281,16 @@
}, },
{ {
"name": "league/flysystem", "name": "league/flysystem",
"version": "3.27.0", "version": "3.28.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/thephpleague/flysystem.git", "url": "https://github.com/thephpleague/flysystem.git",
"reference": "4729745b1ab737908c7d055148c9a6b3e959832f" "reference": "e611adab2b1ae2e3072fa72d62c62f52c2bf1f0c"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/thephpleague/flysystem/zipball/4729745b1ab737908c7d055148c9a6b3e959832f", "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/e611adab2b1ae2e3072fa72d62c62f52c2bf1f0c",
"reference": "4729745b1ab737908c7d055148c9a6b3e959832f", "reference": "e611adab2b1ae2e3072fa72d62c62f52c2bf1f0c",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -314,10 +314,13 @@
"composer/semver": "^3.0", "composer/semver": "^3.0",
"ext-fileinfo": "*", "ext-fileinfo": "*",
"ext-ftp": "*", "ext-ftp": "*",
"ext-mongodb": "^1.3",
"ext-zip": "*", "ext-zip": "*",
"friendsofphp/php-cs-fixer": "^3.5", "friendsofphp/php-cs-fixer": "^3.5",
"google/cloud-storage": "^1.23", "google/cloud-storage": "^1.23",
"guzzlehttp/psr7": "^2.6",
"microsoft/azure-storage-blob": "^1.1", "microsoft/azure-storage-blob": "^1.1",
"mongodb/mongodb": "^1.2",
"phpseclib/phpseclib": "^3.0.36", "phpseclib/phpseclib": "^3.0.36",
"phpstan/phpstan": "^1.10", "phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.5.11|^10.0", "phpunit/phpunit": "^9.5.11|^10.0",
@ -355,32 +358,22 @@
], ],
"support": { "support": {
"issues": "https://github.com/thephpleague/flysystem/issues", "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": [ "time": "2024-05-22T10:09:12+00:00"
{
"url": "https://ecologi.com/frankdejonge",
"type": "custom"
},
{
"url": "https://github.com/frankdejonge",
"type": "github"
}
],
"time": "2024-04-07T19:17:50+00:00"
}, },
{ {
"name": "league/flysystem-local", "name": "league/flysystem-local",
"version": "3.25.1", "version": "3.28.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/thephpleague/flysystem-local.git", "url": "https://github.com/thephpleague/flysystem-local.git",
"reference": "61a6a90d6e999e4ddd9ce5adb356de0939060b92" "reference": "13f22ea8be526ea58c2ddff9e158ef7c296e4f40"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/thephpleague/flysystem-local/zipball/61a6a90d6e999e4ddd9ce5adb356de0939060b92", "url": "https://api.github.com/repos/thephpleague/flysystem-local/zipball/13f22ea8be526ea58c2ddff9e158ef7c296e4f40",
"reference": "61a6a90d6e999e4ddd9ce5adb356de0939060b92", "reference": "13f22ea8be526ea58c2ddff9e158ef7c296e4f40",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -414,19 +407,9 @@
"local" "local"
], ],
"support": { "support": {
"source": "https://github.com/thephpleague/flysystem-local/tree/3.25.1" "source": "https://github.com/thephpleague/flysystem-local/tree/3.28.0"
}, },
"funding": [ "time": "2024-05-06T20:05:52+00:00"
{
"url": "https://ecologi.com/frankdejonge",
"type": "custom"
},
{
"url": "https://github.com/frankdejonge",
"type": "github"
}
],
"time": "2024-03-15T19:58:44+00:00"
}, },
{ {
"name": "league/mime-type-detection", "name": "league/mime-type-detection",
@ -1925,16 +1908,16 @@
}, },
{ {
"name": "twig/twig", "name": "twig/twig",
"version": "v3.10.1", "version": "v3.10.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/twigphp/Twig.git", "url": "https://github.com/twigphp/Twig.git",
"reference": "3af5ab2e52279e5e23dc192b1a26db3b8cffa4e7" "reference": "67f29781ffafa520b0bbfbd8384674b42db04572"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/3af5ab2e52279e5e23dc192b1a26db3b8cffa4e7", "url": "https://api.github.com/repos/twigphp/Twig/zipball/67f29781ffafa520b0bbfbd8384674b42db04572",
"reference": "3af5ab2e52279e5e23dc192b1a26db3b8cffa4e7", "reference": "67f29781ffafa520b0bbfbd8384674b42db04572",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1988,7 +1971,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/twigphp/Twig/issues", "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": [ "funding": [
{ {
@ -2000,7 +1983,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-05-12T06:16:18+00:00" "time": "2024-05-16T10:04:27+00:00"
} }
], ],
"packages-dev": [ "packages-dev": [

View File

@ -70,8 +70,9 @@ class Bootstrap
// Twig // Twig
$templatePath = __DIR__ . "/../templates"; $templatePath = __DIR__ . "/../templates";
$twig = Twig::create($templatePath, [ 'cache' => false ]); $templateCache = false;
$app->add(TwigMiddleware::create($app, $twig)); $twig = Twig::create($templatePath, [ 'cache' => $templateCache ]);
$app->add(TwigMiddleware::create($app));
} }
/** /**

View File

@ -13,6 +13,7 @@ abstract class Controller
protected Response $response; protected Response $response;
protected array $args; protected array $args;
protected RouteContext $routeContext; protected RouteContext $routeContext;
protected Twig $view;
/** /**
* Make the Class invokable and pass it into a route as its handler. * Make the Class invokable and pass it into a route as its handler.
@ -31,6 +32,7 @@ abstract class Controller
$this->response = $response; $this->response = $response;
$this->args = $args; $this->args = $args;
$this->routeContext = RouteContext::fromRequest($this->request); $this->routeContext = RouteContext::fromRequest($this->request);
$this->view = Twig::fromRequest($request);
return $this->respond($request, $response); return $this->respond($request, $response);
} }
@ -51,7 +53,29 @@ abstract class Controller
*/ */
public function render(string $templateName, array $data = []): Response public function render(string $templateName, array $data = []): Response
{ {
$view = Twig::fromRequest($this->request); return $this->view->render($this->response, $templateName, $data);
return $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');
} }
} }

View File

@ -0,0 +1,31 @@
<?php
namespace Slovocast\Tests\Controller;
use Slovocast\Tests\TestCase;
use Slovocast\Controller\Controller;
use Psr\Http\Message\ResponseInterface as Response;
class ControllerTest extends TestCase
{
public function testHtmlInlineResponse(): void
{
$testController = new class extends Controller {
public function respond(): Response
{
return $this->renderInline(
'<p>{{ name }}</p>',
[ 'name' => 'Dave' ]
);
}
};
$app = $this->getAppInstance();
$app->get('/test', $testController);
$request = $this->createRequest('GET', '/test');
$response = $app->handle($request);
$this->assertEquals('<p>Dave</p>', $response->getBody());
}
}

View File

@ -3,14 +3,13 @@
namespace Slovocast\Tests; namespace Slovocast\Tests;
use PHPUnit\Framework\TestCase as PHPUnit_TestCase; use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
use DI\ContainerBuilder;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App; use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Psr7\Factory\StreamFactory; use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Headers; use Slim\Psr7\Headers;
use Slim\Psr7\Request as SlimRequest; use Slim\Psr7\Request as SlimRequest;
use Slim\Psr7\Uri; use Slim\Psr7\Uri;
use Slovocast\Bootstrap;
/** /**
* This is the common test harness pattern lifted right out of the official * 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 protected function getAppInstance(): App
{ {
$containerBuilder = new ContainerBuilder(); return Bootstrap::init();
// add settings too container
// add dependencies to container
// add repositories
$container = $containerBuilder->build();
$app = AppFactory::setContainer($container);
// add middleware
// add routes
return $app;
} }
/** /**

View File

@ -1,2 +0,0 @@
<?php