Added test suite

This commit is contained in:
Sergio Brighenti 2020-04-15 00:08:51 +02:00
parent 2303cb263f
commit 1bb05f5c2b
9 changed files with 1635 additions and 10 deletions

View file

@ -19,7 +19,6 @@ use App\Middleware\AuthMiddleware;
use App\Middleware\CheckForMaintenanceMiddleware;
use Slim\Routing\RouteCollectorProxy;
global $app;
$app->group('', function (RouteCollectorProxy $group) {
$group->get('/home[/page/{page}]', [DashboardController::class, 'home'])->setName('home');
$group->get('/upload', [UploadController::class, 'uploadWebPage'])->setName('upload.web.show');

View file

@ -17,11 +17,11 @@ use Psr\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
if (!file_exists('config.php') && is_dir('install/')) {
if (!file_exists(BASE_DIR.'config.php') && is_dir(BASE_DIR.'install/')) {
header('Location: ./install/');
exit();
} else {
if (!file_exists('config.php') && !is_dir('install/')) {
if (!file_exists(BASE_DIR.'config.php') && !is_dir(BASE_DIR.'install/')) {
exit('Cannot find the config file.');
}
}
@ -71,6 +71,7 @@ $builder->addDefinitions([
$builder->addDefinitions(__DIR__.'/container.php');
global $app;
$app = Bridge::create($builder->build());
$app->getContainer()->set('config', $config);
$app->setBasePath(parse_url($config['base_url'], PHP_URL_PATH) ?: '');

View file

@ -38,14 +38,14 @@
"App\\": "app/"
}
},
"extra": {
"violinist": {
"allow_updates_beyond_constraint": 1,
"blacklist": [],
"update_with_dependencies": 1
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require-dev": {
"phpstan/phpstan": "^0.11.5"
"phpstan/phpstan": "^0.11.5",
"symfony/browser-kit": "^4.4",
"phpunit/phpunit": "7.5"
}
}

1532
composer.lock generated

File diff suppressed because it is too large Load diff

27
phpunit.xml Normal file
View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
stopOnFailure="false"
cacheResult="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory>bin</directory>
<directory>docs</directory>
<directory>resources</directory>
<directory>vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

25
tests/Client.php Normal file
View file

@ -0,0 +1,25 @@
<?php
namespace Tests;
use GuzzleHttp\Psr7\ServerRequest;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\Response;
class Client extends AbstractBrowser
{
protected function doRequest($request)
{
define('BASE_DIR', realpath(__DIR__.'/../').DIRECTORY_SEPARATOR);
define('PLATFORM_VERSION', json_decode(file_get_contents(BASE_DIR.'composer.json'))->version);
/** @var \Slim\App $app */
$app = require_once BASE_DIR.'bootstrap/app.php';
$response = $app->handle(new ServerRequest($request->getMethod(), $request->getUri(), [], $request->getContent()));
return new Response($response->getBody()->getContents(), $response->getStatusCode(), $response->getHeaders());
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
class LoginControllerTest extends TestCase
{
/** @test */
public function it_loads_the_login_page()
{
$this->client->request('GET', '/login');
//$this->client->getResponse();
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
}
}

20
tests/TestCase.php Normal file
View file

@ -0,0 +1,20 @@
<?php
namespace Tests;
use PHPUnit\Framework\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
/** @var Client */
protected $client;
protected function setUp()
{
$_SERVER['HTTP_HOST'] = 'http://localhost';
$_SERVER['HTTPS'] = false;
$this->client = new Client();
}
}

3
tests/bootstrap.php Normal file
View file

@ -0,0 +1,3 @@
<?php
ob_start();