Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ charset = utf-8

[*.md]
indent_size = 2

[tests/views/*.php]
insert_final_newline = false
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@
},
"autoload-dev": {
"classmap": [
"tests/classes/User.php",
"tests/classes/Hello.php",
"tests/classes/Factory.php",
"tests/classes/TesterClass.php"
]
"tests/classes/"
],
"psr-4": {
"Tests\\PHP8\\": [
"tests/named-arguments"
]
}
},
"require-dev": {
"ext-pdo_sqlite": "*",
"flightphp/container": "^1.0",
"flightphp/runway": "^0.2.3 || ^1.0",
"league/container": "^4.2",
"level-2/dice": "^4.0",
Expand Down
5 changes: 3 additions & 2 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use flight\template\View;
use Throwable;
use flight\net\Route;
use Psr\Container\ContainerInterface;

/**
* The Engine class contains the core functionality of the framework.
Expand Down Expand Up @@ -265,9 +266,9 @@ public function handleException(Throwable $e): void
/**
* Registers the container handler
*
* @param callable|object $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes
* @template T of object
*
* @return void
* @param ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes
*/
public function registerContainerHandler($containerHandler): void
{
Expand Down
12 changes: 10 additions & 2 deletions flight/Flight.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use flight\template\View;
use flight\net\Route;
use flight\core\EventDispatcher;
use Psr\Container\ContainerInterface;

require_once __DIR__ . '/autoload.php';

Expand All @@ -18,17 +19,22 @@
* @license MIT, http://flightphp.com/license
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
*
* @template T of object
*
* # Core methods
* @method static void start() Starts the framework.
* @method static void path(string $path) Adds a path for autoloading classes.
* @method static void path(string $dir) Adds a path for autoloading classes.
* @method static void stop(?int $code = null) Stops the framework and sends a response.
* @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true)
* Stop the framework with an optional status code and message.
* @method static void register(string $name, string $class, array<int, mixed> $params = [], ?callable $callback = null)
* Registers a class to a framework method.
* @method static void unregister(string $methodName)
* Unregisters a class to a framework method.
* @method static void registerContainerHandler(callable|object $containerHandler) Registers a container handler.
* @method static void registerContainerHandler(ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler) Registers a container handler.
*
* # Class registration
* @method EventDispatcher eventDispatcher() Gets event dispatcher
*
* # Class registration
* @method EventDispatcher eventDispatcher() Gets event dispatcher
Expand Down Expand Up @@ -104,6 +110,7 @@ class Flight
*/
private function __construct()
{
//
}

/**
Expand All @@ -114,6 +121,7 @@ private function __construct()
*/
private function __clone()
{
//
}

/**
Expand Down
2 changes: 1 addition & 1 deletion flight/commands/RouteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function shouldAddRoute(Route $route)
if ($showAll === true) {
$boolval = true;
} else {
$methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' ];
$methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
foreach ($methods as $method) {
$lowercaseMethod = strtolower($method);
if (
Expand Down
17 changes: 10 additions & 7 deletions flight/core/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class Dispatcher
/**
* Sets the dependency injection container handler.
*
* @param ContainerInterface|(callable(string $classString, array<int, mixed> $params): (null|object)) $containerHandler
* @template T of object
*
* @param ContainerInterface|(callable(class-string<T> $classString, array<int, mixed> $params): ?T) $containerHandler
* Dependency injection container.
*
* @throws InvalidArgumentException If $containerHandler is not a `callable` or instance of `Psr\Container\ContainerInterface`.
Expand Down Expand Up @@ -231,7 +233,7 @@ public function hook(string $name, string $type, callable $callback): self

if ($parametersNumber === 1) {
/** @disregard &$params in after filters are deprecated. */
$callback = fn (array &$params, &$output) => $callback($output);
$callback = fn(array &$params, &$output) => $callback($output);
}
}

