From 3472361c350ae3883b5a209a78171a3ba711c735 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Thu, 29 Dec 2016 21:21:03 -0500 Subject: [PATCH] 56 - Dealing with Lingering State --- .../unit/Billing/StripePaymentGatewayTest.php | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/tests/unit/Billing/StripePaymentGatewayTest.php b/tests/unit/Billing/StripePaymentGatewayTest.php index 119c160..66fe196 100644 --- a/tests/unit/Billing/StripePaymentGatewayTest.php +++ b/tests/unit/Billing/StripePaymentGatewayTest.php @@ -7,13 +7,44 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; class StripePaymentGatewayTest extends TestCase { + protected function setUp() + { + parent::setUp(); + $this->lastCharge = $this->lastCharge(); + } + /** @test */ function charges_with_a_valid_payment_token_are_successful() { - // Create a new StripePaymentGateway $paymentGateway = new StripePaymentGateway(config('services.stripe.secret')); - $token = \Stripe\Token::create([ + $paymentGateway->charge(2500, $this->validToken()); + + $this->assertCount(1, $this->newCharges()); + $this->assertEquals(2500, $this->lastCharge()->amount); + } + + private function lastCharge() + { + return array_first(\Stripe\Charge::all( + ['limit' => 1], + ['api_key' => config('services.stripe.secret')] + )['data']); + } + + private function newCharges() + { + return \Stripe\Charge::all( + [ + 'ending_before' => $this->lastCharge ? $this->lastCharge->id : null, + ], + ['api_key' => config('services.stripe.secret')] + )['data']; + } + + private function validToken() + { + return \Stripe\Token::create([ "card" => [ "number" => "4242424242424242", "exp_month" => 1, @@ -21,16 +52,5 @@ class StripePaymentGatewayTest extends TestCase "cvc" => "123" ] ], ['api_key' => config('services.stripe.secret')])->id; - - // Create a new charge for some amount using a valid token - $paymentGateway->charge(2500, $token); - - // Verify that the charge was completed successfully - $lastCharge = \Stripe\Charge::all( - ['limit' => 1], - ['api_key' => config('services.stripe.secret')] - )['data'][0]; - - $this->assertEquals(2500, $lastCharge->amount); } }