Skip to content

Commit

Permalink
CHORE: Better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed Dec 14, 2022
1 parent c776bf5 commit c32662e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 55 deletions.
15 changes: 0 additions & 15 deletions cli-config.php

This file was deleted.

5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
],
"scripts": {
"fix": "php-cs-fixer fix",
"test": "phpunit --testdox",
"migrate": "doctrine-migrations migrate",
"migrate:generate": "doctrine-migrations generate",
"migrate:status": "doctrine-migrations status"
"test": "phpunit --testdox"
}
}
2 changes: 1 addition & 1 deletion config/databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'user' => $_ENV['DATABASE_USER'] ?? '',
'password' => $_ENV['DATABASE_PASSWORD'] ?? '',
'host' => $_ENV['DATABASE_HOST'] ?? '',
'driver' => $_ENV['DATABASE_DRIVER'] ?? '',
'driver' => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql',
],

/** Memory Databases */
Expand Down
11 changes: 11 additions & 0 deletions public/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Gravatalonga\Framework\ValueObject\Path;
use Gravatalonga\KingFoundation\Kernel;

/**
* Create a new Kernel Application
*/
$app = new Kernel(new Path(__DIR__ . '/../'));

return $app;
13 changes: 4 additions & 9 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
<?php

use Gravatalonga\Framework\ValueObject\Path;
use Gravatalonga\KingFoundation\Kernel;
use Middlewares\Whoops;

require_once "../vendor/autoload.php";

chdir(__DIR__ . '/../');

$app = new Kernel(new Path(getcwd()));

$app->add(new Whoops());
/**
* @var \Gravatalonga\KingFoundation\Kernel $app
*/
$app = require_once "bootstrap.php";

$app->run();
13 changes: 13 additions & 0 deletions tests/CreateApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests;

use Gravatalonga\KingFoundation\Kernel;

trait CreateApplication
{
public function createApplication()
{
$this->app = require_once __DIR__ . "/../public/bootstrap.php";
}
}
36 changes: 10 additions & 26 deletions tests/HelloWorldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,36 @@
namespace Tests;

use Gravatalonga\KingFoundation\Kernel;
use PHPUnit\Framework\TestCase;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Headers;
use Gravatalonga\KingFoundation\Testing\TraitRequest;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Slim\Psr7\Uri;

/**
* @covers \Gravatalonga\KingFoundation\Kernel
*/
class HelloWorldTest extends TestCase
{
use TraitRequest;

public function setUp(): void
{
$this->createApplication();
}

/**
* @test
*/
public function it_can_handle_request()
{
$http = new Kernel();

$http->get('/get', function (Request $request, Response $response) {
$this->app->get('/get', function (Request $request, Response $response) {
$response->getBody()->write('Hello World');
return $response;
});

$response = $http->handle($this->createRequest('GET', '/get'));
$response = $this->app->handle($this->createRequest('GET', '/get'));
$body = $response->getBody();

$this->assertNotEmpty($body);
$this->assertEquals('Hello World', $body);
}

public function createRequest(string $method, string $uri, ?string $payload = null, array $headers = []): Request
{
$handle = fopen('php://temp', 'w+');
$stream = (new StreamFactory())->createStreamFromResource($handle);

if (! empty($payload)) {
$stream->write($payload);
}

$uri = new Uri('', '', 80, $uri);
$h = new Headers();
foreach ($headers as $key => $value) {
$h->addHeader($key, $value);
}

return new Request($method, $uri, $h, [], [], $stream);
}
}
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests;

use Gravatalonga\KingFoundation\Kernel;
use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
use CreateApplication;

protected Kernel $app;
}

0 comments on commit c32662e

Please sign in to comment.