From 5e80d0695dc010e412f21462d2aadc59f7241358 Mon Sep 17 00:00:00 2001 From: Rustam Date: Mon, 9 Oct 2023 18:29:57 +0500 Subject: [PATCH] Rename Resource to Provider, add more tests --- .../ArrayRoutesProvider.php} | 4 +- .../AttributeRoutesProvider.php} | 7 +-- .../FileRoutesProvider.php} | 10 ++-- .../RoutesProviderInterface.php} | 6 +-- src/RouteCollector.php | 14 +++--- src/RouteCollectorInterface.php | 6 +-- .../ArrayResourceTest.php | 6 +-- tests/Provider/FileResourceTest.php | 46 +++++++++++++++++++ tests/Resource/FileResourceTest.php | 27 ----------- tests/RouteCollectorTest.php | 33 +++++++++++++ tests/Support/{ => resources}/routes.php | 0 tests/Support/resources/test.php | 8 ++++ 12 files changed, 115 insertions(+), 52 deletions(-) rename src/{Resource/ArrayResource.php => Provider/ArrayRoutesProvider.php} (74%) rename src/{Resource/AttributeResource.php => Provider/AttributeRoutesProvider.php} (87%) rename src/{Resource/FileResource.php => Provider/FileRoutesProvider.php} (86%) rename src/{Resource/ResourceInterface.php => Provider/RoutesProviderInterface.php} (61%) rename tests/{Resource => Provider}/ArrayResourceTest.php (73%) create mode 100644 tests/Provider/FileResourceTest.php delete mode 100644 tests/Resource/FileResourceTest.php rename tests/Support/{ => resources}/routes.php (100%) create mode 100644 tests/Support/resources/test.php diff --git a/src/Resource/ArrayResource.php b/src/Provider/ArrayRoutesProvider.php similarity index 74% rename from src/Resource/ArrayResource.php rename to src/Provider/ArrayRoutesProvider.php index 12b4d21..83abd22 100644 --- a/src/Resource/ArrayResource.php +++ b/src/Provider/ArrayRoutesProvider.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace Yiisoft\Router\Resource; +namespace Yiisoft\Router\Provider; use Yiisoft\Router\Route; use Yiisoft\Router\Group; -final class ArrayResource implements ResourceInterface +final class ArrayRoutesProvider implements RoutesProviderInterface { /** * @param Group[]|Route[] $routes diff --git a/src/Resource/AttributeResource.php b/src/Provider/AttributeRoutesProvider.php similarity index 87% rename from src/Resource/AttributeResource.php rename to src/Provider/AttributeRoutesProvider.php index 2cd9cc4..373227b 100644 --- a/src/Resource/AttributeResource.php +++ b/src/Provider/AttributeRoutesProvider.php @@ -2,17 +2,18 @@ declare(strict_types=1); -namespace Yiisoft\Router\Resource; +namespace Yiisoft\Router\Provider; use olvlvl\ComposerAttributeCollector\Attributes; use Yiisoft\Router\Group; use Yiisoft\Router\Route; /** - * An attribute resource represents routes that declared via PHP Attributes. + * An attribute provider provides routes that declared via PHP Attributes. + * Currently, uses `olvlvl/composer-attribute-collector`. {@link https://github.com/olvlvl/composer-attribute-collector}. * @codeCoverageIgnore */ -final class AttributeResource implements ResourceInterface +final class AttributeRoutesProvider implements RoutesProviderInterface { /** * @var array diff --git a/src/Resource/FileResource.php b/src/Provider/FileRoutesProvider.php similarity index 86% rename from src/Resource/FileResource.php rename to src/Provider/FileRoutesProvider.php index 6c3d805..ee130ee 100644 --- a/src/Resource/FileResource.php +++ b/src/Provider/FileRoutesProvider.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Yiisoft\Router\Resource; +namespace Yiisoft\Router\Provider; use Yiisoft\Router\Group; use Yiisoft\Router\Route; /** - * A file resource represents routes from a file or directory of files. + * A file provider provides routes from a file or directory of files. */ -final class FileResource implements ResourceInterface +final class FileRoutesProvider implements RoutesProviderInterface { public function __construct(private string $file, private array $scope = []) { @@ -26,7 +26,9 @@ public function getRoutes(): array return require $file; }; if (!file_exists($this->file)) { - throw new \RuntimeException(); + throw new \RuntimeException( + 'Failed to provide routes from "' . $this->file . '". File or directory not found.' + ); } if (is_dir($this->file) && !is_file($this->file)) { $directoryRoutes = []; diff --git a/src/Resource/ResourceInterface.php b/src/Provider/RoutesProviderInterface.php similarity index 61% rename from src/Resource/ResourceInterface.php rename to src/Provider/RoutesProviderInterface.php index 727f3cf..60dc2f3 100644 --- a/src/Resource/ResourceInterface.php +++ b/src/Provider/RoutesProviderInterface.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Yiisoft\Router\Resource; +namespace Yiisoft\Router\Provider; use Yiisoft\Router\Group; use Yiisoft\Router\Route; /** - * `ResourceInterface` is a resource of routes. + * `RoutesProviderInterface` provides routes. */ -interface ResourceInterface +interface RoutesProviderInterface { /** * @return Group[]|Route[] diff --git a/src/RouteCollector.php b/src/RouteCollector.php index 5fb27c5..c387f28 100644 --- a/src/RouteCollector.php +++ b/src/RouteCollector.php @@ -4,7 +4,7 @@ namespace Yiisoft\Router; -use Yiisoft\Router\Resource\ResourceInterface; +use Yiisoft\Router\Provider\RoutesProviderInterface; final class RouteCollector implements RouteCollectorInterface { @@ -14,9 +14,9 @@ final class RouteCollector implements RouteCollectorInterface private array $items = []; /** - * @var ResourceInterface[] + * @var RoutesProviderInterface[] */ - private array $resources = []; + private array $providers = []; /** * @var array[]|callable[]|string[] @@ -32,9 +32,9 @@ public function addRoute(Route|Group ...$routes): RouteCollectorInterface return $this; } - public function addResource(ResourceInterface $resource): RouteCollectorInterface + public function addProvider(RoutesProviderInterface $provider): RouteCollectorInterface { - $this->resources[] = $resource; + $this->providers[] = $provider; return $this; } @@ -58,10 +58,10 @@ public function prependMiddleware(array|callable|string ...$middlewareDefinition public function getItems(): array { - foreach ($this->resources as $resource) { + foreach ($this->providers as $provider) { array_push( $this->items, - ...$resource->getRoutes() + ...$provider->getRoutes() ); } return $this->items; diff --git a/src/RouteCollectorInterface.php b/src/RouteCollectorInterface.php index 12b25c6..e9192b6 100644 --- a/src/RouteCollectorInterface.php +++ b/src/RouteCollectorInterface.php @@ -4,7 +4,7 @@ namespace Yiisoft\Router; -use Yiisoft\Router\Resource\ResourceInterface; +use Yiisoft\Router\Provider\RoutesProviderInterface; interface RouteCollectorInterface { @@ -14,9 +14,9 @@ interface RouteCollectorInterface public function addRoute(Route|Group ...$routes): self; /** - * Add a resource of routes + * Add a provider of routes */ - public function addResource(ResourceInterface $resource): self; + public function addProvider(RoutesProviderInterface $provider): self; /** * Appends a handler middleware definition that should be invoked for a matched route. diff --git a/tests/Resource/ArrayResourceTest.php b/tests/Provider/ArrayResourceTest.php similarity index 73% rename from tests/Resource/ArrayResourceTest.php rename to tests/Provider/ArrayResourceTest.php index 2dda35c..e2e4cdb 100644 --- a/tests/Resource/ArrayResourceTest.php +++ b/tests/Provider/ArrayResourceTest.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Yiisoft\Router\Tests\Resource; +namespace Yiisoft\Router\Tests\Provider; use PHPUnit\Framework\TestCase; use Yiisoft\Router\Group; -use Yiisoft\Router\Resource\ArrayResource; +use Yiisoft\Router\Provider\ArrayRoutesProvider; use Yiisoft\Router\Route; class ArrayResourceTest extends TestCase @@ -18,7 +18,7 @@ public function testGetRoutes(): void Group::create('')->routes(Route::get('/blog')), ]; - $resource = new ArrayResource($routes); + $resource = new ArrayRoutesProvider($routes); $this->assertSame($routes, $resource->getRoutes()); } diff --git a/tests/Provider/FileResourceTest.php b/tests/Provider/FileResourceTest.php new file mode 100644 index 0000000..054273c --- /dev/null +++ b/tests/Provider/FileResourceTest.php @@ -0,0 +1,46 @@ +routes = require $this->file; + } + + public function testGetRoutes(): void + { + $provider = new FileRoutesProvider($this->file); + + $this->assertEquals($this->routes, $provider->getRoutes()); + } + + public function testGetRoutesWithNotExistFile(): void + { + $file = __DIR__ . '/foo.php'; + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Failed to provide routes from "' . $file . '". File or directory not found.'); + + $provider = new FileRoutesProvider($file); + $provider->getRoutes(); + } + + public function testGetRoutesInDirectory(): void + { + $provider = new FileRoutesProvider(dirname($this->file)); + + $this->assertEquals($this->routes, $provider->getRoutes()); + } +} diff --git a/tests/Resource/FileResourceTest.php b/tests/Resource/FileResourceTest.php deleted file mode 100644 index 9dbce8f..0000000 --- a/tests/Resource/FileResourceTest.php +++ /dev/null @@ -1,27 +0,0 @@ -routes = require $this->file; - } - - public function testGetRoutes(): void - { - $resource = new FileResource($this->file); - - $this->assertEquals($this->routes, $resource->getRoutes()); - } -} diff --git a/tests/RouteCollectorTest.php b/tests/RouteCollectorTest.php index 5fa1ea1..ffbf7ac 100644 --- a/tests/RouteCollectorTest.php +++ b/tests/RouteCollectorTest.php @@ -7,6 +7,7 @@ use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use Yiisoft\Router\Group; +use Yiisoft\Router\Provider\ArrayRoutesProvider; use Yiisoft\Router\Route; use Yiisoft\Router\RouteCollector; @@ -59,6 +60,38 @@ public function testAddGroup(): void $this->assertContainsOnlyInstancesOf(Group::class, $collector->getItems()); } + public function testAddProvider(): void + { + $logoutRoute = Route::post('/logout'); + $listRoute = Route::get('/'); + $viewRoute = Route::get('/{id}'); + $postGroup = Group::create('/post') + ->routes( + $listRoute, + $viewRoute + ); + + $rootGroup = Group::create() + ->routes( + Group::create('/api') + ->routes( + $logoutRoute, + $postGroup + ), + ); + + $testGroup = Group::create() + ->routes( + Route::get('test/') + ); + + $collector = new RouteCollector(); + $collector->addProvider(new ArrayRoutesProvider([$rootGroup, $postGroup, $testGroup])); + + $this->assertCount(3, $collector->getItems()); + $this->assertContainsOnlyInstancesOf(Group::class, $collector->getItems()); + } + public function testAddMiddleware(): void { $collector = new RouteCollector(); diff --git a/tests/Support/routes.php b/tests/Support/resources/routes.php similarity index 100% rename from tests/Support/routes.php rename to tests/Support/resources/routes.php diff --git a/tests/Support/resources/test.php b/tests/Support/resources/test.php new file mode 100644 index 0000000..33a8a8b --- /dev/null +++ b/tests/Support/resources/test.php @@ -0,0 +1,8 @@ +