-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathStripePurchaseHandlingTest.php
More file actions
132 lines (110 loc) · 3.91 KB
/
Copy pathStripePurchaseHandlingTest.php
File metadata and controls
132 lines (110 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Tests\Feature;
use App\Enums\Subscription;
use App\Jobs\CreateAnystackLicenseJob;
use App\Jobs\CreateUserFromStripeCustomer;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Stripe\Customer;
use Stripe\StripeClient;
use Tests\TestCase;
class StripePurchaseHandlingTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
config()->set('cashier.webhook.secret', null);
Http::fake([
'https://api.anystack.sh/v1/contacts' => Http::response(['data' => ['id' => 'contact-123']], 200),
'https://api.anystack.sh/v1/products/*/licenses' => Http::response(['data' => ['key' => 'test-license-key-12345']], 200),
]);
}
#[Test]
public function a_user_is_created_when_a_stripe_customer_is_created()
{
Bus::fake();
$payload = [
'id' => 'evt_test_webhook',
'type' => 'customer.created',
'data' => [
'object' => [
'id' => 'cus_test123',
'name' => 'Test Customer',
'email' => 'test@example.com',
],
],
];
$this->postJson('/stripe/webhook', $payload);
Bus::assertDispatched(CreateUserFromStripeCustomer::class);
}
#[Test]
public function a_license_is_created_when_a_stripe_subscription_is_created()
{
Bus::fake([CreateAnystackLicenseJob::class]);
$user = User::factory()->create([
'stripe_id' => 'cus_test123',
'name' => 'John Doe',
'email' => 'john@example.com',
]);
$this->mockStripeClient($user);
$payload = [
'id' => 'evt_test_webhook',
'type' => 'customer.subscription.created',
'data' => [
'object' => [
'id' => 'sub_test123',
'customer' => 'cus_test123',
'status' => 'active',
'items' => [
'object' => 'list',
'data' => [
[
'id' => 'si_test',
'price' => [
'id' => Subscription::Max->stripePriceId(),
'product' => 'prod_test',
],
'quantity' => 1,
],
],
],
],
],
];
$this->postJson('/stripe/webhook', $payload);
Bus::assertDispatched(CreateAnystackLicenseJob::class, function (CreateAnystackLicenseJob $job) {
return $job->user->email === 'john@example.com' &&
$job->subscription === Subscription::Max &&
$job->firstName === 'John' &&
$job->lastName === 'Doe';
});
$user->refresh();
$this->assertNotEmpty($user->subscriptions);
$this->assertNotEmpty($user->subscriptions->first()->items);
}
protected function mockStripeClient(User $user): void
{
$mockStripeClient = $this->createMock(StripeClient::class);
$mockStripeClient->customers = new class($user)
{
private $user;
public function __construct($user)
{
$this->user = $user;
}
public function retrieve()
{
return Customer::constructFrom([
'id' => $this->user->stripe_id,
'name' => $this->user->name,
'email' => $this->user->email,
]);
}
};
$this->app->instance(StripeClient::class, $mockStripeClient);
}
}