Last active
March 30, 2022 13:28
-
-
Save DominikStyp/3adf241be127efb4bcd2a4d608e4ce3b to your computer and use it in GitHub Desktop.
Laravel: Test if request URI matches the route name (Unit Test)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature\Controllers; | |
use Illuminate\Routing\Router; | |
use Illuminate\Support\Facades\Crypt; | |
use Illuminate\Support\Facades\Request; | |
use Faker\Factory; | |
class SomeTestClass { | |
/** | |
* @test | |
*/ | |
public function test_if_ids_match_the_route_pattern() | |
{ | |
$faker = Factory::create(); | |
/** @var Router $router */ | |
$router = app('router'); | |
for($i = 1; $i <= 100; $i++) { | |
$str = $faker->randomNumber(9, false); | |
$encryptedId = Crypt::encryptString($str); | |
$uri = "/encryptedId/$encryptedId"; | |
/** @var \Illuminate\Http\Request $request */ | |
$request = Request::create($uri, 'GET'); | |
$route = $router->getRoutes()->match($request); | |
$this->assertEquals('encrypted.show', $route->getName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment