Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. <?php
  2. namespace App\Tests\Controller;
  3.  
  4. use DateTime;
  5. use App\Entity\User;
  6. use App\Entity\Subscription;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  9.  
  10. class WebhookControllerTest extends WebTestCase
  11. {
  12. /** @var EntityManagerInterface */
  13. private $em;
  14. public static $container;
  15.  
  16. public function setUp()
  17. {
  18. self::bootKernel();
  19. $this->em = $this->container->get('doctrine')->getManager();
  20. }
  21.  
  22. public function testStripeCustomerSubscriptionDeleted()
  23. {
  24. $subscription = $this->createSubscription();
  25.  
  26. $eventJson = $this->getCustomerSubscriptionDeletedEvent($subscription->getStripeSubscriptionId());
  27.  
  28. $client = $this->createClient();
  29. $client->request(
  30. 'POST',
  31. 'webhooks/stripe',
  32. [],
  33. [],
  34. [],
  35. $eventJson
  36. );
  37.  
  38. dump($client->getResponse()->getContent());
  39. $this->assertEquals(200, $client->getResponse()->getStatusCode());
  40.  
  41. $this->assertFalse($subscription->isActive());
  42. }
  43.  
  44. private function createSubscription()
  45. {
  46. $user = new User();
  47. $user->setEmail('fluffy'.mt_rand().'@sheep.com');
  48. $user->setUsername('fluffy'.mt_rand());
  49. $user->setPassword("password");
  50.  
  51. $subscription = new Subscription();
  52. $subscription->setUser($user);
  53. $subscription->activateSubscription(
  54. 'plan_STRIPE_TEST_ABC'.mt_rand(),
  55. 'sub_STRIPE_TEST_XYZ'.mt_rand(),
  56. new \DateTime('+1 month')
  57. );
  58.  
  59. $this->em->persist($user);
  60. $this->em->persist($subscription);
  61. $this->em->flush();
  62.  
  63. return $subscription;
  64. }
  65.  
  66. private function getCustomerSubscriptionDeletedEvent($subscriptionId)
  67. {
  68. $json = <<<EOF
  69. {
  70. "created": 1326853478,
  71. "livemode": false,
  72. "id": "evt_00000000000000",
  73. "type": "customer.subscription.deleted",
  74. "object": "event",
  75. "request": null,
  76. "pending_webhooks": 1,
  77. "api_version": "2019-12-03",
  78. "data": {
  79. "object": {
  80. "id": "sub_00000000000000",
  81. "object": "subscription",
  82. "application_fee_percent": null,
  83. "billing_cycle_anchor": 1579617600,
  84. "billing_thresholds": null,
  85. "cancel_at": null,
  86. "cancel_at_period_end": false,
  87. "canceled_at": null,
  88. "collection_method": "charge_automatically",
  89. "created": 1579617600,
  90. "current_period_end": 1582296000,
  91. "current_period_start": 1579617600,
  92. "customer": "cus_00000000000000",
  93. "days_until_due": null,
  94. "default_payment_method": null,
  95. "default_source": null,
  96. "default_tax_rates": [
  97.  
  98. ],
  99. "discount": null,
  100. "ended_at": 1579694523,
  101. "items": {
  102. "object": "list",
  103. "data": [
  104. {
  105. "id": "%s",
  106. "object": "subscription_item",
  107. "billing_thresholds": null,
  108. "created": 1579617601,
  109. "metadata": {
  110. },
  111. "plan": {
  112. "id": "farmer_00000000000000",
  113. "object": "plan",
  114. "active": true,
  115. "aggregate_usage": null,
  116. "amount": 9900,
  117. "amount_decimal": "9900",
  118. "billing_scheme": "per_unit",
  119. "created": 1579548278,
  120. "currency": "eur",
  121. "interval": "month",
  122. "interval_count": 1,
  123. "livemode": false,
  124. "metadata": {
  125. },
  126. "nickname": "Farmer Brent (monthly)",
  127. "product": "prod_00000000000000",
  128. "tiers": null,
  129. "tiers_mode": null,
  130. "transform_usage": null,
  131. "trial_period_days": null,
  132. "usage_type": "licensed"
  133. },
  134. "quantity": 1,
  135. "subscription": "sub_00000000000000",
  136. "tax_rates": [
  137.  
  138. ]
  139. }
  140. ],
  141. "has_more": false,
  142. "url": "/v1/subscription_items?subscription=sub_GaZ1c9WocA6CcW"
  143. },
  144. "latest_invoice": "in_1G3NtcGgCQgXBLKXe6AFO0Wf",
  145. "livemode": false,
  146. "metadata": {
  147. },
  148. "next_pending_invoice_item_invoice": null,
  149. "pending_invoice_item_interval": null,
  150. "pending_setup_intent": null,
  151. "pending_update": null,
  152. "plan": {
  153. "id": "farmer_00000000000000",
  154. "object": "plan",
  155. "active": true,
  156. "aggregate_usage": null,
  157. "amount": 9900,
  158. "amount_decimal": "9900",
  159. "billing_scheme": "per_unit",
  160. "created": 1579548278,
  161. "currency": "eur",
  162. "interval": "month",
  163. "interval_count": 1,
  164. "livemode": false,
  165. "metadata": {
  166. },
  167. "nickname": "Farmer Brent (monthly)",
  168. "product": "prod_00000000000000",
  169. "tiers": null,
  170. "tiers_mode": null,
  171. "transform_usage": null,
  172. "trial_period_days": null,
  173. "usage_type": "licensed"
  174. },
  175. "quantity": 1,
  176. "schedule": null,
  177. "start_date": 1579617600,
  178. "status": "canceled",
  179. "tax_percent": null,
  180. "trial_end": null,
  181. "trial_start": null
  182. }
  183. }
  184. }
  185. EOF;
  186.  
  187. return sprintf($json, $subscriptionId);
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement