I am new to Laravel and I am having an issue to write a test function that asserts that the HTTP response of my welcome page is 200 knowing that I have EN and FR locales defined, meaning that the test should test both localhost:8000/en and localhost:8000/fr.
This is my exampletest.php:
<?php
namespace Tests\Feature;
use App\Http\Middleware\SetLanguage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(301);
}
public function test_getRedirected_DefaultedEnglish_WelcomePage()
{
$response = $this->get('/en');
$response->assertStatus(200);
}
public function test_getRedirected_French_WelcomePage()
{
$response = $this->get('/fr');
$response->assertStatus(200);
}
}
I don't like writing the code twice for both /en and /fr.
This is my web.php:
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
Route::redirect('/', 'en', 301);
Route::group(['prefix' => '{lang}', 'where' => ['lang' => '[a-zA-Z]{2}'], 'middleware' => 'setlanguage'], function() {
Auth::routes();
Route::get('/','App\Http\Controllers\welcomeController@index')->name('welcome');
});
And i have written my localization middleware below:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class SetLanguage
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
App::setLocale($request->lang);
return $next($request);
}
}
I would appreciate your help on the above. I believe the test should be dynamic.
Thank you in advance..