Advertisement
Guest User

Untitled

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