nekooee's avatar

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

0 likes
5 replies
MichalOravec's avatar
public function handle(Request $request, Closure $next)
{
    $locale = $request->segment(1);

    $locales = []; // add this row
 
    // other code
}
1 like
nekooee's avatar

They are no different because it does not read languages from the database. Now instead of 500 error, it gives a 404 error. Because the $locales are empty! It has to get languages from the database, but it can't.

Tray2's avatar

Goin from a 500 to a 404 is a change.

You need to have a /en route in your routes files and the locale en in your database.

1 like
nekooee's avatar

You did not notice the problem. Language information is available in the database. And the site opens properly in the browser. But in the test it can not get language information from the database. Therefore, the $locales are not defined and give an error of 500. If I define the $locales as empty before reading the information from the database, it returns the same empty $locales. Because it could not get data from the database in the loop and add it in the $locales. When $locales is an empty array, error 404 returns because en is not in the array, so it is redirected according to the code to page 404. The main problem is just why there is data in the database but in the test it can not take the languages and add it to the $locales.

nekooee's avatar

I realized that to be able to use the main database and existing data, I have to comment on the following.

 <!--  <server name="DB_CONNECTION" value="sqlite"/>
         <server name="DB_DATABASE" value=":memory:"/>-->

Because when these two lines are not comments, another database is used instead of the main database.

Please or to participate in this conversation.