But [my unit test] fails because I'm doing multiple tests including checking that an email has been sent at the end. It fails because of the consequence of the exception (email not sent), not because of the exception itself.
@axeloz Then that’s not a unit test; that’s very much a feature test if you’re testing that many things.
Also, just gobbling exceptions in your controller action or whatever that is completely defeats the point of having an exception handler in your application.
You’d be better off just trying to update the default payment method and letting your exception handler handle exceptions:
public function register()
{
$this->renderable(WhateverStripeException::class, function () {
return redirect()->back()->withInput()->with('error', __('Error updating payment method.'));
});
}
Your controller should only be interested in performing business logic; not error handling. Once the payment is updated, you could dispatch an event that has a listener to send whatever email. Again, your controller shouldn’t be concerned with side effects.
public function storePayment(Request $request)
{
$user = $request->user();
$user->updateDefaultPaymentMethod($request->input('setup_method'));
DefaultPaymentMethodUpdated::dispatch($user);
// Return successful response
}
Alternatively, you could just not dispatch an event at all given Stripe will send you a webhook if the customer's default payment method is updated, and you could instead send the email off the back of that webhook event.