Skip to content
Open
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
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Controller\Demo\DemoDetailController;
use App\Controller\Demo\ProcessDemoFormController;
use App\Controller\DocumentationController;
use App\Controller\FallbackController;
use App\Controller\FindRuleController;
use App\Controller\HireTeamController;
use App\Controller\HomepageController;
Expand Down Expand Up @@ -76,3 +77,5 @@
Route::get('ast/{hash}', AstDetailController::class);
Route::get('ast', AstController::class);
Route::post('process-ast', ProcessAstFormController::class);

Route::fallback(FallbackController::class);
31 changes: 31 additions & 0 deletions src/Controller/FallbackController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Controller;

use App\FileSystem\RectorFinder;
use App\RuleFilter\ValueObject\RuleMetadata;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class FallbackController extends Controller
{
public function __construct(
private readonly RectorFinder $rectorFinder
) {
}

public function __invoke(Request $request): RedirectResponse
{
$slug = str($request->path())->kebab()->toString();
$ruleMetadata = $this->rectorFinder->findBySlug($slug);

if (! $ruleMetadata instanceof RuleMetadata) {
abort(404);
}

return redirect(status: 301)->action(RuleDetailController::class, [
'slug' => $ruleMetadata->getSlug(),
]);
}
}
9 changes: 8 additions & 1 deletion src/Controller/RuleDetailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ public function __construct(

public function __invoke(string $slug): View|RedirectResponse
{
$ruleMetadata = $this->rectorFinder->findBySlug($slug);
$kebab = str($slug)->kebab()->toString();
$ruleMetadata = $this->rectorFinder->findBySlug($kebab);

if (! $ruleMetadata instanceof RuleMetadata) {
// nothing found, get back
return redirect()->action(FindRuleController::class);
}

if ($kebab !== $slug) {
return redirect(status: 301)->action(RuleDetailController::class, [
'slug' => $kebab,
]);
}

return \view('homepage/rule_detail', [
'page_title' => $ruleMetadata->getRuleShortClass(),
'ruleMetadata' => $ruleMetadata,
Expand Down
6 changes: 6 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

abstract class AbstractTestCase extends TestCase
{
protected function setup(): void
{
parent::setup();
$this->withoutVite();
}

#[Override]
public function createApplication(): Application
{
Expand Down
30 changes: 30 additions & 0 deletions tests/Controller/FallbackControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Tests\Controller;

use App\Tests\AbstractTestCase;
use Illuminate\Foundation\Http\Middleware\PreventRequestForgery;

class FallbackControllerTest extends AbstractTestCase
{
public function testRedirectsRuleSlug(): void
{
$response = $this->get('/simplify-array-search-rector');

$response->assertRedirect('/rule-detail/simplify-array-search-rector');
}

public function testReturns404ForUnknownSlug(): void
{
$response = $this->get('/unknown-rule');

$response->assertNotFound();
}

public function testRedirectsWithPascalCaseSlug(): void
{
$response = $this->get('/SimplifyArraySearchRector');

$response->assertRedirect('/rule-detail/simplify-array-search-rector');
}
}
38 changes: 38 additions & 0 deletions tests/Controller/RuleDetailControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Tests\Controller;

use App\Tests\AbstractTestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class RuleDetailControllerTest extends AbstractTestCase
{
public function testItRendersRulesBySlug(): void
{
$response = $this->get('/rule-detail/simplify-array-search-rector');

$response
->assertOk()
->assertViewIs('homepage.rule_detail')
->assertViewHas('ruleMetadata')
->assertViewHas('codeMirror', true)
->assertViewHas('page_title', 'SimplifyArraySearchRector')
->assertSeeText('SimplifyArraySearchRector');
}

public function testItRedirectsToFindRuleIfRuleNotFound(): void
{
$response = $this->get('/rule-detail/unknown-rule');

$response->assertRedirect('/find-rule');
}

public function testItRedirectsToKebabCaseSlugIfPascalCaseSlug(): void
{
$response = $this->get('/rule-detail/SimplifyArraySearchRector');

$response->assertRedirect('/rule-detail/simplify-array-search-rector');
}
}
Loading