-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3.3 - Refusing Orders When There Are No More Tickets
- Loading branch information
1 parent
53a6977
commit 2ccc635
Showing
4 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
class NotEnoughTicketsException extends \RuntimeException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
|
||
|
@@ -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."); | ||
} | ||
} |