Skip to content

Commit

Permalink
Merge pull request #45 from maurobonfietti/0.29.0
Browse files Browse the repository at this point in the history
Version 0.29.0
  • Loading branch information
maurobonfietti committed Nov 15, 2020
2 parents bb36da0 + dbca3d7 commit f625b88
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 48 deletions.
20 changes: 11 additions & 9 deletions src/App/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/DotEnv.php';
require __DIR__ . '/Container.php';
require __DIR__ . '/ErrorHandler.php';
require __DIR__ . '/Middlewares.php';
require __DIR__ . '/Cors.php';
require __DIR__ . '/Database.php';
require __DIR__ . '/Services.php';
require __DIR__ . '/Repositories.php';
require __DIR__ . '/Routes.php';
require __DIR__ . '/NotFound.php';
$app = require __DIR__ . '/Container.php';
$customErrorHandler = require __DIR__ . '/ErrorHandler.php';
(require __DIR__ . '/Middlewares.php')($app, $customErrorHandler);
(require __DIR__ . '/Cors.php')($app);
(require __DIR__ . '/Database.php');
(require __DIR__ . '/Services.php');
(require __DIR__ . '/Repositories.php');
(require __DIR__ . '/Routes.php');
(require __DIR__ . '/NotFound.php')($app);

return $app;
2 changes: 2 additions & 0 deletions src/App/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

$container = new Container();
$app = AppFactory::create(null, new Psr11Container($container));

return $app;
35 changes: 19 additions & 16 deletions src/App/Cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;

$app->options('/{routes:.+}', function (Request $request, Response $response) {
return $response;
});
return function (App $app): void {
$app->options('/{routes:.+}', function (Request $request, Response $response) {
return $response;
});

$app->add(function (Request $request, $handler): Response {
$response = $handler->handle($request);
$app->add(function (Request $request, $handler): Response {
$response = $handler->handle($request);

return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader(
'Access-Control-Allow-Headers',
'X-Requested-With, Content-Type, Accept, Origin, Authorization'
)
->withHeader(
'Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE, PATCH, OPTIONS'
);
});
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader(
'Access-Control-Allow-Headers',
'X-Requested-With, Content-Type, Accept, Origin, Authorization'
)
->withHeader(
'Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE, PATCH, OPTIONS'
);
});
};
2 changes: 2 additions & 0 deletions src/App/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@
->withStatus($statusCode)
->withHeader('Content-type', 'application/problem+json');
};

return $customErrorHandler;
24 changes: 14 additions & 10 deletions src/App/Middlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

declare(strict_types=1);

$path = $_SERVER['SLIM_BASE_PATH'] ?? '';
$app->setBasePath($path);
$app->addRoutingMiddleware();
$app->addBodyParsingMiddleware();
$displayError = filter_var(
$_SERVER['DISPLAY_ERROR_DETAILS'] ?? false,
FILTER_VALIDATE_BOOLEAN
);
$errorMiddleware = $app->addErrorMiddleware($displayError, true, true);
$errorMiddleware->setDefaultErrorHandler($customErrorHandler);
use Slim\App;

return function (App $app, $customErrorHandler): void {
$path = $_SERVER['SLIM_BASE_PATH'] ?? '';
$app->setBasePath($path);
$app->addRoutingMiddleware();
$app->addBodyParsingMiddleware();
$displayError = filter_var(
$_SERVER['DISPLAY_ERROR_DETAILS'] ?? false,
FILTER_VALIDATE_BOOLEAN
);
$errorMiddleware = $app->addErrorMiddleware($displayError, true, true);
$errorMiddleware->setDefaultErrorHandler($customErrorHandler);
};
13 changes: 8 additions & 5 deletions src/App/NotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
declare(strict_types=1);

use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;

$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}',
function (Request $request): void {
throw new Slim\Exception\HttpNotFoundException($request);
}
);
return function (App $app): void {
$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}',
function (Request $request): void {
throw new Slim\Exception\HttpNotFoundException($request);
}
);
};
2 changes: 1 addition & 1 deletion src/Controller/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Home
{
private const API_NAME = 'slim4-api-skeleton';

private const API_VERSION = '0.28.0';
private const API_VERSION = '0.29.0';

/** @var Container */
private $container;
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/HomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class HomeTest extends TestCase
{
public function testGetHelp()
public function testGetHelp(): void
{
$request = $this->createRequest('GET', '/');
$response = $this->getAppInstance()->handle($request);
Expand All @@ -20,7 +20,7 @@ public function testGetHelp()
$this->assertStringNotContainsString('error', $result);
}

public function testGetStatus()
public function testGetStatus(): void
{
$request = $this->createRequest('GET', '/status');
$response = $this->getAppInstance()->handle($request);
Expand All @@ -37,15 +37,15 @@ public function testGetStatus()
$this->assertStringNotContainsString('PDOException', $result);
}

public function testPreflightOptions()
public function testPreflightOptions(): void
{
$request = $this->createRequest('OPTIONS', '/status');
$response = $this->getAppInstance()->handle($request);

$this->assertEquals(200, $response->getStatusCode());
}

public function testRouteNotFoundException()
public function testRouteNotFoundException(): void
{
$request = $this->createRequest('GET', '/notfound');
$response = $this->getAppInstance()->handle($request);
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
namespace Tests\integration;

use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
use Slim\App;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Headers;
use Slim\Psr7\Request as SlimRequest;
use Slim\Psr7\Uri;

class TestCase extends PHPUnit_TestCase
{
protected function getAppInstance()
protected function getAppInstance(): App
{
require __DIR__ . '/../../src/App/App.php';
$app = require __DIR__ . '/../../src/App/App.php';

return $app;
}
Expand All @@ -25,7 +26,7 @@ protected function createRequest(
array $headers = ['HTTP_ACCEPT' => 'application/json'],
array $cookies = [],
array $serverParams = []
) {
): SlimRequest {
$uri = new Uri('', '', 80, $path);
$handle = fopen('php://temp', 'w+');
$stream = (new StreamFactory())->createStreamFromResource($handle);
Expand Down

0 comments on commit f625b88

Please sign in to comment.