mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
3.7 - Cleaning Up Our Tests
This commit is contained in:
@@ -35,6 +35,16 @@ class Concert extends Model
|
||||
return $this->hasMany(Order::class);
|
||||
}
|
||||
|
||||
public function hasOrderFor($customerEmail)
|
||||
{
|
||||
return $this->orders()->where('email', $customerEmail)->count() > 0;
|
||||
}
|
||||
|
||||
public function ordersFor($customerEmail)
|
||||
{
|
||||
return $this->orders()->where('email', $customerEmail)->get();
|
||||
}
|
||||
|
||||
public function tickets()
|
||||
{
|
||||
return $this->hasMany(Ticket::class);
|
||||
@@ -62,6 +72,8 @@ class Concert extends Model
|
||||
foreach (range(1, $quantity) as $i) {
|
||||
$this->tickets()->create([]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function ticketsRemaining()
|
||||
|
||||
@@ -13,6 +13,11 @@ class Order extends Model
|
||||
return $this->hasMany(Ticket::class);
|
||||
}
|
||||
|
||||
public function ticketQuantity()
|
||||
{
|
||||
return $this->tickets()->count();
|
||||
}
|
||||
|
||||
public function cancel()
|
||||
{
|
||||
foreach ($this->tickets as $ticket) {
|
||||
|
||||
@@ -32,8 +32,7 @@ class PurchaseTicketsTest extends TestCase
|
||||
/** @test */
|
||||
function customer_can_purchase_tickets_to_a_published_concert()
|
||||
{
|
||||
$concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250]);
|
||||
$concert->addTickets(3);
|
||||
$concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250])->addTickets(3);
|
||||
|
||||
$this->orderTickets($concert, [
|
||||
'email' => 'john@example.com',
|
||||
@@ -43,16 +42,14 @@ class PurchaseTicketsTest extends TestCase
|
||||
|
||||
$this->assertResponseStatus(201);
|
||||
$this->assertEquals(9750, $this->paymentGateway->totalCharges());
|
||||
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
||||
$this->assertNotNull($order);
|
||||
$this->assertEquals(3, $order->tickets()->count());
|
||||
$this->assertTrue($concert->hasOrderFor('john@example.com'));
|
||||
$this->assertEquals(3, $concert->ordersFor('john@example.com')->first()->ticketQuantity());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function cannot_purchase_tickets_to_an_unpublished_concert()
|
||||
{
|
||||
$concert = factory(Concert::class)->states('unpublished')->create();
|
||||
$concert->addTickets(3);
|
||||
$concert = factory(Concert::class)->states('unpublished')->create()->addTickets(3);
|
||||
|
||||
$this->orderTickets($concert, [
|
||||
'email' => 'john@example.com',
|
||||
@@ -61,15 +58,14 @@ class PurchaseTicketsTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertResponseStatus(404);
|
||||
$this->assertEquals(0, $concert->orders()->count());
|
||||
$this->assertFalse($concert->hasOrderFor('john@example.com'));
|
||||
$this->assertEquals(0, $this->paymentGateway->totalCharges());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function an_order_is_not_created_if_payment_fails()
|
||||
{
|
||||
$concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250]);
|
||||
$concert->addTickets(3);
|
||||
$concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250])->addTickets(3);
|
||||
|
||||
$this->orderTickets($concert, [
|
||||
'email' => 'john@example.com',
|
||||
@@ -78,17 +74,13 @@ class PurchaseTicketsTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertResponseStatus(422);
|
||||
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
||||
$this->assertNull($order);
|
||||
$this->assertFalse($concert->hasOrderFor('john@example.com'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function cannot_purchase_more_tickets_than_remain()
|
||||
{
|
||||
$this->disableExceptionHandling();
|
||||
|
||||
$concert = factory(Concert::class)->states('published')->create();
|
||||
$concert->addTickets(50);
|
||||
$concert = factory(Concert::class)->states('published')->create()->addTickets(50);
|
||||
|
||||
$this->orderTickets($concert, [
|
||||
'email' => 'john@example.com',
|
||||
@@ -97,8 +89,7 @@ class PurchaseTicketsTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertResponseStatus(422);
|
||||
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
||||
$this->assertNull($order);
|
||||
$this->assertFalse($concert->hasOrderFor('john@example.com'));
|
||||
$this->assertEquals(0, $this->paymentGateway->totalCharges());
|
||||
$this->assertEquals(50, $concert->ticketsRemaining());
|
||||
}
|
||||
|
||||
@@ -58,13 +58,12 @@ class ConcertTest extends TestCase
|
||||
/** @test */
|
||||
function can_order_concert_tickets()
|
||||
{
|
||||
$concert = factory(Concert::class)->create();
|
||||
$concert->addTickets(3);
|
||||
$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->tickets()->count());
|
||||
$this->assertEquals(3, $order->ticketQuantity());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -80,8 +79,7 @@ class ConcertTest extends TestCase
|
||||
/** @test */
|
||||
function tickets_remaining_does_not_include_tickets_associated_with_an_order()
|
||||
{
|
||||
$concert = factory(Concert::class)->create();
|
||||
$concert->addTickets(50);
|
||||
$concert = factory(Concert::class)->create()->addTickets(50);
|
||||
$concert->orderTickets('jane@example.com', 30);
|
||||
|
||||
$this->assertEquals(20, $concert->ticketsRemaining());
|
||||
@@ -90,14 +88,12 @@ class ConcertTest extends TestCase
|
||||
/** @test */
|
||||
function trying_to_purchase_more_tickets_than_remain_throws_an_exception()
|
||||
{
|
||||
$concert = factory(Concert::class)->create();
|
||||
$concert->addTickets(10);
|
||||
$concert = factory(Concert::class)->create()->addTickets(10);
|
||||
|
||||
try {
|
||||
$concert->orderTickets('jane@example.com', 11);
|
||||
} catch (NotEnoughTicketsException $e) {
|
||||
$order = $concert->orders()->where('email', 'jane@example.com')->first();
|
||||
$this->assertNull($order);
|
||||
$this->assertFalse($concert->hasOrderFor('jane@example.com'));
|
||||
$this->assertEquals(10, $concert->ticketsRemaining());
|
||||
return;
|
||||
}
|
||||
@@ -108,15 +104,13 @@ class ConcertTest extends TestCase
|
||||
/** @test */
|
||||
function cannot_order_tickets_that_have_already_been_purchased()
|
||||
{
|
||||
$concert = factory(Concert::class)->create();
|
||||
$concert->addTickets(10);
|
||||
$concert = factory(Concert::class)->create()->addTickets(10);
|
||||
$concert->orderTickets('jane@example.com', 8);
|
||||
|
||||
try {
|
||||
$concert->orderTickets('john@example.com', 3);
|
||||
} catch (NotEnoughTicketsException $e) {
|
||||
$johnsOrder = $concert->orders()->where('email', 'john@example.com')->first();
|
||||
$this->assertNull($johnsOrder);
|
||||
$this->assertFalse($concert->hasOrderFor('john@example.com'));
|
||||
$this->assertEquals(2, $concert->ticketsRemaining());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ class OrderTest extends TestCase
|
||||
/** @test */
|
||||
function tickets_are_released_when_an_order_is_cancelled()
|
||||
{
|
||||
$concert = factory(Concert::class)->create();
|
||||
$concert->addTickets(10);
|
||||
$concert = factory(Concert::class)->create()->addTickets(10);
|
||||
$order = $concert->orderTickets('jane@example.com', 5);
|
||||
$this->assertEquals(5, $concert->ticketsRemaining());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user