From f1a57e27f6ff18e1125fbc8866d264b6fc947aac Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Tue, 17 Jan 2017 19:57:34 -0500 Subject: [PATCH] 62 - Refactoring Towards Duplication --- app/Billing/PaymentGateway.php | 2 ++ app/Billing/StripePaymentGateway.php | 12 ++++++++++++ tests/unit/Billing/FakePaymentGatewayTest.php | 7 ++++++- tests/unit/Billing/StripePaymentGatewayTest.php | 16 ++++++++++++---- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/Billing/PaymentGateway.php b/app/Billing/PaymentGateway.php index 21b1f5b..b1f5bf8 100644 --- a/app/Billing/PaymentGateway.php +++ b/app/Billing/PaymentGateway.php @@ -5,4 +5,6 @@ namespace App\Billing; interface PaymentGateway { public function charge($amount, $token); + + public function getValidTestToken(); } diff --git a/app/Billing/StripePaymentGateway.php b/app/Billing/StripePaymentGateway.php index 0022d26..ac177b5 100644 --- a/app/Billing/StripePaymentGateway.php +++ b/app/Billing/StripePaymentGateway.php @@ -26,4 +26,16 @@ class StripePaymentGateway implements PaymentGateway throw new PaymentFailedException; } } + + public function getValidTestToken() + { + return \Stripe\Token::create([ + "card" => [ + "number" => "4242424242424242", + "exp_month" => 1, + "exp_year" => date('Y') + 1, + "cvc" => "123" + ] + ], ['api_key' => $this->apiKey])->id; + } } diff --git a/tests/unit/Billing/FakePaymentGatewayTest.php b/tests/unit/Billing/FakePaymentGatewayTest.php index a5caccb..42c8c17 100644 --- a/tests/unit/Billing/FakePaymentGatewayTest.php +++ b/tests/unit/Billing/FakePaymentGatewayTest.php @@ -8,10 +8,15 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; class FakePaymentGatewayTest extends TestCase { + protected function getPaymentGateway() + { + return new FakePaymentGateway; + } + /** @test */ function charges_with_a_valid_payment_token_are_successful() { - $paymentGateway = new FakePaymentGateway; + $paymentGateway = $this->getPaymentGateway(); $paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); diff --git a/tests/unit/Billing/StripePaymentGatewayTest.php b/tests/unit/Billing/StripePaymentGatewayTest.php index 7722700..61b846b 100644 --- a/tests/unit/Billing/StripePaymentGatewayTest.php +++ b/tests/unit/Billing/StripePaymentGatewayTest.php @@ -14,15 +14,23 @@ class StripePaymentGatewayTest extends TestCase $this->lastCharge = $this->lastCharge(); } + protected function getPaymentGateway() + { + return new StripePaymentGateway(config('services.stripe.secret')); + } + /** @test */ function charges_with_a_valid_payment_token_are_successful() { - $paymentGateway = new StripePaymentGateway(config('services.stripe.secret')); + $paymentGateway = $this->getPaymentGateway(); - $paymentGateway->charge(2500, $this->validToken()); + // How could we make this API work? + $newCharges = $paymentGateway->newChargesDuring(function () { + $paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); + }); - $this->assertCount(1, $this->newCharges()); - $this->assertEquals(2500, $this->lastCharge()->amount); + $this->assertCount(1, $newCharges); + $this->assertEquals(2500, $newCharges->sum()); } /** @test */