Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring + Tests + MSI 100% #22

Merged
merged 12 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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: 2 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
},
"mutators": {
"@default": true
}
},
"minMsi": 100
}
64 changes: 30 additions & 34 deletions src/FileRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,61 +155,57 @@ private function parseRequestPath(ServerRequestInterface $request): Generator
}
}

$possibleAction = null;

if ($path === '/') {
yield [
$this->cleanClassname(
$this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $this->defaultControllerName . $this->classPostfix
),
$possibleAction,
$this->makeClassName($this->defaultControllerName),
null,
];
return;
}

$controllerName = preg_replace_callback(
'#(/.)#u',
static fn(array $matches) => mb_strtoupper($matches[1]),
'#/.#u',
static fn(array $matches) => mb_strtoupper($matches[0]),
$path,
);

if (!preg_match('#^(.*?)/([^/]+)/?$#', $controllerName, $matches)) {
if (!preg_match('#^/?(.*?)/([^/]+)/?$#', $controllerName, $matches)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That enables / prefixing controller name. Use-case for that is interesting.

Copy link
Member Author

@vjik vjik Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here $controllerName contains path with uppercased first symbols. Renamed it to $pathAsNamespace.

This regexp should be valid for paths with one and more items (/user, /user/profile, /user/profile/view).

return;
}

$directoryPath = $matches[1];
$controllerName = $matches[2];
[$_, $directoryPath, $controllerName] = $matches;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave it as it was before

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced to [, $directoryPath, $controllerName] = $matches;


yield [
$this->cleanClassname(
$this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $directoryPath . '\\' . $controllerName . $this->classPostfix
),
$possibleAction,
$this->makeClassName($controllerName, $directoryPath),
null,
];

if (preg_match('#^(.*?)/([^/]+)/?$#', $directoryPath, $matches)) {
$possibleAction = strtolower($controllerName);
$directoryPath = $matches[1];
$controllerName = $matches[2];
if ($directoryPath === '') {
yield [
$this->makeClassName($this->defaultControllerName, $controllerName),
null,
];
} else {
$directoryPath = $controllerName;
$controllerName = $this->defaultControllerName;
yield [
$this->makeClassName($directoryPath),
strtolower($controllerName),
];
}

yield [
$this->cleanClassname(
$this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $directoryPath . '\\' . $controllerName . $this->classPostfix
),
$possibleAction,
];
}

private function cleanClassname(string $className): string
private function makeClassName(string $controllerName, string $directoryPath = ''): string
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
return str_replace(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I remember there were some Windows cases where such operations are necessary to make the router work correctly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

['\\/\\', '\\/', '\\\\'],
'\\',
$className,
);
$parts = [];
if ($this->namespace !== '') {
$parts[] = $this->namespace;
}
if ($this->baseControllerDirectory !== '') {
$parts[] = $this->baseControllerDirectory;
}
if ($directoryPath !== '') {
$parts[] = str_replace('/', '\\', $directoryPath);
}
$parts[] = str_replace('/', '\\', $controllerName) . $this->classPostfix;
return implode('\\', $parts);
}
}
63 changes: 59 additions & 4 deletions tests/FileRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Yiisoft\FileRouter\Tests\Support\App3;
use Yiisoft\FileRouter\Tests\Support\App4;
use Yiisoft\FileRouter\Tests\Support\App5;
use Yiisoft\FileRouter\Tests\Support\App6;
use Yiisoft\FileRouter\Tests\Support\HeaderMiddleware;
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
Expand Down Expand Up @@ -103,6 +104,26 @@ public static function dataRouter(): iterable
'/user',
'Hello, delete!',
];
yield 'HEAD /user' => [
'HEAD',
'/user',
'Hello, head!',
];
yield 'OPTIONS /user' => [
'OPTIONS',
'/user',
'Hello, options!',
];
yield 'GET /user/profile/view' => [
'GET',
'/user/profile/view',
'Hello, User\Profile\IndexController!',
];
yield 'GET /user/blog/view' => [
'GET',
'/user/blog/view',
'Hello, blog view!',
];
}

