From d3b19b31abe9cafa224f3a6b04b66473e1837503 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Thu, 15 Dec 2016 14:29:25 -0500 Subject: [PATCH] 50 - Refactor "Long Parameter List" Using "Preserve Whole Object" --- app/Order.php | 12 ++++++++++++ tests/unit/OrderTest.php | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/Order.php b/app/Order.php index d1983ec..f8b18d1 100644 --- a/app/Order.php +++ b/app/Order.php @@ -22,6 +22,18 @@ class Order extends Model return $order; } + public static function fromReservation($reservation) + { + $order = self::create([ + 'email' => $reservation->email(), + 'amount' => $reservation->totalCost(), + ]); + + $order->tickets()->saveMany($reservation->tickets()); + + return $order; + } + public function concert() { return $this->belongsTo(Concert::class); diff --git a/tests/unit/OrderTest.php b/tests/unit/OrderTest.php index 765686e..eb94a41 100644 --- a/tests/unit/OrderTest.php +++ b/tests/unit/OrderTest.php @@ -1,7 +1,9 @@ assertEquals(2, $concert->ticketsRemaining()); } + /** @test */ + function creating_an_order_from_a_reservation() + { + $concert = factory(Concert::class)->create(['ticket_price' => 1200]); + $tickets = factory(Ticket::class, 3)->create(['concert_id' => $concert->id]); + $reservation = new Reservation($tickets, 'john@example.com'); + + $order = Order::fromReservation($reservation); + + $this->assertEquals('john@example.com', $order->email); + $this->assertEquals(3, $order->ticketQuantity()); + $this->assertEquals(3600, $order->amount); + } + /** @test */ function converting_to_an_array() {