@@ -31,7 +31,7 @@ protected function setUp(): void
31
31
}
32
32
33
33
#[Test]
34
- public function a_user_is_created_when_a_stripe_customer_is_created ()
34
+ public function a_user_is_not_created_when_a_stripe_customer_is_created ()
35
35
{
36
36
Bus::fake ();
37
37
@@ -49,9 +49,89 @@ public function a_user_is_created_when_a_stripe_customer_is_created()
49
49
50
50
$ this ->postJson ('/stripe/webhook ' , $ payload );
51
51
52
+ Bus::assertNotDispatched (CreateUserFromStripeCustomer::class);
53
+ }
54
+
55
+ #[Test]
56
+ public function a_user_is_created_when_a_stripe_customer_subscription_is_created_and_a_matching_user_doesnt_exist ()
57
+ {
58
+ Bus::fake ();
59
+
60
+ $ this ->mockStripeClient ();
61
+
62
+ $ payload = [
63
+ 'id ' => 'evt_test_webhook ' ,
64
+ 'type ' => 'customer.subscription.created ' ,
65
+ 'data ' => [
66
+ 'object ' => [
67
+ 'id ' => 'sub_test123 ' ,
68
+ 'customer ' => 'cus_test123 ' ,
69
+ 'status ' => 'active ' ,
70
+ 'items ' => [
71
+ 'object ' => 'list ' ,
72
+ 'data ' => [
73
+ [
74
+ 'id ' => 'si_test ' ,
75
+ 'price ' => [
76
+ 'id ' => Subscription::Max->stripePriceId (),
77
+ 'product ' => 'prod_test ' ,
78
+ ],
79
+ 'quantity ' => 1 ,
80
+ ],
81
+ ],
82
+ ],
83
+ ],
84
+ ],
85
+ ];
86
+
87
+ $ this ->postJson ('/stripe/webhook ' , $ payload );
88
+
52
89
Bus::assertDispatched (CreateUserFromStripeCustomer::class);
53
90
}
54
91
92
+ #[Test]
93
+ public function a_user_is_not_created_when_a_stripe_customer_subscription_is_created_if_a_matching_user_already_exists ()
94
+ {
95
+ Bus::fake ();
96
+
97
+ $ user = User::factory ()->create ([
98
+ 'stripe_id ' => 'cus_test123 ' ,
99
+ 'name ' => 'John Doe ' ,
100
+
101
+ ]);
102
+
103
+ $ this ->mockStripeClient ($ user );
104
+
105
+ $ payload = [
106
+ 'id ' => 'evt_test_webhook ' ,
107
+ 'type ' => 'customer.subscription.created ' ,
108
+ 'data ' => [
109
+ 'object ' => [
110
+ 'id ' => 'sub_test123 ' ,
111
+ 'customer ' => $ user ->stripe_id ,
112
+ 'status ' => 'active ' ,
113
+ 'items ' => [
114
+ 'object ' => 'list ' ,
115
+ 'data ' => [
116
+ [
117
+ 'id ' => 'si_test ' ,
118
+ 'price ' => [
119
+ 'id ' => Subscription::Max->stripePriceId (),
120
+ 'product ' => 'prod_test ' ,
121
+ ],
122
+ 'quantity ' => 1 ,
123
+ ],
124
+ ],
125
+ ],
126
+ ],
127
+ ],
128
+ ];
129
+
130
+ $ this ->postJson ('/stripe/webhook ' , $ payload );
131
+
132
+ Bus::assertNotDispatched (CreateUserFromStripeCustomer::class);
133
+ }
134
+
55
135
#[Test]
56
136
public function a_license_is_created_when_a_stripe_subscription_is_created ()
57
137
{
@@ -95,6 +175,7 @@ public function a_license_is_created_when_a_stripe_subscription_is_created()
95
175
Bus::assertDispatched (CreateAnystackLicenseJob::class, function (CreateAnystackLicenseJob $ job ) {
96
176
return $ job->
user ->
email ===
'[email protected] ' &&
97
177
$ job ->subscription === Subscription::Max &&
178
+ $ job ->subscriptionItemId === $ job ->user ->subscriptions ->first ()->items ()->first ()->id &&
98
179
$ job ->firstName === 'John ' &&
99
180
$ job ->lastName === 'Doe ' ;
100
181
});
@@ -105,7 +186,7 @@ public function a_license_is_created_when_a_stripe_subscription_is_created()
105
186
$ this ->assertNotEmpty ($ user ->subscriptions ->first ()->items );
106
187
}
107
188
108
- protected function mockStripeClient (User $ user ): void
189
+ protected function mockStripeClient (? User $ user = null ): void
109
190
{
110
191
$ mockStripeClient = $ this ->createMock (StripeClient::class);
111
192
$ mockStripeClient ->customers = new class ($ user )
@@ -120,13 +201,15 @@ public function __construct($user)
120
201
public function retrieve ()
121
202
{
122
203
return Customer::constructFrom ([
123
- 'id ' => $ this ->user ->stripe_id ,
124
- 'name ' => $ this ->user ->name ,
125
- 'email ' => $ this ->user ->email ,
204
+ 'id ' => $ this ->user ? ->stripe_id ?: ' cus_test123 ' ,
205
+ 'name ' => $ this ->user ? ->name ?: ' Test Customer ' ,
206
+ 'email ' =>
$ this ->
user ? ->email
?: ' [email protected] ' ,
126
207
]);
127
208
}
128
209
};
129
210
130
- $ this ->app ->instance (StripeClient::class, $ mockStripeClient );
211
+ $ this ->app ->bind (StripeClient::class, function ($ app , $ parameters ) use ($ mockStripeClient ) {
212
+ return $ mockStripeClient ;
213
+ });
131
214
}
132
215
}
0 commit comments