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

declare(strict_types=1);

namespace Pest\Laravel\Concerns;

use Illuminate\Routing\Matching\HostValidator;
use Illuminate\Routing\Route;

trait BypassesDomainRouting
{
public function setupBypassesDomainRouting(): void
{
if (! isset(Route::$validators)) {
Route::$validators = Route::getValidators();
}

foreach (Route::$validators as $key => $validator) {
if ($validator instanceof HostValidator) {
unset(Route::$validators[$key]);
}
}
}
}
50 changes: 50 additions & 0 deletions tests/Concerns/BypassesDomainRouting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Routing\Matching\HostValidator;
use Illuminate\Routing\Matching\ValidatorInterface;
use Illuminate\Routing\Route;
use Pest\Laravel\Concerns\BypassesDomainRouting;

test('domain validation is removed from laravel routing', function () {
// at this point we'd expect to see the HostValidator
expect(collect(Route::getValidators()))
->filter(fn (ValidatorInterface $validator) => $validator instanceof HostValidator)
->toHaveCount(1);

// build a class that uses this concern
(new class
{
use BypassesDomainRouting;
})->setupBypassesDomainRouting();

// and check we no longer have our HostValidator
expect(collect(Route::getValidators()))
->toHaveCount(3)
->filter(fn (ValidatorInterface $validator) => $validator instanceof HostValidator)
->toHaveCount(0);
});

test('if a custom validator has been added, it retains it', function () {

$anonymousClass = new class implements ValidatorInterface
{
public function matches(Route $route, Request $request)
{
return true;
}
};

// add a custom validator.
Route::$validators = [...Route::getValidators(), $anonymousClass];

(new class
{
use BypassesDomainRouting;
})->setupBypassesDomainRouting();

expect(collect(Route::getValidators()))
->toHaveCount(4)
->filter(fn (ValidatorInterface $validator) => $validator instanceof $anonymousClass)
->toHaveCount(1);
});