Advertisement
Guest User

WebhookControllerTest.php

a guest
Jan 23rd, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 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.  
  15. public function setUp():void
  16. {
  17. self::bootKernel();
  18. $this->em = self::$container->get('doctrine')->getManager();
  19. }
  20.  
  21. public function testStripeCustomerSubscriptionDeleted()
  22. {
  23. $subscription = $this->createSubscription();
  24.  
  25. $eventJson = $this->getCustomerSubscriptionDeletedEvent($subscription->getStripeSubscriptionId());
  26.  
  27. $client = static::createClient();
  28.  
  29. $client->request(
  30. 'POST',
  31. '/webhooks/stripe',
  32. [],
  33. [],
  34. [],
  35. $eventJson
  36. );
  37.  
  38. $this->assertEquals(200, $client->getResponse()->getStatusCode());
  39. var_dump($client->getResponse()->getContent());
  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": "2016-07-06",
  78. "data": {
  79. "object": {
  80. "id": "%s",
  81. "object": "subscription",
  82. "application_fee_percent": null,
  83. "cancel_at_period_end": true,
  84. "canceled_at": 1469731697,
  85. "created": 1469729305,
  86. "current_period_end": 1472407705,
  87. "current_period_start": 1469729305,
  88. "customer": "cus_00000000000000",
  89. "discount": null,
  90. "ended_at": 1470436151,
  91. "livemode": false,
  92. "metadata": {
  93. },
  94. "plan": {
  95. "id": "farmer_00000000000000",
  96. "object": "plan",
  97. "amount": 9900,
  98. "created": 1469720306,
  99. "currency": "usd",
  100. "interval": "month",
  101. "interval_count": 1,
  102. "livemode": false,
  103. "metadata": {
  104. },
  105. "name": "Farmer Brent (monthly)",
  106. "statement_descriptor": null,
  107. "trial_period_days": null
  108. },
  109. "quantity": 1,
  110. "start": 1469729305,
  111. "status": "canceled",
  112. "tax_percent": null,
  113. "trial_end": null,
  114. "trial_start": null
  115. }
  116. }
  117. }
  118. EOF;
  119.  
  120. return sprintf($json, $subscriptionId);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement