From e0bfaceb20d23bf1e37e978d9ecbbac62c12601e Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 17 Mar 2017 13:11:19 -0400 Subject: [PATCH] 86 - Deleting More Stale Code --- app/Concert.php | 11 ----- database/factories/ModelFactory.php | 2 + .../2016_11_08_205823_create_orders_table.php | 2 +- tests/unit/ConcertTest.php | 43 ++++--------------- 4 files changed, 12 insertions(+), 46 deletions(-) diff --git a/app/Concert.php b/app/Concert.php index c7761cf..6339f40 100644 --- a/app/Concert.php +++ b/app/Concert.php @@ -50,12 +50,6 @@ class Concert extends Model return $this->hasMany(Ticket::class); } - public function orderTickets($email, $ticketQuantity) - { - $tickets = $this->findTickets($ticketQuantity); - return $this->createOrder($email, $tickets); - } - public function reserveTickets($quantity, $email) { $tickets = $this->findTickets($quantity)->each(function ($ticket) { @@ -76,11 +70,6 @@ class Concert extends Model return $tickets; } - public function createOrder($email, $tickets) - { - return Order::forTickets($tickets, $email, $tickets->sum('price')); - } - public function addTickets($quantity) { foreach (range(1, $quantity) as $i) { diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index b53309a..049a1ae 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -71,5 +71,7 @@ $factory->define(App\Order::class, function (Faker\Generator $faker) { return [ 'amount' => 5250, 'email' => 'somebody@example.com', + 'confirmation_number' => 'ORDERCONFIRMATION1234', + 'card_last_four' => '1234', ]; }); diff --git a/database/migrations/2016_11_08_205823_create_orders_table.php b/database/migrations/2016_11_08_205823_create_orders_table.php index d51b725..36ca999 100644 --- a/database/migrations/2016_11_08_205823_create_orders_table.php +++ b/database/migrations/2016_11_08_205823_create_orders_table.php @@ -18,7 +18,7 @@ class CreateOrdersTable extends Migration $table->string('confirmation_number'); $table->integer('amount'); $table->string('email'); - $table->string('card_last_four')->nullable(); + $table->string('card_last_four'); $table->timestamps(); }); } diff --git a/tests/unit/ConcertTest.php b/tests/unit/ConcertTest.php index 4ce3f46..06353cd 100644 --- a/tests/unit/ConcertTest.php +++ b/tests/unit/ConcertTest.php @@ -1,5 +1,7 @@ assertFalse($publishedConcerts->contains($unpublishedConcert)); } - /** @test */ - function can_order_concert_tickets() - { - $concert = factory(Concert::class)->create()->addTickets(3); - - $order = $concert->orderTickets('jane@example.com', 3); - - $this->assertEquals('jane@example.com', $order->email); - $this->assertEquals(3, $order->ticketQuantity()); - } - /** @test */ function can_add_tickets() { @@ -79,21 +70,21 @@ class ConcertTest extends TestCase /** @test */ function tickets_remaining_does_not_include_tickets_associated_with_an_order() { - $concert = factory(Concert::class)->create()->addTickets(50); - $concert->orderTickets('jane@example.com', 30); + $concert = factory(Concert::class)->create(); + $concert->tickets()->saveMany(factory(Ticket::class, 30)->create(['order_id' => 1])); + $concert->tickets()->saveMany(factory(Ticket::class, 20)->create(['order_id' => null])); $this->assertEquals(20, $concert->ticketsRemaining()); } /** @test */ - function trying_to_purchase_more_tickets_than_remain_throws_an_exception() + function trying_to_reserve_more_tickets_than_remain_throws_an_exception() { $concert = factory(Concert::class)->create()->addTickets(10); try { - $concert->orderTickets('jane@example.com', 11); + $reservation = $concert->reserveTickets(11, 'john@example.com'); } catch (NotEnoughTicketsException $e) { - $this->assertFalse($concert->hasOrderFor('jane@example.com')); $this->assertEquals(10, $concert->ticketsRemaining()); return; } @@ -101,23 +92,6 @@ class ConcertTest extends TestCase $this->fail("Order succeeded even though there were not enough tickets remaining."); } - /** @test */ - function cannot_order_tickets_that_have_already_been_purchased() - { - $concert = factory(Concert::class)->create()->addTickets(10); - $concert->orderTickets('jane@example.com', 8); - - try { - $concert->orderTickets('john@example.com', 3); - } catch (NotEnoughTicketsException $e) { - $this->assertFalse($concert->hasOrderFor('john@example.com')); - $this->assertEquals(2, $concert->ticketsRemaining()); - return; - } - - $this->fail("Order succeeded even though there were not enough tickets remaining."); - } - /** @test */ function can_reserve_available_tickets() { @@ -135,7 +109,8 @@ class ConcertTest extends TestCase function cannot_reserve_tickets_that_have_already_been_purchased() { $concert = factory(Concert::class)->create()->addTickets(3); - $concert->orderTickets('jane@example.com', 2); + $order = factory(Order::class)->create(); + $order->tickets()->saveMany($concert->tickets->take(2)); try { $concert->reserveTickets(2, 'john@example.com');