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

Add RouteNotDefined Solution Provider #113

Merged
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
53 changes: 53 additions & 0 deletions src/SolutionProviders/RouteNotDefinedSolutionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Facade\Ignition\SolutionProviders;

use Throwable;
use InvalidArgumentException;
use Illuminate\Support\Facades\Route;
use Facade\IgnitionContracts\BaseSolution;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Support\StringComparator;
use Facade\IgnitionContracts\HasSolutionsForThrowable;

class RouteNotDefinedSolutionProvider implements HasSolutionsForThrowable
{
protected const REGEX = '/Route \[(.*)\] not defined/m';

public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof InvalidArgumentException && ! $throwable instanceof ViewException) {
return false;
}

return preg_match(self::REGEX, $throwable->getMessage(), $matches);
}

public function getSolutions(Throwable $throwable): array
{
preg_match(self::REGEX, $throwable->getMessage(), $matches);

$missingRoute = $matches[1] ?? null;

$suggestedRoute = $this->findRelatedRoute($missingRoute);

if ($suggestedRoute) {
return [
BaseSolution::create("{$missingRoute} was not defined.")
->setSolutionDescription("Did you mean `{$suggestedRoute}`?"),
];
}

return [
BaseSolution::create("{$missingRoute} was not defined.")
->setSolutionDescription('Are you sure that the route is defined'),
];
}

protected function findRelatedRoute(string $missingRoute): ?string
{
Route::getRoutes()->refreshNameLookups();

return StringComparator::findClosestMatch(array_keys(Route::getRoutes()->getRoutesByName()), $missingRoute);
}
}
47 changes: 47 additions & 0 deletions tests/Solutions/RouteNotDefinedSolutionProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Facade\Ignition\Tests\Solutions;

use Illuminate\Support\Str;
use InvalidArgumentException;
use Facade\Ignition\Tests\TestCase;
use Illuminate\Support\Facades\Route;
use Facade\Ignition\SolutionProviders\RouteNotDefinedSolutionProvider;

class RouteNotDefinedSolutionProviderTest extends TestCase
{
/** @test */
public function it_can_solve_the_exception()
{
$canSolve = app(RouteNotDefinedSolutionProvider::class)->canSolve($this->getRouteNotDefinedException());

$this->assertTrue($canSolve);
}

/** @test */
public function it_can_recommend_changing_the_route_name()
{
Route::get('/test', 'TestController@typo')->name('test.typo');

/** @var \Facade\IgnitionContracts\Solution $solution */
$solution = app(RouteNotDefinedSolutionProvider::class)->getSolutions($this->getRouteNotDefinedException())[0];

$this->assertTrue(Str::contains($solution->getSolutionDescription(), 'Did you mean `test.typo`?'));
}

/** @test */
public function it_wont_recommend_another_route_if_the_names_are_too_different()
{
Route::get('/test', 'TestController@typo')->name('test.typo');

/** @var \Facade\IgnitionContracts\Solution $solution */
$solution = app(RouteNotDefinedSolutionProvider::class)->getSolutions($this->getRouteNotDefinedException('test.is-too-different'))[0];

$this->assertFalse(Str::contains($solution->getSolutionDescription(), 'Did you mean'));
}

protected function getRouteNotDefinedException(string $route = 'test.typoo'): InvalidArgumentException
{
return new InvalidArgumentException("Route [{$route}] not defined.");
}
}