Skip to content

Commit

Permalink
RouteInfo ValueObject + RouteCommandSourceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
juampi92 committed Aug 20, 2022
1 parent cfc4dd9 commit 072c8b9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/Domain/Sources/RouteCommandSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
use Juampi92\Phecks\Domain\DTOs\MatchValue;
use Juampi92\Phecks\Domain\MatchCollection;
use Juampi92\Phecks\Domain\MatchString;
use Juampi92\Phecks\Domain\Sources\ValueObjects\RouteInfo;

/**
* @phpstan-type TRoute array{
* @phpstan-type TRouteArray array{
* domain: string,
* method: string,
* uri: string,
* name: string,
* action: string,
* middleware: array<string>
* }
* @implements Source<TRoute>
* @implements Source<RouteInfo>
*/
class RouteCommandSource implements Source
{
Expand All @@ -35,26 +36,33 @@ public function __construct(
}

/**
* @return MatchCollection<TRoute>
* @return MatchCollection<RouteInfo>
*/
public function run(): MatchCollection
{
$this->artisan->call('route:list', ['--json' => true]);

/** @var Collection<array-key, TRoute> $routes */
/** @var Collection<array-key, TRouteArray> $routes */
$routes = (new MatchString($this->artisan->output()))->collect();

return new MatchCollection(
$routes
->map(
/**
* @param TRoute $route
* @return MatchValue<TRoute>
* @param TRouteArray $route
* @return MatchValue<RouteInfo>
*/
function ($route): MatchValue {
return new MatchValue(
new FileMatch('Route: ' . $route['uri'] . ' (name: ' . $route['name'] . ')'),
$route,
new RouteInfo(
$route['domain'],
$route['method'],
$route['uri'],
$route['name'],
$route['action'],
$route['middleware'],
),
);
},
)
Expand Down
38 changes: 38 additions & 0 deletions src/Domain/Sources/ValueObjects/RouteInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Juampi92\Phecks\Domain\Sources\ValueObjects;

class RouteInfo
{
public ?string $domain;

public string $method;

public string $uri;

public ?string $name;

public string $action;

/** @var array<string> */
public array $middleware;

/**
* @param array<string> $middleware
*/
public function __construct(
?string $domain,
string $method,
string $uri,
?string $name,
string $action,
array $middleware
) {
$this->domain = $domain;
$this->method = $method;
$this->uri = $uri;
$this->name = $name;
$this->action = $action;
$this->middleware = $middleware;
}
}
31 changes: 30 additions & 1 deletion tests/Feature/Sources/RouteCommandSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@

namespace Juampi92\Phecks\Tests\Feature\Sources;

class RouteCommandSourceTest extends \Juampi92\Phecks\Tests\Feature\TestCase
use Illuminate\Support\Facades\Route;
use Juampi92\Phecks\Domain\Sources\RouteCommandSource;
use Juampi92\Phecks\Domain\Sources\ValueObjects\RouteInfo;
use Juampi92\Phecks\Tests\Feature\TestCase;

class RouteCommandSourceTest extends TestCase
{
public function test_should_work(): void
{
// Arrange
Route::get('/example-route-123', function (int $a) {
return $a;
})
->name('my_route');
Route::post('/example-route-no-name', function (int $a) {
return $a;
});

/** @var RouteCommandSource $source */
$source = resolve(RouteCommandSource::class);

// Act
$output = $source->run();

// Assert
$routesInfo = $output->getItems()->map->value;

$this->assertInstanceOf(RouteInfo::class, $routesInfo[0]);
$this->assertEquals('my_route', $routesInfo[0]->name);
$this->assertNull($routesInfo[1]->name);
}
}

0 comments on commit 072c8b9

Please sign in to comment.