Add the Test harness

This commit is contained in:
Dave Smith-Hayes 2024-05-21 22:33:17 -04:00
parent 404d4bbc85
commit 39018f78d9
2 changed files with 78 additions and 0 deletions

76
app/tests/TestCase.php Normal file
View File

@ -0,0 +1,76 @@
<?php
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;
/**
* This is the common test harness pattern lifted right out of the official
* SlimPHP Skeleton application.
*/
class TestCase extends PHPUnit_TestCase
{
/**
* Generate an instance of the Application
*/
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;
}
/**
* @param string $method
* @param string path
* @param array $headers
* @param array $cookies
* @param array $serverParams
*/
protected function createRequest(
string $method,
string $path,
array $headers = [],
array $cookies = [],
array $serverParams = []
): Request {
$uri = new Uri('', '', 80, $path);
$handle = fopen('php://temp', 'w+');
$stream = (new StreamFactory())->createStreamFromResource($handle);
$h = new Headers();
foreach ($headers as $name => $value) {
$h->addHeader($name, $value);
}
return new SlimRequest(
$method,
$uri,
$h,
$cookies,
$serverParams,
$stream
);
}
}

2
app/tests/bootstrap.php Normal file
View File

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