marvinrabe / laravel-graphql-test

🛠 Elegant GraphQL testing utilities for Laravel.
MIT License
57 stars 2 forks source link

PHPUnit testing Authentication - Logout #3

Closed imd-solutions closed 5 years ago

imd-solutions commented 5 years ago

I have tried testing the authentication process with PHPUnit. I have managed to test all mutations apart from the logout.

$this->mutation('logout', ['status', 'message']); logout = the mutation, [status and message] = the LogoutResponse payload

When I run that, I get the below error message:

“message” => “Syntax Error: Expected Name, found Int”

It would seem that it is asking for a second parameter (Which I am guessing is the Authorization Bearer info), but in the schema, there are no parameters, just the payload:

Schema: logout: LogoutResponse!

I have got it working with another package:

$response = $this->postGraphQL([ 'query' => 'mutation { logout { status message } }' ], [ 'Authorization' => 'Bearer '.$token ]);

But I would ideally like to get it working with your package.

Would appreciate a stir in the right direct.

marvinrabe commented 5 years ago

When using mutations with only two arguments the second argument is expected to be the arguments. Only for queries you can omit the arguments by only using two arguments.

$this->mutation('logout', ['status', 'message']);

This results in this query:

mutation {
   logout(0: 'status', 1: 'message')
}

What you want to do is this:

$this->mutation('logout', [], ['status', 'message']);

Which results in this:

mutation {
   logout {
      status
      message
   }
}

See this table for correct argument order:

Method Arguments Result
query (object) GraphQLClient
query (object, selectionSet) TestResponse
query (object, arguments, selectionSet) TestResponse
mutation (object) GraphQLClient
mutation (object, arguments) TestResponse
mutation (object, arguments, selectionSet) TestResponse