paboo's avatar
Level 1

can i send request with cookie in laravel unit test. This is my way but still working



public function testRegisterUserSuccess()
    {
        $this->faker = Factory::create();
        $data = [
            User::FAMILY_NAME =>  $this->faker->randomElement(['ド','ダオ']),
            User::GIVEN_NAME => $this->faker->randomElement(['アイン', 'ウイアン']),
            User::FAMILY_NAME_KANA => $this->faker->randomElement(['ド','ダオ']),
            User::GIVEN_NAME_KANA =>$this->faker->randomElement(['アイン', 'ウイアン']),
            User::WORK_PREFECTURE_ID => 1,
            User::WORK_PREFECTURE_PUBLISH_TYPE => 'PRIVATE',
            User::SOCIETY_TYPE => 'student',
            User::SOCIETY_PUBLISH_TYPE => 'private',
            User::LANGUAGE => 'jp',
            User::NICK_NAME => 'uyen',
            User::IS_MAIL_RECEIVABLED => 1,
            User::WORK_ORGANIZATION_NAME => 'TakajouRei',
            User::WORK_CORPORATE_NUMBER => 1000,

        ];
        $record = [
            'family_name'=>'anh',
            'given_name_kana'=>'ダオ',
            'family_name_kana'=>'ダオ',
            'profile_publish_type'=>'private',
            'work_prefecture_id'=>1000,
            'work_prefecture_publish_type'=> 'private',
            'work_organization_name'=> 'asdasda',
            'given_name'=>'sada',
            'society_type'=>'student',
            'society_publish_type'=> 'asda',
            'language'=> 'VI',
            'nick_name'=>'asd',
            'is_mail_receivabled'=>1,
            'work_corporate_number'=> 1000

        ];
//        dd($data);
        $cookie = Cookie('api-token','e3ef09fa9434e65837193bd77ca7b281c6847f8b8554fe48d98694e3c7e40dc8');
        $response = $this->call('POST',route('user.register'),$record,$cookie);
//        $response = $this->postJson(route('user.register'),$record)->withCookie('api-token','e3ef09fa9434e65837193bd77ca7b281c6847f8b8554fe48d98694e3c7e40dc8');
        $response->assertStatus(200);
        $response->assertJsonStructure([
            'api_version',
            'data'=>['user_id'],

        ]);
    }

thanks for help guys

and this is log error from CMD


 vendor/bin/phpunit --filter=testRegisterUserSuccess
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 129 ms, Memory: 18.00 MB

There was 1 error:

1) Tests\Feature\Apis\ApiRegisterUserTest::testRegisterUserSuccess
TypeError: Argument 4 passed to Symfony\Component\HttpFoundation\Request::createRequestFromFactory() must be of the type array, object given, called in /home/ket/Desktop/gcode/vendor/symfony/http-foundation/Request.php on line 410

/home/ket/Desktop/gcode/vendor/symfony/http-foundation/Request.php:1976
/home/ket/Desktop/gcode/vendor/symfony/http-foundation/Request.php:410
/home/ket/Desktop/gcode/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:464
/home/ket/Desktop/gcode/tests/Feature/Apis/ApiRegisterUserTest.php:64

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.


0 likes
8 replies
gitwithravish's avatar

Do it like this.

$this->disableCookieEncryption();

$cookies = ['api-token' => 'e3ef09fa9434e65837193bd77ca7b281c6847f8b8554fe48d98694e3c7e40dc8'];


$response = $this->call('POST', route('user.register'), $record, $cookies);
1 like
paboo's avatar
Level 1

when i try dd($response), Cookie return []. whats happen guy

gitwithravish's avatar

@paboo may I know why do you want to check if the cookie is there or not? What exactly are you trying to achieve here?

Sinnbeck's avatar

@paboo I am not quite sure what you are trying to do? First of, sending a request to an endpoint is a feature test :)

If you want to send cookies this is the syntax: https://laravel.com/docs/8.x/http-tests#cookies

$response = $this->withCookie('api-token', 'e3ef09fa9434e65837193bd77ca7b281c6847f8b8554fe48d98694e3c7e40dc8')->post(route('user.register'), $record);

//and assertion
$this->assertCookie('api-token');
//or
$this->assertPlainCookie('api-token','e3ef09fa9434e65837193bd77ca7b281c6847f8b8554fe48d98694e3c7e40dc8'); //unencrypted
1 like
paboo's avatar
Level 1

we login from social. when you login with social we will return cookie have api-token. first time you login with social you will register accout on our server. so i fake cookie for register thanks

paboo's avatar
Level 1

@sinnbeck $response return header is object but from app or postman return its array so can i convertJsonResponse toArray thanks

Sinnbeck's avatar

Can you show what you are trying currently? Did you set the cookie as shown?

1 like
trafoos's avatar

It means that if Laravel changes the session implementation, I would have to change my unit tests. Which would mean I would lose the advantage of the unit test , because it should be about change

Please or to participate in this conversation.