Skip to content

Commit

Permalink
Rename Resource to Provider, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Oct 9, 2023
1 parent 6d377a2 commit 5e80d06
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<class-string, \ReflectionMethod>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
{
Expand All @@ -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 = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
14 changes: 7 additions & 7 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Router;

use Yiisoft\Router\Resource\ResourceInterface;
use Yiisoft\Router\Provider\RoutesProviderInterface;

final class RouteCollector implements RouteCollectorInterface
{
Expand All @@ -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[]
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/RouteCollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Router;

use Yiisoft\Router\Resource\ResourceInterface;
use Yiisoft\Router\Provider\RoutesProviderInterface;

interface RouteCollectorInterface
{
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Provider/FileResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Tests\Provider;

use PHPUnit\Framework\TestCase;
use Yiisoft\Router\Provider\FileRoutesProvider;

use const _PHPStan_4dd92cd93\__;

class FileResourceTest extends TestCase
{
private array $routes = [];
private string $file = __DIR__ . '/../Support/resources/routes.php';

protected function setUp(): void
{
parent::setUp();
$this->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());
}
}
27 changes: 0 additions & 27 deletions tests/Resource/FileResourceTest.php

This file was deleted.

33 changes: 33 additions & 0 deletions tests/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions tests/Support/resources/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Yiisoft\Router\Group;
use Yiisoft\Router\Route;

return 'test';

0 comments on commit 5e80d06

Please sign in to comment.