Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afrayedknot's avatar

How to mock Guard

How do I mock Guard in L5 testing?

use Illuminate\Contracts\Auth\Guard;

class SubscriptionController extends WebhookController {

    protected $auth;

    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    public function postCancel()
    {
        $this->auth->user()->subscription()->cancel();
    }
}

How to I mock this so in my test I dont actually hit the stripe API?

0 likes
1 reply
LukasNeicelis's avatar

You can rebind guard mock to ioc container before test is run (setUp method in PHPUnit) or you can use aspect mock.

Please or to participate in this conversation.