Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Tests\Controller;
- use DateTime;
- use App\Entity\User;
- use App\Entity\Subscription;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
- class WebhookControllerTest extends WebTestCase
- {
- /** @var EntityManagerInterface */
- private $em;
- public static $container;
- public function setUp():void
- {
- dump("test1");
- self::bootKernel();
- dump("test2");
- $this->em = self::$container->get('doctrine')->getManager();
- dump("test3");
- }
- public function testStripeCustomerSubscriptionDeleted()
- {
- dump("test de la fonction");
- $subscription = $this->createSubscription();
- dump($subscription->getStripePlanId());
- $eventJson = $this->getCustomerSubscriptionDeletedEvent($subscription->getStripeSubscriptionId());
- $client = static::createClient();
- $client->request(
- 'POST',
- '/webhooks/stripe',
- [],
- [],
- [],
- $eventJson
- );
- dump($client->getResponse()->getContent());
- $this->assertEquals(200, $client->getResponse()->getStatusCode());
- $this->assertFalse($subscription->isActive());
- }
- private function createSubscription()
- {
- $user = new User();
- $user->setEmail('fluffy'.mt_rand().'@sheep.com');
- $user->setUsername('fluffy'.mt_rand());
- $user->setPassword("password");
- $subscription = new Subscription();
- $subscription->setUser($user);
- $subscription->activateSubscription(
- 'plan_STRIPE_TEST_ABC'.mt_rand(),
- 'sub_STRIPE_TEST_XYZ'.mt_rand(),
- new \DateTime('+1 month')
- );
- dump($subscription);
- // $this->em = self::$container->get('doctrine')->getManager();
- $this->em->persist($user);
- $this->em->persist($subscription);
- $this->em->flush();
- return $subscription;
- }
- private function getCustomerSubscriptionDeletedEvent($subscriptionId)
- {
- $json = <<<EOF
- {
- "created": 1326853478,
- "livemode": false,
- "id": "evt_00000000000000",
- "type": "customer.subscription.deleted",
- "object": "event",
- "request": null,
- "pending_webhooks": 1,
- "api_version": "2019-12-03",
- "data": {
- "object": {
- "id": "sub_00000000000000",
- "object": "subscription",
- "application_fee_percent": null,
- "billing_cycle_anchor": 1579617600,
- "billing_thresholds": null,
- "cancel_at": null,
- "cancel_at_period_end": false,
- "canceled_at": null,
- "collection_method": "charge_automatically",
- "created": 1579617600,
- "current_period_end": 1582296000,
- "current_period_start": 1579617600,
- "customer": "cus_00000000000000",
- "days_until_due": null,
- "default_payment_method": null,
- "default_source": null,
- "default_tax_rates": [
- ],
- "discount": null,
- "ended_at": 1579694523,
- "items": {
- "object": "list",
- "data": [
- {
- "id": "%s",
- "object": "subscription_item",
- "billing_thresholds": null,
- "created": 1579617601,
- "metadata": {
- },
- "plan": {
- "id": "farmer_00000000000000",
- "object": "plan",
- "active": true,
- "aggregate_usage": null,
- "amount": 9900,
- "amount_decimal": "9900",
- "billing_scheme": "per_unit",
- "created": 1579548278,
- "currency": "eur",
- "interval": "month",
- "interval_count": 1,
- "livemode": false,
- "metadata": {
- },
- "nickname": "Farmer Brent (monthly)",
- "product": "prod_00000000000000",
- "tiers": null,
- "tiers_mode": null,
- "transform_usage": null,
- "trial_period_days": null,
- "usage_type": "licensed"
- },
- "quantity": 1,
- "subscription": "sub_00000000000000",
- "tax_rates": [
- ]
- }
- ],
- "has_more": false,
- "url": "/v1/subscription_items?subscription=sub_GaZ1c9WocA6CcW"
- },
- "latest_invoice": "in_1G3NtcGgCQgXBLKXe6AFO0Wf",
- "livemode": false,
- "metadata": {
- },
- "next_pending_invoice_item_invoice": null,
- "pending_invoice_item_interval": null,
- "pending_setup_intent": null,
- "pending_update": null,
- "plan": {
- "id": "farmer_00000000000000",
- "object": "plan",
- "active": true,
- "aggregate_usage": null,
- "amount": 9900,
- "amount_decimal": "9900",
- "billing_scheme": "per_unit",
- "created": 1579548278,
- "currency": "eur",
- "interval": "month",
- "interval_count": 1,
- "livemode": false,
- "metadata": {
- },
- "nickname": "Farmer Brent (monthly)",
- "product": "prod_00000000000000",
- "tiers": null,
- "tiers_mode": null,
- "transform_usage": null,
- "trial_period_days": null,
- "usage_type": "licensed"
- },
- "quantity": 1,
- "schedule": null,
- "start_date": 1579617600,
- "status": "canceled",
- "tax_percent": null,
- "trial_end": null,
- "trial_start": null
- }
- }
- }
- EOF;
- return sprintf($json, $subscriptionId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement