Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
karlomikus committed Dec 21, 2023
1 parent 9ae418d commit 36e1433
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function updateSubscription(Request $request): JsonResponse

Mail::to($request->user())->queue(new SubscriptionChanged($type));
} catch (Throwable $e) {
return response()->json($e->getMessage());
abort(400, $e->getMessage());
}

return response()->json(status: 204);
Expand All @@ -67,7 +67,7 @@ public function updateSubscription(Request $request): JsonResponse

Mail::to($request->user())->queue(new SubscriptionChanged($type));
} catch (Throwable $e) {
return response()->json($e->getMessage());
abort(400, $e->getMessage());
}

return response()->json(status: 204);
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/Http/SubscriptionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Laravel\Paddle\Cashier;
use Kami\Cocktail\Models\User;
use Laravel\Paddle\Subscription;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Config;
use Kami\Cocktail\Mail\SubscriptionChanged;
use Illuminate\Testing\Fluent\AssertableJson;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -136,13 +138,15 @@ public function test_subscription_active_response(): void

public function test_subscription_pause_response(): void
{
Mail::fake();
$user = auth()->user();

Cashier::fake([
'subscriptions/sub_12345/pause' => [
'data' => [
'status' => 'paused',
'paused_at' => now(),
'items' => [],
],
]
]);
Expand All @@ -164,18 +168,22 @@ public function test_subscription_pause_response(): void
$response = $this->postJson('/api/billing/subscription', ['type' => 'pause']);
$response->assertSuccessful();

Mail::assertQueued(SubscriptionChanged::class);

$this->assertDatabaseHas('subscriptions', ['paddle_id' => 'sub_12345', 'status' => Subscription::STATUS_PAUSED]);
}

public function test_subscription_resume_response(): void
{
Mail::fake();
$user = auth()->user();

Cashier::fake([
'subscriptions/sub_12345/resume' => [
'data' => [
'status' => 'active',
'paused_at' => null,
'items' => [],
],
]
]);
Expand All @@ -197,6 +205,8 @@ public function test_subscription_resume_response(): void
$response = $this->postJson('/api/billing/subscription', ['type' => 'resume']);
$response->assertSuccessful();

Mail::assertQueued(SubscriptionChanged::class);

$this->assertDatabaseHas('subscriptions', ['paddle_id' => 'sub_12345', 'status' => Subscription::STATUS_ACTIVE]);
}
}

0 comments on commit 36e1433

Please sign in to comment.