public function testUnsupportedMethod(): void
Expand Down Expand Up @@ -272,19 +293,41 @@ public function testRoutesCollision(string $method, string $uri, string $expecte

public static function dataRoutesCollision(): iterable
{
yield 'direct' => [
yield 'direct get' => [
'GET',
'/user',
'Hello, Controller/UserController!',
'Hello, index Controller/UserController!',
];
yield 'direct delete' => [
'DELETE',
'/user',
'Hello, delete Controller/UserController!',
];

yield 'indirect' => [
yield 'indirect post' => [
'POST',
'/user',
'Hello, Controller/User/IndexController!',
'Hello, create Controller/User/IndexController!',
];
}

public function testActions(): void
{
$router = $this->createRouter();
$router = $router->withNamespace('Yiisoft\FileRouter\Tests\Support\App6');

$handler = $this->createExceptionHandler();
$request = new ServerRequest(
method: 'POST',
uri: '/user',
);

$response = $router->process($request, $handler);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Hello, create Controller/User/IndexController!', (string) $response->getBody());
}

#[DataProvider('dataUnicodeRoutes')]
public function testTestUnicodeRoutes(string $method, string $uri, string $expectedResponse): void
{
Expand Down Expand Up @@ -370,6 +413,13 @@ public static function dataModularity(): iterable
'/m/o/d/u/l/e/index',
'Hello, module2!',
];
yield 'модуль3 /index' => [
'Yiisoft\\FileRouter\\Tests\\Support\\App5\\Модуль3',
'/Модуль3',
'GET',
'/Модуль3/index',
'Hello, модуль3!',
];
}

public function testModularityFastPath(): void
Expand Down Expand Up @@ -407,6 +457,7 @@ private function createRouter(): FileRouter
$container = new SimpleContainer([
HeaderMiddleware::class => new HeaderMiddleware(),

App1\Controller\User\Profile\ViewController::class => new App1\Controller\User\Profile\ViewController(),
App1\Controller\User\BlogController::class => new App1\Controller\User\BlogController(),
App1\Controller\UserController::class => new App1\Controller\UserController(),
App1\Controller\IndexController::class => new App1\Controller\IndexController(),
Expand All @@ -421,6 +472,10 @@ private function createRouter(): FileRouter

App5\Module1\Controller\IndexController::class => new App5\Module1\Controller\IndexController(),
App5\Module2\Controller\IndexController::class => new App5\Module2\Controller\IndexController(),
App5\Модуль3\Controller\Index\IndexController::class => new App5\Модуль3\Controller\Index\IndexController(),

App6\Controller\UserController::class => new App6\Controller\UserController(),
App6\Controller\User\IndexController::class => new App6\Controller\User\IndexController(),
]);

return new FileRouter(
Expand Down
5 changes: 5 additions & 0 deletions tests/Support/App1/Controller/User/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function index(): ResponseInterface
{
return new TextResponse('Hello, index!');
}

public function view(): ResponseInterface
{
return new TextResponse('Hello, blog view!');
}
}
16 changes: 16 additions & 0 deletions tests/Support/App1/Controller/User/Profile/ViewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FileRouter\Tests\Support\App1\Controller\User\Profile;

use HttpSoft\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;

final class ViewController
{
public function index(): ResponseInterface
{
return new TextResponse('Hello, User\Profile\IndexController!', 200);
}
}
10 changes: 10 additions & 0 deletions tests/Support/App1/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public function index(): ResponseInterface
return new TextResponse('Hello, index!');
}

public function head(): ResponseInterface
{
return new TextResponse('Hello, head!');
}

public function options(): ResponseInterface
{
return new TextResponse('Hello, options!');
}

public function create(): ResponseInterface
{
return new TextResponse('Hello, create!');
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/App3/Controller/User/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class IndexController
{
public function index(): ResponseInterface
{
return new TextResponse('Hello, Controller/User/IndexController!');
return new TextResponse('Hello, index Controller/User/IndexController!');
}

public function create(): ResponseInterface
{
return new TextResponse('Hello, Controller/User/IndexController!');
return new TextResponse('Hello, create Controller/User/IndexController!');
}
}
7 changes: 6 additions & 1 deletion tests/Support/App3/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class UserController
{
public function index(): ResponseInterface
{
return new TextResponse('Hello, Controller/UserController!');
return new TextResponse('Hello, index Controller/UserController!');
}

public function delete(): ResponseInterface
{
return new TextResponse('Hello, delete Controller/UserController!');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FileRouter\Tests\Support\App5\Модуль3\Controller\Index;

use HttpSoft\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;

class IndexController
{
public function index(): ResponseInterface
{
return new TextResponse('Hello, модуль3!', 200);
}
}
16 changes: 16 additions & 0 deletions tests/Support/App6/Controller/User/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FileRouter\Tests\Support\App6\Controller\User;

use HttpSoft\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;

class IndexController
{
public function create(): ResponseInterface
{
return new TextResponse('Hello, create Controller/User/IndexController!');
}
}
25 changes: 25 additions & 0 deletions tests/Support/App6/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FileRouter\Tests\Support\App6\Controller;

use HttpSoft\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;

class UserController
{
public static array $actions = [
'GET' => 'index',
];

public function index(): ResponseInterface
{
return new TextResponse('Hello, index Controller/UserController!');
}

public function create(): ResponseInterface
{
return new TextResponse('Hello, create Controller/UserController!');
}
}