Skip to content

Instantly share code, notes, and snippets.

@inxilpro
Created March 25, 2024 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inxilpro/75cb720414e0e5a1fbf03492ac16d16d to your computer and use it in GitHub Desktop.
Save inxilpro/75cb720414e0e5a1fbf03492ac16d16d to your computer and use it in GitHub Desktop.
<?php
class DemoTest extends DatabaseTestCase
{
use TestsMoney;
use TestsStripeWebhooks;
use MocksAuthorizeNet;
protected User $owner;
protected Company $owner_company;
protected Plan $destination_plan;
protected Plan $owner_plan;
protected User $user1;
protected Company $user1_company;
protected Subscription $owner_subscription;
protected Subscription $user1_subscription;
protected MockStripeClient $stripe_mock;
protected function setUp(): void
{
parent::setUp();
// Test was manually run on 2024-03-21 14:19:23
Date::setTestNow(Date::createFromTimestamp(1711045163));
$this->stripe_mock = MockStripeClient::fake()->preventStrayRequests();
$this->setUpPlans();
$this->setUpCompany();
$this->setUpUsers();
$this->setUpSubscriptions();
}
public function test_demo(): void
{
$this->assertStripeApiVersionMatchesReplay();
// Advance time to 2024-03-21 14:22:11
Date::setTestNow(Date::createFromTimestamp(1711045331));
$this->runInitialConversionToStripeReplays();
// FIXME: Implement assertions
// Advance time to 2024-03-21 14:22:11
Date::setTestNow(Date::createFromTimestamp(1711045331));
$this->runFirstRecurringPaymentReplays();
// FIXME: Implement assertions
}
protected function setUpPlans(): void
{
$this->destination_plan = Plan::factory()->createQuietly([
'id' => 123,
'interval_unit' => 'months',
'interval_length' => 1,
'cents' => 4900,
]);
PaymentGatewayPlan::factory()->createQuietly([
'id' => 123,
'plan_id' => 123,
'payment_gateway' => 'stripe',
'payment_gateway_id' => 'price_abc123',
]);
$this->owner_plan = Plan::factory()->createQuietly([
'id' => 1234,
'interval_unit' => 'months',
'interval_length' => 12,
'cents' => 49900,
]);
PaymentGatewayPlan::factory()->createQuietly([
'id' => 123,
'plan_id' => 123,
'payment_gateway' => 'stripe',
'payment_gateway_id' => 'price_abc123',
]);
Plan::factory()->createQuietly([
'id' => 1234,
'interval_unit' => 'months',
'interval_length' => 12,
'cents' => 10000,
]);
}
protected function setUpCompany(): void
{
// Create the company that will be moving to Stripe
$this->owner_company = Company::factory()->createQuietly([
'id' => 1234,
'company_name' => 'Demo Company',
'payment_gateway' => 'authorize_net',
'payment_gateway_id' => '1234',
'next_payment_at' => '2024-05-14 23:59:59',
'owner_id' => null,
]);
$this->owner_company->setMetadata('stripe_customer_id', 'cus_abc123');
}
protected function setUpUsers(): void
{
// Create the main company owner
$this->owner = User::factory()->createQuietly([
'id' => 1234,
'account_id' => 1234,
'confirmed' => true,
]);
CompanyMembership::factory()->createQuietly([
'account_id' => 1234,
'user_id' => 1234,
'role' => 'manager',
]);
// Add company member 1
$this->user1 = User::factory()->createQuietly([
'id' => 1234,
'account_id' => 1234,
'confirmed' => true,
]);
CompanyMembership::factory()->createQuietly([
'account_id' => 1234,
'user_id' => 1234,
'role' => 'user',
]);
}
protected function setUpSubscriptions(): void
{
$this->owner_subscription = Subscription::factory()->createQuietly([
'account_id' => 1234,
'plan_id' => 1234,
'starts_at' => '2023-05-14 00:00:00',
'trial_ends_at' => null,
'ends_at' => '2024-05-14 23:59:59',
'created_at' => '2023-05-05 14:46:32',
]);
$this->user1_subscription = Subscription::factory()->createQuietly([
'account_id' => 1234,
'plan_id' => 1234,
'starts_at' => '2023-05-14 00:00:00',
'trial_ends_at' => null,
'ends_at' => '2024-05-14 23:59:59',
'created_at' => '2023-05-05 14:46:58',
]);
}
protected function assertStripeApiVersionMatchesReplay(): void
{
$this->assertEquals('2022-11-15', Stripe::getApiVersion());
}
protected function runInitialConversionToStripeReplays(): void
{
$this->postStripeWebhookReplay('demo/00000_checkout.session.expired')->assertOk();
$this->postStripeWebhookReplay('demo/00000_customer.created')->assertOk();
$this->postStripeWebhookReplay('demo/00000_coupon.created')->assertOk();
$this->postStripeWebhookReplay('demo/00000_payment_method.attached')->assertOk();
$this->postStripeWebhookReplay('demo/00000_setup_intent.succeeded')->assertOk();
$this->postStripeWebhookReplay('demo/00000_setup_intent.created')->assertOk();
$this->postStripeWebhookReplay('demo/00000_checkout.session.completed')->assertOk();
$this->postStripeWebhookReplay('demo/00000_customer.updated')->assertOk();
$this->postStripeWebhookReplay('demo/00000_coupon.updated')->assertOk();
$this->postStripeWebhookReplay('demo/00000_customer.discount.created')->assertOk();
$this->postStripeWebhookReplay('demo/00000_customer.discount.deleted')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.created')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.finalized')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.paid')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.payment_succeeded')->assertOk();
$this->postStripeWebhookReplay('demo/00000_customer.subscription.created')->assertOk();
}
protected function runFirstRecurringPaymentReplays(): void
{
$this->postStripeWebhookReplay('demo/00000_invoice.upcoming')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.created')->assertOk();
$this->postStripeWebhookReplay('demo/00000_customer.subscription.updated')->assertOk();
$this->postStripeWebhookReplay('demo/00000_payment_intent.succeeded')->assertOk();
$this->postStripeWebhookReplay('demo/00000_charge.succeeded')->assertOk();
$this->postStripeWebhookReplay('demo/00000_customer.updated')->assertOk();
$this->postStripeWebhookReplay('demo/00000_payment_intent.created')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.updated')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.paid')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.payment_succeeded')->assertOk();
$this->postStripeWebhookReplay('demo/00000_invoice.finalized')->assertOk();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment