public function handle(Request $request, Closure $next)
{
$locale = $request->segment(1);
$locales = []; // add this row
// other code
}
Jul 9, 2021
5
Level 1
when run test the middleware can't access database data
when run test the middleware can't access database data.
my middleware (Localization.php) is:
public function handle(Request $request, Closure $next)
{
$locale = $request->segment(1);
$languages = Language::enabledLanguages()->get();
foreach ($languages->toArray() as $key => $language) {
$locales[$key] = $language['locale'];
}
if (!in_array($locale, $locales)) {
abort(404);
}
App::setLocale($locale);
return $next($request);
}
and my test is:
class ThreadsTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function a_user_can_browse_home()
{
$response = $this->get('/en');
$response->assertStatus(200);
}
}
and the result of the test is :
Expected status code 200 but received 500.
Failed asserting that 200 is identical to 500.
I check my log. and I found middleware can't read languages from the database. You can see the log below:
[2021-07-09 20:52:21] testing.ERROR: Undefined variable $locales {"exception":"[object] (ErrorException(code: 0): Undefined variable $locales at C:\project\MyApp\app\Http\Middleware\Localization.php:45)
in the browser everything is good and the home page opens successfully. please help m. thank you
Please or to participate in this conversation.