From 2b21f07aa26853b5cedf28dbfe005d8ca8f3a9ad Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Sun, 20 Nov 2016 17:47:25 -0500 Subject: [PATCH] 6.7 - Replicating the Failure at the Unit Level --- app/Billing/FakePaymentGateway.php | 4 +++- tests/features/PurchaseTicketsTest.php | 2 ++ tests/unit/Billing/FakePaymentGatewayTest.php | 13 +++++++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/Billing/FakePaymentGateway.php b/app/Billing/FakePaymentGateway.php index 0e91109..668ef62 100644 --- a/app/Billing/FakePaymentGateway.php +++ b/app/Billing/FakePaymentGateway.php @@ -20,7 +20,9 @@ class FakePaymentGateway implements PaymentGateway public function charge($amount, $token) { if ($this->beforeFirstChargeCallback !== null) { - $this->beforeFirstChargeCallback->__invoke($this); + $callback = $this->beforeFirstChargeCallback; + $this->beforeFirstChargeCallback = null; + $callback($this); } if ($token !== $this->getValidTestToken()) { diff --git a/tests/features/PurchaseTicketsTest.php b/tests/features/PurchaseTicketsTest.php index 177d985..5ee734c 100644 --- a/tests/features/PurchaseTicketsTest.php +++ b/tests/features/PurchaseTicketsTest.php @@ -105,6 +105,8 @@ class PurchaseTicketsTest extends TestCase /** @test */ function cannot_purchase_tickets_another_customer_is_already_trying_to_purchase() { + $this->disableExceptionHandling(); + $concert = factory(Concert::class)->states('published')->create([ 'ticket_price' => 1200 ])->addTickets(3); diff --git a/tests/unit/Billing/FakePaymentGatewayTest.php b/tests/unit/Billing/FakePaymentGatewayTest.php index 192fe6f..a5caccb 100644 --- a/tests/unit/Billing/FakePaymentGatewayTest.php +++ b/tests/unit/Billing/FakePaymentGatewayTest.php @@ -35,15 +35,16 @@ class FakePaymentGatewayTest extends TestCase function running_a_hook_before_the_first_charge() { $paymentGateway = new FakePaymentGateway; - $callbackRan = false; + $timesCallbackRan = 0; - $paymentGateway->beforeFirstCharge(function ($paymentGateway) use (&$callbackRan) { - $callbackRan = true; - $this->assertEquals(0, $paymentGateway->totalCharges()); + $paymentGateway->beforeFirstCharge(function ($paymentGateway) use (&$timesCallbackRan) { + $timesCallbackRan++; + $paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); + $this->assertEquals(2500, $paymentGateway->totalCharges()); }); $paymentGateway->charge(2500, $paymentGateway->getValidTestToken()); - $this->assertTrue($callbackRan); - $this->assertEquals(2500, $paymentGateway->totalCharges()); + $this->assertEquals(1, $timesCallbackRan); + $this->assertEquals(5000, $paymentGateway->totalCharges()); } }