I have a laravel app with passport installed to manage api auth. I’m trying to write some tests but I’m unable to create a Client as per the docs on laravel. I’ve googled similar SO answers but they all suggest using setUp and tearDown methods which I am doing. When I run the test I get
InvalidArgumentException Unable to locate factory for [LaravelPassportClient].
How can I get this to work?
Below is my code. I have included the Client model from the passport package and I am using the setUp and tearDown methods as suggested in similar SO answers.
I’ve tried composer dump-autoload
and php artisan config:cache
.
use LaravelPassportPassport; use LaravelPassportClient; ... use RefreshDatabase; protected function setUp(): void { parent::setUp(); } protected function tearDown(): void { parent::tearDown(); } public function testAPIEndpointFailsWhenNoParamIsSet() { Passport::actingAsClient( factory(Client::class)->create(), ['*'] ); $response = $this->postJson('/api/endpoint', [ 'param' => '' ]); $response->assertStatus(401) ->assertJson(['message' => 'Unauthenticated.']); }
Advertisement
Answer
passport client factory should be existed on publishing …
if it did not …. make it your self:
from : here
use LaravelPassportPassport; use LaravelPassportClient; $factory->define(Client::class, function (Faker $faker) { return [ 'user_id' => null, 'name' => $faker->company, 'secret' => Str::random(40), 'redirect' => $faker->url, 'personal_access_client' => 0, 'password_client' => 0, 'revoked' => 0, ]; });