@@ -21,15 +21,7 @@ class HandleCustomerSubscriptionCreatedJobTest extends TestCase
2121 /** @test */
2222 public function it_dispatches_the_create_anystack_license_job_with_correct_data ()
2323 {
24- $ mockCustomer = Customer::constructFrom ([
25- 'id ' => 'cus_S9dhoV2rJK2Auy ' ,
26- 27- 'name ' => 'John Doe ' ,
28- ]);
29-
30- $ this ->mockStripeClient ($ mockCustomer );
31-
32- dispatch_sync (new CreateUserFromStripeCustomer ($ mockCustomer ));
24+ $ this ->createTestData ('John Doe ' );
3325
3426 Bus::fake ();
3527
@@ -54,15 +46,7 @@ public function it_dispatches_the_create_anystack_license_job_with_correct_data(
5446 */
5547 public function it_extracts_customer_name_parts_correctly ($ fullName , $ expectedFirstName , $ expectedLastName )
5648 {
57- $ mockCustomer = Customer::constructFrom ([
58- 'id ' => 'cus_S9dhoV2rJK2Auy ' ,
59- 60- 'name ' => $ fullName ,
61- ]);
62-
63- $ this ->mockStripeClient ($ mockCustomer );
64-
65- dispatch_sync (new CreateUserFromStripeCustomer ($ mockCustomer ));
49+ $ this ->createTestData ($ fullName );
6650
6751 $ webhookCall = new WebhookHandled ($ this ->getTestWebhookPayload ());
6852
@@ -117,6 +101,37 @@ public function it_fails_when_customer_has_no_email()
117101 Bus::assertNotDispatched (CreateAnystackLicenseJob::class);
118102 }
119103
104+ protected function createTestData (?string $ customerName )
105+ {
106+ $ mockCustomer = Customer::constructFrom ([
107+ 'id ' => $ this ->getTestWebhookPayload ()['data ' ]['object ' ]['customer ' ],
108+ 'email ' =>
$ email =
'[email protected] ' ,
109+ 'name ' => $ customerName ,
110+ ]);
111+
112+ $ this ->mockStripeClient ($ mockCustomer );
113+
114+ dispatch_sync (new CreateUserFromStripeCustomer ($ mockCustomer ));
115+
116+ $ user = User::query ()->where ('email ' , $ email )->firstOrFail ();
117+
118+ $ subscription = \Laravel \Cashier \Subscription::factory ()
119+ ->for ($ user , 'user ' )
120+ ->create ([
121+ 'stripe_id ' => $ this ->getTestWebhookPayload ()['data ' ]['object ' ]['id ' ],
122+ 'stripe_status ' => 'active ' ,
123+ 'stripe_price ' => $ this ->getTestWebhookPayload ()['data ' ]['object ' ]['items ' ]['data ' ][0 ]['price ' ]['id ' ],
124+ 'quantity ' => 1 ,
125+ ]);
126+ $ subscriptionItem = \Laravel \Cashier \SubscriptionItem::factory ()
127+ ->for ($ subscription , 'subscription ' )
128+ ->create ([
129+ 'stripe_id ' => $ this ->getTestWebhookPayload ()['data ' ]['object ' ]['items ' ]['data ' ][0 ]['id ' ],
130+ 'stripe_price ' => $ this ->getTestWebhookPayload ()['data ' ]['object ' ]['items ' ]['data ' ][0 ]['price ' ]['id ' ],
131+ 'quantity ' => 1 ,
132+ ]);
133+ }
134+
120135 protected function getTestWebhookPayload (): array
121136 {
122137 return [
0 commit comments