2

I seem to be facing the general problem that directive driven mutations won't properly work when unit testing them. Using GraphQL Playground everything works as expected and the User model is auto detected. But if unit testing the same functionality it fails.

All config values are properly set (as I can see from regular requests not failing):

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => ClientName\PackageName\Models\User::class,
        ],
   ...
]

And lighthouse config values also set to the namespace above!

So for example this mutation would trigger the UserPolicy class and after that automatically call the create method of the User class:

type Mutation @protect(guards: ["api"]) {
  "Create user"
  createUser(data: NewUserInput! @spread): User
  @can(ability: "create")
  @create 
}

But calling the same request via phpunit would result in errors: for @can it returns a not authorized error and if I remove @can it only updates the email field of the user model as if mass assignment is not properly set.

When I now change the above schema definition to:

type Mutation @protect(guards: ["api"]) {
  "Create user"
  createUser(data: NewUserInput! @spread): User
  @can(ability: "create" model: "ClientName\\PackageName\\Models\\User")
  @create(model: "ClientName\\PackageName\\Models\\User") 
}

And it works as expected within phpunit tests!

Now I suspect that within tests there is not User model found or not the appropriate but I cannot understand why this is.

Does anyone have any idea?

carsten
  • 191
  • 1
  • 2
  • 7

0 Answers0