Skip to content

Commit

Permalink
3.3 - Refusing Orders When There Are No More Tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Nov 14, 2016
1 parent 53a6977 commit 2ccc635
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
10 changes: 8 additions & 2 deletions app/Concert.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Exceptions\NotEnoughTicketsException;

class Concert extends Model
{
Expand Down Expand Up @@ -41,8 +42,13 @@ public function tickets()

public function orderTickets($email, $ticketQuantity)
{
$tickets = $this->tickets()->available()->take($ticketQuantity)->get();

if ($tickets->count() < $ticketQuantity) {
throw new NotEnoughTicketsException;
}

$order = $this->orders()->create(['email' => $email]);
$tickets = $this->tickets()->take($ticketQuantity)->get();

foreach ($tickets as $ticket) {
$order->tickets()->save($ticket);
Expand All @@ -60,6 +66,6 @@ public function addTickets($quantity)

public function ticketsRemaining()
{
return $this->tickets()->whereNull('order_id')->count();
return $this->tickets()->available()->count();
}
}
5 changes: 5 additions & 0 deletions app/Exceptions/NotEnoughTicketsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace App\Exceptions;

class NotEnoughTicketsException extends \RuntimeException {}
5 changes: 4 additions & 1 deletion app/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@

class Ticket extends Model
{
//
public function scopeAvailable($query)
{
return $query->whereNull('order_id');
}
}
39 changes: 39 additions & 0 deletions tests/unit/ConcertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Concert;
use Carbon\Carbon;
use App\Exceptions\NotEnoughTicketsException;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
Expand Down Expand Up @@ -58,6 +59,7 @@ function concerts_with_a_published_at_date_are_published()
function can_order_concert_tickets()
{
$concert = factory(Concert::class)->create();
$concert->addTickets(3);

$order = $concert->orderTickets('[email protected]', 3);

Expand All @@ -84,4 +86,41 @@ function tickets_remaining_does_not_include_tickets_associated_with_an_order()

$this->assertEquals(20, $concert->ticketsRemaining());
}

/** @test */
function trying_to_purchase_more_tickets_than_remain_throws_an_exception()
{
$concert = factory(Concert::class)->create();
$concert->addTickets(10);

try {
$concert->orderTickets('[email protected]', 11);
} catch (NotEnoughTicketsException $e) {
$order = $concert->orders()->where('email', '[email protected]')->first();
$this->assertNull($order);
$this->assertEquals(10, $concert->ticketsRemaining());
return;
}

$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();
$concert->addTickets(10);
$concert->orderTickets('[email protected]', 8);

try {
$concert->orderTickets('[email protected]', 3);
} catch (NotEnoughTicketsException $e) {
$johnsOrder = $concert->orders()->where('email', '[email protected]')->first();
$this->assertNull($johnsOrder);
$this->assertEquals(2, $concert->ticketsRemaining());
return;
}

$this->fail("Order succeeded even though there were not enough tickets remaining.");
}
}

0 comments on commit 2ccc635

Please sign in to comment.