Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
Last active March 30, 2022 13:28
Show Gist options
  • Save DominikStyp/3adf241be127efb4bcd2a4d608e4ce3b to your computer and use it in GitHub Desktop.
Save DominikStyp/3adf241be127efb4bcd2a4d608e4ce3b to your computer and use it in GitHub Desktop.
Laravel: Test if request URI matches the route name (Unit Test)
<?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