diff --git a/app/Billing/FakePaymentGateway.php b/app/Billing/FakePaymentGateway.php index 26182a6..c53b432 100644 --- a/app/Billing/FakePaymentGateway.php +++ b/app/Billing/FakePaymentGateway.php @@ -35,7 +35,7 @@ class FakePaymentGateway implements PaymentGateway { $chargesFrom = $this->charges->count(); $callback($this); - return $this->charges->slice($chargesFrom)->values(); + return $this->charges->slice($chargesFrom)->reverse()->values(); } public function totalCharges() diff --git a/composer.json b/composer.json index 3bec5de..b37808f 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ }, "autoload-dev": { "classmap": [ - "tests/TestCase.php" + "tests" ] }, "scripts": { diff --git a/tests/unit/Billing/FakePaymentGatewayTest.php b/tests/unit/Billing/FakePaymentGatewayTest.php index c2f4367..f623a82 100644 --- a/tests/unit/Billing/FakePaymentGatewayTest.php +++ b/tests/unit/Billing/FakePaymentGatewayTest.php @@ -8,40 +8,13 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; class FakePaymentGatewayTest extends TestCase { + use PaymentGatewayContractTests; + protected function getPaymentGateway() { return new FakePaymentGateway; } - /** @test */ - function can_fetch_charges_created_during_a_callback() - { - $paymentGateway = $this->getPaymentGateway(); - $paymentGateway->charge(2000, $paymentGateway->getValidTestToken()); - $paymentGateway->charge(3000, $paymentGateway->getValidTestToken()); - - $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { - $paymentGateway->charge(4000, $paymentGateway->getValidTestToken()); - $paymentGateway->charge(5000, $paymentGateway->getValidTestToken()); - }); - - $this->assertCount(2, $newCharges); - $this->assertEquals([4000, 5000], $newCharges->all()); - } - - /** @test */ - function charges_with_a_valid_payment_token_are_successful() - { - $paymentGateway = $this->getPaymentGateway(); - - $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { - $paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); - }); - - $this->assertCount(1, $newCharges); - $this->assertEquals(2500, $newCharges->sum()); - } - /** @test */ function charges_with_an_invalid_payment_token_fail() { diff --git a/tests/unit/Billing/PaymentGatewayContractTests.php b/tests/unit/Billing/PaymentGatewayContractTests.php new file mode 100644 index 0000000..1c3b4cb --- /dev/null +++ b/tests/unit/Billing/PaymentGatewayContractTests.php @@ -0,0 +1,35 @@ +getPaymentGateway(); + + $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { + $paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); + }); + + $this->assertCount(1, $newCharges); + $this->assertEquals(2500, $newCharges->sum()); + } + + /** @test */ + function can_fetch_charges_created_during_a_callback() + { + $paymentGateway = $this->getPaymentGateway(); + $paymentGateway->charge(2000, $paymentGateway->getValidTestToken()); + $paymentGateway->charge(3000, $paymentGateway->getValidTestToken()); + + $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { + $paymentGateway->charge(4000, $paymentGateway->getValidTestToken()); + $paymentGateway->charge(5000, $paymentGateway->getValidTestToken()); + }); + + $this->assertCount(2, $newCharges); + $this->assertEquals([5000, 4000], $newCharges->all()); + } +} diff --git a/tests/unit/Billing/StripePaymentGatewayTest.php b/tests/unit/Billing/StripePaymentGatewayTest.php index e17f353..829a441 100644 --- a/tests/unit/Billing/StripePaymentGatewayTest.php +++ b/tests/unit/Billing/StripePaymentGatewayTest.php @@ -8,6 +8,8 @@ use App\Billing\PaymentFailedException; */ class StripePaymentGatewayTest extends TestCase { + use PaymentGatewayContractTests; + protected function setUp() { parent::setUp(); @@ -19,19 +21,6 @@ class StripePaymentGatewayTest extends TestCase return new StripePaymentGateway(config('services.stripe.secret')); } - /** @test */ - function charges_with_a_valid_payment_token_are_successful() - { - $paymentGateway = $this->getPaymentGateway(); - - $newCharges = $paymentGateway->newChargesDuring(function ($paymentGateway) { - $paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); - }); - - $this->assertCount(1, $newCharges); - $this->assertEquals(2500, $newCharges->sum()); - } - /** @test */ function charges_with_an_invalid_payment_token_fail() {