Expand Down Expand Up @@ -437,11 +439,12 @@ protected function verifyValidClassCallable($class, $method, $resolvedClass): vo
public function resolveContainerClass(string $class, array &$params)
{
// PSR-11
if (
is_a($this->containerHandler, '\Psr\Container\ContainerInterface')
&& $this->containerHandler->has($class)
) {
return $this->containerHandler->get($class);
if (is_a($this->containerHandler, '\Psr\Container\ContainerInterface')) {
try {
return $this->containerHandler->get($class);
} catch (Throwable $exception) {
return null;
}
}

// Just a callable where you configure the behavior (Dice, PHP-DI, etc.)
Expand Down
4 changes: 2 additions & 2 deletions flight/net/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ public function init(array $properties = []): self
$this->data->setData($data);
}
}
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
} elseif (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) {
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
} elseif (in_array($this->method, ['PUT', 'DELETE', 'PATCH'], true) === true) {
$body = $this->getBody();
if ($body !== '') {
$data = [];
Expand Down
2 changes: 1 addition & 1 deletion flight/net/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Route
*/
public function __construct(string $pattern, $callback, array $methods, bool $pass, string $alias = '')
{
$this->pattern = $pattern;
$this->pattern = str_replace('//', '/', $pattern);
$this->callback = $callback;
$this->methods = $methods;
$this->pass = $pass;
Expand Down
1 change: 1 addition & 0 deletions flight/util/ReturnTypeWillChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
// This file is only here so that the PHP8 attribute for doesn't throw an error in files
class ReturnTypeWillChange
{
//
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
excludePaths:
- vendor
- flight/util/ReturnTypeWillChange.php
- tests/named-arguments
paths:
- flight
- index.php
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<testsuites>
<testsuite name="default">
<directory>tests/</directory>
<exclude>tests/named-arguments/</exclude>
</testsuite>
</testsuites>
<logging />
Expand Down
1 change: 0 additions & 1 deletion tests/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace tests;

use ErrorException;
use Exception;
use flight\database\PdoWrapper;
use flight\Engine;
Expand Down
21 changes: 16 additions & 5 deletions tests/FlightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ public function testStaticRouteGroup()
Flight::start();
}

public function testStaticNestedGroups(): void {
Flight::group('/', static function (): void {
Flight::group('/', static function (): void {
Flight::route('GET /', static function (): void {
echo "test";
});
});
});

Flight::request()->url = '/';

$this->expectOutputString('test');
Flight::start();
}

public function testStaticRouteGet()
{

Expand Down Expand Up @@ -381,15 +396,11 @@ public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
$html = <<<'html'
<div>Hi</div>
<div>Hi</div>

<input type="number" />

<input type="number" />

html;

// if windows replace \n with \r\n
$html = str_replace("\n", PHP_EOL, $html);
$html = str_replace(["\n", "\r"], '', $html);

$this->expectOutputString($html);

Expand Down
17 changes: 4 additions & 13 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,12 @@ public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
$html = <<<'html'
<div>Hi</div>
<div>Hi</div>

<input type="number" />

<input type="number" />

html;

// if windows replace \n with \r\n
$html = str_replace("\n", PHP_EOL, $html);
$html = str_replace(["\n", "\r"], '', $html);

$this->expectOutputString($html);

Expand All @@ -205,11 +202,9 @@ public function testKeepThePreviousStateOfDataSettedBySetMethod(): void
$html = <<<'html'
<div>qux</div>
<div>bar</div>

html;

// if windows replace \n with \r\n
$html = str_replace("\n", PHP_EOL, $html);
$html = str_replace(["\n", "\r"], '', $html);

$this->expectOutputString($html);

Expand All @@ -222,19 +217,15 @@ public static function renderDataProvider(): array
$html1 = <<<'html'
<div>Hi</div>
<div></div>

html;

$html2 = <<<'html'

<input type="number" />

<input type="text" />

html;

$html1 = str_replace("\n", PHP_EOL, $html1);
$html2 = str_replace("\n", PHP_EOL, $html2);
$html1 = str_replace(["\n", "\r"], '', $html1);
$html2 = str_replace(["\n", "\r"], '', $html2);

return [
[
Expand Down
6 changes: 3 additions & 3 deletions tests/commands/ControllerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public function tearDown(): void

protected function newApp(string $name, string $version = '')
{
$app = new Application($name, $version ?: '0.0.1', fn () => false);
$app = @new Application($name, $version ?: '0.0.1', fn () => false);

return $app->io(new Interactor(static::$in, static::$ou));
return @$app->io(new Interactor(static::$in, static::$ou));
}

public function testConfigAppRootNotSet()
{
$app = $this->newApp('test', '0.0.1');
$app->add(new ControllerCommand([]));
$app->handle(['runway', 'make:controller', 'Test']);
@$app->handle(['runway', 'make:controller', 'Test']);

$this->assertStringContainsString('app_root not set in .runway-config.json', file_get_contents(static::$ou));
}
Expand Down
57 changes: 36 additions & 21 deletions tests/commands/RouteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function tearDown(): void

protected function newApp(string $name, string $version = '')
{
$app = new Application($name, $version ?: '0.0.1', fn() => false);
$app = @new Application($name, $version ?: '0.0.1', fn() => false);

return $app->io(new Interactor(static::$in, static::$ou));
}
Expand Down Expand Up @@ -90,44 +90,59 @@ protected function removeColors(string $str): string

public function testConfigIndexRootNotSet()
{
$app = $this->newApp('test', '0.0.1');
$app = @$this->newApp('test', '0.0.1');
$app->add(new RouteCommand([]));
$app->handle(['runway', 'routes']);
@$app->handle(['runway', 'routes']);

$this->assertStringContainsString('index_root not set in .runway-config.json', file_get_contents(static::$ou));
}

public function testGetRoutes()
{
$app = $this->newApp('test', '0.0.1');
$app = @$this->newApp('test', '0.0.1');
$this->createIndexFile();
$app->add(new RouteCommand(['index_root' => 'tests/commands/index.php']));
$app->handle(['runway', 'routes']);
@$app->handle(['runway', 'routes']);

$this->assertStringContainsString('Routes', file_get_contents(static::$ou));
$this->assertStringContainsString('+---------+-----------+-------+----------+----------------+
| Pattern | Methods | Alias | Streamed | Middleware |
+---------+-----------+-------+----------+----------------+
| / | GET, HEAD | | No | - |
| /post | POST | | No | Closure |
| /delete | DELETE | | No | - |
| /put | PUT | | No | - |
| /patch | PATCH | | No | Bad Middleware |
+---------+-----------+-------+----------+----------------+', $this->removeColors(file_get_contents(static::$ou)));
$expected = <<<'output'
+---------+-----------+-------+----------+----------------+
| Pattern | Methods | Alias | Streamed | Middleware |
+---------+-----------+-------+----------+----------------+
| / | GET, HEAD | | No | - |
| /post | POST | | No | Closure |
| /delete | DELETE | | No | - |
| /put | PUT | | No | - |
| /patch | PATCH | | No | Bad Middleware |
+---------+-----------+-------+----------+----------------+
output;

$this->assertStringContainsString(
$expected,
$this->removeColors(file_get_contents(static::$ou))
);
}

public function testGetPostRoute()
{
$app = $this->newApp('test', '0.0.1');
$app = @$this->newApp('test', '0.0.1');
$this->createIndexFile();
$app->add(new RouteCommand(['index_root' => 'tests/commands/index.php']));
$app->handle(['runway', 'routes', '--post']);
@$app->handle(['runway', 'routes', '--post']);

$this->assertStringContainsString('Routes', file_get_contents(static::$ou));
$this->assertStringContainsString('+---------+---------+-------+----------+------------+
| Pattern | Methods | Alias | Streamed | Middleware |
+---------+---------+-------+----------+------------+
| /post | POST | | No | Closure |
+---------+---------+-------+----------+------------+', $this->removeColors(file_get_contents(static::$ou)));

$expected = <<<'output'
+---------+---------+-------+----------+------------+
| Pattern | Methods | Alias | Streamed | Middleware |
+---------+---------+-------+----------+------------+
| /post | POST | | No | Closure |
+---------+---------+-------+----------+------------+
output;

$this->assertStringContainsString(
$expected,
$this->removeColors(file_get_contents(static::$ou))
);
}
}
Loading