Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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()
  16. {
  17. self::bootKernel();
  18. $this->em = self::$container->get('doctrine');
  19. }
  20.  
  21. public function testStripeCustomerSubscriptionDeleted()
  22. {
  23. $subscription = $this->createSubscription();
  24.  
  25. $eventJson = $this->getCustomerSubscriptionDeletedEvent($subscription->getStripeSubscriptionId());
  26.  
  27. $client = $this->createClient();
  28. $client->request(
  29. 'POST',
  30. 'webhooks/stripe',
  31. [],
  32. [],
  33. [],
  34. $eventJson
  35. );
  36.  
  37. dump($client->getResponse()->getContent());
  38. $this->assertEquals(200, $client->getResponse()->getStatusCode());
  39.  
  40. $this->assertFalse($subscription->isActive());
  41. }
  42.  
  43. private function createSubscription()
  44. {
  45. $user = new User();
  46. $user->setEmail('fluffy'.mt_rand().'@sheep.com');
  47. $user->setUsername('fluffy'.mt_rand());
  48. $user->setPassword("password");
  49.  
  50. $subscription = new Subscription();
  51. $subscription->setUser($user);
  52. $subscription->activateSubscription(
  53. 'plan_STRIPE_TEST_ABC'.mt_rand(),
  54. 'sub_STRIPE_TEST_XYZ'.mt_rand(),
  55. new \DateTime('+1 month')
  56. );
  57.  
  58. $this->em->persist($user);
  59. $this->em->persist($subscription);
  60. $this->em->flush();
  61.  
  62. return $subscription;
  63. }
  64.  
  65. private function getCustomerSubscriptionDeletedEvent($subscriptionId)
  66. {
  67. $json = <<<EOF
  68. {
  69. //...
  70. }
  71. EOF;
  72.  
  73. return sprintf($json, $subscriptionId);
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement