62 - Refactoring Towards Duplication

This commit is contained in:
Adam Wathan
2017-01-17 19:57:34 -05:00
parent 74ed409e46
commit f1a57e27f6
4 changed files with 32 additions and 5 deletions

View File

@@ -5,4 +5,6 @@ namespace App\Billing;
interface PaymentGateway interface PaymentGateway
{ {
public function charge($amount, $token); public function charge($amount, $token);
public function getValidTestToken();
} }

View File

@@ -26,4 +26,16 @@ class StripePaymentGateway implements PaymentGateway
throw new PaymentFailedException; 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;
}
} }

View File

@@ -8,10 +8,15 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
class FakePaymentGatewayTest extends TestCase class FakePaymentGatewayTest extends TestCase
{ {
protected function getPaymentGateway()
{
return new FakePaymentGateway;
}
/** @test */ /** @test */
function charges_with_a_valid_payment_token_are_successful() function charges_with_a_valid_payment_token_are_successful()
{ {
$paymentGateway = new FakePaymentGateway; $paymentGateway = $this->getPaymentGateway();
$paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); $paymentGateway->charge(2500, $paymentGateway->getValidTestToken());

View File

@@ -14,15 +14,23 @@ class StripePaymentGatewayTest extends TestCase
$this->lastCharge = $this->lastCharge(); $this->lastCharge = $this->lastCharge();
} }
protected function getPaymentGateway()
{
return new StripePaymentGateway(config('services.stripe.secret'));
}
/** @test */ /** @test */
function charges_with_a_valid_payment_token_are_successful() 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->assertCount(1, $newCharges);
$this->assertEquals(2500, $this->lastCharge()->amount); $this->assertEquals(2500, $newCharges->sum());
} }
/** @test */ /** @test */