|
| 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