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