mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-10 16:51:31 +00:00
86 - Deleting More Stale Code
This commit is contained in:
@@ -50,12 +50,6 @@ class Concert extends Model
|
|||||||
return $this->hasMany(Ticket::class);
|
return $this->hasMany(Ticket::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function orderTickets($email, $ticketQuantity)
|
|
||||||
{
|
|
||||||
$tickets = $this->findTickets($ticketQuantity);
|
|
||||||
return $this->createOrder($email, $tickets);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function reserveTickets($quantity, $email)
|
public function reserveTickets($quantity, $email)
|
||||||
{
|
{
|
||||||
$tickets = $this->findTickets($quantity)->each(function ($ticket) {
|
$tickets = $this->findTickets($quantity)->each(function ($ticket) {
|
||||||
@@ -76,11 +70,6 @@ class Concert extends Model
|
|||||||
return $tickets;
|
return $tickets;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createOrder($email, $tickets)
|
|
||||||
{
|
|
||||||
return Order::forTickets($tickets, $email, $tickets->sum('price'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addTickets($quantity)
|
public function addTickets($quantity)
|
||||||
{
|
{
|
||||||
foreach (range(1, $quantity) as $i) {
|
foreach (range(1, $quantity) as $i) {
|
||||||
|
|||||||
@@ -71,5 +71,7 @@ $factory->define(App\Order::class, function (Faker\Generator $faker) {
|
|||||||
return [
|
return [
|
||||||
'amount' => 5250,
|
'amount' => 5250,
|
||||||
'email' => 'somebody@example.com',
|
'email' => 'somebody@example.com',
|
||||||
|
'confirmation_number' => 'ORDERCONFIRMATION1234',
|
||||||
|
'card_last_four' => '1234',
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class CreateOrdersTable extends Migration
|
|||||||
$table->string('confirmation_number');
|
$table->string('confirmation_number');
|
||||||
$table->integer('amount');
|
$table->integer('amount');
|
||||||
$table->string('email');
|
$table->string('email');
|
||||||
$table->string('card_last_four')->nullable();
|
$table->string('card_last_four');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Order;
|
||||||
|
use App\Ticket;
|
||||||
use App\Concert;
|
use App\Concert;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use App\Exceptions\NotEnoughTicketsException;
|
use App\Exceptions\NotEnoughTicketsException;
|
||||||
@@ -55,17 +57,6 @@ class ConcertTest extends TestCase
|
|||||||
$this->assertFalse($publishedConcerts->contains($unpublishedConcert));
|
$this->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 */
|
/** @test */
|
||||||
function can_add_tickets()
|
function can_add_tickets()
|
||||||
{
|
{
|
||||||
@@ -79,21 +70,21 @@ class ConcertTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
function tickets_remaining_does_not_include_tickets_associated_with_an_order()
|
function tickets_remaining_does_not_include_tickets_associated_with_an_order()
|
||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create()->addTickets(50);
|
$concert = factory(Concert::class)->create();
|
||||||
$concert->orderTickets('jane@example.com', 30);
|
$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());
|
$this->assertEquals(20, $concert->ticketsRemaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @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);
|
$concert = factory(Concert::class)->create()->addTickets(10);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$concert->orderTickets('jane@example.com', 11);
|
$reservation = $concert->reserveTickets(11, 'john@example.com');
|
||||||
} catch (NotEnoughTicketsException $e) {
|
} catch (NotEnoughTicketsException $e) {
|
||||||
$this->assertFalse($concert->hasOrderFor('jane@example.com'));
|
|
||||||
$this->assertEquals(10, $concert->ticketsRemaining());
|
$this->assertEquals(10, $concert->ticketsRemaining());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -101,23 +92,6 @@ class ConcertTest extends TestCase
|
|||||||
$this->fail("Order succeeded even though there were not enough tickets remaining.");
|
$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 */
|
/** @test */
|
||||||
function can_reserve_available_tickets()
|
function can_reserve_available_tickets()
|
||||||
{
|
{
|
||||||
@@ -135,7 +109,8 @@ class ConcertTest extends TestCase
|
|||||||
function cannot_reserve_tickets_that_have_already_been_purchased()
|
function cannot_reserve_tickets_that_have_already_been_purchased()
|
||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create()->addTickets(3);
|
$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 {
|
try {
|
||||||
$concert->reserveTickets(2, 'john@example.com');
|
$concert->reserveTickets(2, 'john@example.com');
|
||||||
|
|||||||
Reference in New Issue
Block a user