diff --git a/app/Concert.php b/app/Concert.php index 7f22bc3..c7761cf 100644 --- a/app/Concert.php +++ b/app/Concert.php @@ -56,13 +56,13 @@ class Concert extends Model return $this->createOrder($email, $tickets); } - public function reserveTickets($quantity) + public function reserveTickets($quantity, $email) { $tickets = $this->findTickets($quantity)->each(function ($ticket) { $ticket->reserve(); }); - return new Reservation($tickets); + return new Reservation($tickets, $email); } public function findTickets($quantity) diff --git a/app/Http/Controllers/ConcertOrdersController.php b/app/Http/Controllers/ConcertOrdersController.php index 6c11304..4bea797 100644 --- a/app/Http/Controllers/ConcertOrdersController.php +++ b/app/Http/Controllers/ConcertOrdersController.php @@ -31,13 +31,13 @@ class ConcertOrdersController extends Controller try { // Find some tickets - $reservation = $concert->reserveTickets(request('ticket_quantity')); + $reservation = $concert->reserveTickets(request('ticket_quantity'), request('email')); // Charge the customer for the tickets $this->paymentGateway->charge($reservation->totalCost(), request('payment_token')); // Create an order for those tickets - $order = Order::forTickets($reservation->tickets(), request('email'), $reservation->totalCost()); + $order = Order::forTickets($reservation->tickets(), $reservation->email(), $reservation->totalCost()); return response()->json($order, 201); diff --git a/app/Reservation.php b/app/Reservation.php index 49c1312..c6f69b6 100644 --- a/app/Reservation.php +++ b/app/Reservation.php @@ -5,10 +5,12 @@ namespace App; class Reservation { private $tickets; + private $email; - public function __construct($tickets) + public function __construct($tickets, $email) { $this->tickets = $tickets; + $this->email = $email; } public function totalCost() @@ -21,6 +23,11 @@ class Reservation return $this->tickets; } + public function email() + { + return $this->email; + } + public function cancel() { foreach ($this->tickets as $ticket) { diff --git a/tests/unit/ConcertTest.php b/tests/unit/ConcertTest.php index 3c4d575..4ce3f46 100644 --- a/tests/unit/ConcertTest.php +++ b/tests/unit/ConcertTest.php @@ -124,9 +124,10 @@ class ConcertTest extends TestCase $concert = factory(Concert::class)->create()->addTickets(3); $this->assertEquals(3, $concert->ticketsRemaining()); - $reservation = $concert->reserveTickets(2); + $reservation = $concert->reserveTickets(2, 'john@example.com'); $this->assertCount(2, $reservation->tickets()); + $this->assertEquals('john@example.com', $reservation->email()); $this->assertEquals(1, $concert->ticketsRemaining()); } @@ -137,7 +138,7 @@ class ConcertTest extends TestCase $concert->orderTickets('jane@example.com', 2); try { - $concert->reserveTickets(2); + $concert->reserveTickets(2, 'john@example.com'); } catch (NotEnoughTicketsException $e) { $this->assertEquals(1, $concert->ticketsRemaining()); return; @@ -150,10 +151,10 @@ class ConcertTest extends TestCase function cannot_reserve_tickets_that_have_already_been_reserved() { $concert = factory(Concert::class)->create()->addTickets(3); - $concert->reserveTickets(2); + $concert->reserveTickets(2, 'jane@example.com'); try { - $concert->reserveTickets(2); + $concert->reserveTickets(2, 'john@example.com'); } catch (NotEnoughTicketsException $e) { $this->assertEquals(1, $concert->ticketsRemaining()); return; diff --git a/tests/unit/ReservationTest.php b/tests/unit/ReservationTest.php index eba517c..4f0aea2 100644 --- a/tests/unit/ReservationTest.php +++ b/tests/unit/ReservationTest.php @@ -18,7 +18,7 @@ class ReservationTest extends TestCase (object) ['price' => 1200], ]); - $reservation = new Reservation($tickets); + $reservation = new Reservation($tickets, 'john@example.com'); $this->assertEquals(3600, $reservation->totalCost()); } @@ -32,11 +32,19 @@ class ReservationTest extends TestCase (object) ['price' => 1200], ]); - $reservation = new Reservation($tickets); + $reservation = new Reservation($tickets, 'john@example.com'); $this->assertEquals($tickets, $reservation->tickets()); } + /** @test */ + function retrieving_the_customers_email() + { + $reservation = new Reservation(collect(), 'john@example.com'); + + $this->assertEquals('john@example.com', $reservation->email()); + } + /** @test */ function reserved_tickets_are_released_when_a_reservation_is_cancelled() { @@ -46,7 +54,7 @@ class ReservationTest extends TestCase Mockery::spy(Ticket::class), ]); - $reservation = new Reservation($tickets); + $reservation = new Reservation($tickets, 'john@example.com'); $reservation->cancel();