jeudi 4 février 2021

I'm trying to create a HTTP request test with Laravel. But I'm having trouble with cookies

I'm trying to create a simple test that sends a request to my admin's project route. But I'm having trouble with the cookies. I'm following the Laravel's docs, from version 6. I've tried different approaches, none of them were successful.

That's the code:

public function se_usuario_autenticado_consegue_acessar_admin() : void
    {
        $user = Usuario::find(54158);

        $response = $this
            ->withSession([
                'tipo' => [
                    '****', '****', '****', '****'
                ],
                'poder' => ****,
                'acesso' =>  [
                    '****', '****', '****'
                ],
                'email' => '*****'
            ])            
            ->withCookie(cookie('****', '****'))
            // ->withoutExceptionHandling()
            // ->withoutMiddleware()
            ->actingAs($user)
            ->get('/admin');
        
        $response->dumpHeaders();

        $response->assertStatus(200);
    }

And the ErroException that I'm getting is

ErrorException: Trying to get property 'cookies' of non-object /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Session/CookieSessionHandler.php:69 /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Session/Store.php:97 /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Session/Store.php:87 /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Session/Store.php:71 /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Support/Manager.php:165 /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:45 /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:28 /home/baldo/Documents/CEISC/CEISC/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:15 /home/baldo/Documents/CEISC/CEISC/tests/Feature/HomeTest.php:56

CookieSessionHandler.php:69

public function read($sessionId)
    {
        $value = $this->request->cookies->get($sessionId) ?: ''; // This is the line 69

        if (! is_null($decoded = json_decode($value, true)) && is_array($decoded)) {
            if (isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) {
                return $decoded['data'];
            }
        }

        return '';
    }

Also, as It seems, the withCookies() or withCookie() methods are not working even though they are mentioned in the documentation.

Aucun commentaire:

Enregistrer un commentaire