Skip to content

Commit f2070c1

Browse files
committed
Created concern to bypass domain routing with associated testing
1 parent e12a070 commit f2070c1

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pest\Laravel\Concerns;
6+
7+
use Illuminate\Routing\Matching\HostValidator;
8+
use Illuminate\Routing\Route;
9+
10+
trait BypassesDomainRouting
11+
{
12+
public function setupBypassesDomainRouting(): void
13+
{
14+
if (! isset(Route::$validators)) {
15+
Route::$validators = Route::getValidators();
16+
}
17+
18+
foreach (Route::$validators as $key => $validator) {
19+
if ($validator instanceof HostValidator) {
20+
unset(Route::$validators[$key]);
21+
}
22+
}
23+
}
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Illuminate\Http\Request;
4+
use Illuminate\Routing\Matching\HostValidator;
5+
use Illuminate\Routing\Matching\ValidatorInterface;
6+
use Illuminate\Routing\Route;
7+
use Pest\Laravel\Concerns\BypassesDomainRouting;
8+
9+
test('domain validation is removed from laravel routing', function () {
10+
// at this point we'd expect to see the HostValidator
11+
expect(collect(Route::getValidators()))
12+
->filter(fn (ValidatorInterface $validator) => $validator instanceof HostValidator)
13+
->toHaveCount(1);
14+
15+
// build a class that uses this concern
16+
(new class
17+
{
18+
use BypassesDomainRouting;
19+
})->setupBypassesDomainRouting();
20+
21+
// and check we no longer have our HostValidator
22+
expect(collect(Route::getValidators()))
23+
->toHaveCount(3)
24+
->filter(fn (ValidatorInterface $validator) => $validator instanceof HostValidator)
25+
->toHaveCount(0);
26+
});
27+
28+
test('if a custom validator has been added, it retains it', function () {
29+
30+
$anonymousClass = new class implements ValidatorInterface
31+
{
32+
public function matches(Route $route, Request $request)
33+
{
34+
return true;
35+
}
36+
};
37+
38+
// add a custom validator.
39+
Route::$validators = [...Route::getValidators(), $anonymousClass];
40+
41+
(new class
42+
{
43+
use BypassesDomainRouting;
44+
})->setupBypassesDomainRouting();
45+
46+
expect(collect(Route::getValidators()))
47+
->toHaveCount(4)
48+
->filter(fn (ValidatorInterface $validator) => $validator instanceof $anonymousClass)
49+
->toHaveCount(1);
50+
});

0 commit comments

Comments
 (0)