mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-21 12:55:34 +00:00
121 - Custom Factory Classes
This commit is contained in:
13
database/ConcertFactory.php
Normal file
13
database/ConcertFactory.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Concert;
|
||||||
|
|
||||||
|
class ConcertFactory
|
||||||
|
{
|
||||||
|
public static function createPublished($overrides = [])
|
||||||
|
{
|
||||||
|
$concert = factory(Concert::class)->create($overrides);
|
||||||
|
$concert->publish();
|
||||||
|
return $concert;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,10 +18,11 @@ class DatabaseSeeder extends Seeder
|
|||||||
'password' => bcrypt('secret'),
|
'password' => bcrypt('secret'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
factory(App\Concert::class)->states('published')->create([
|
\ConcertFactory::createPublished([
|
||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
'title' => "The Red Chord",
|
'title' => "The Red Chord",
|
||||||
'subtitle' => "with Animosity and Lethargy",
|
'subtitle' => "with Animosity and Lethargy",
|
||||||
|
'additional_information' => "This concert is 19+.",
|
||||||
'venue' => "The Mosh Pit",
|
'venue' => "The Mosh Pit",
|
||||||
'venue_address' => "123 Example Lane",
|
'venue_address' => "123 Example Lane",
|
||||||
'city' => "Laraville",
|
'city' => "Laraville",
|
||||||
@@ -29,13 +30,14 @@ class DatabaseSeeder extends Seeder
|
|||||||
'zip' => "17916",
|
'zip' => "17916",
|
||||||
'date' => Carbon::parse('2017-09-13 8:00pm'),
|
'date' => Carbon::parse('2017-09-13 8:00pm'),
|
||||||
'ticket_price' => 3250,
|
'ticket_price' => 3250,
|
||||||
'additional_information' => "This concert is 19+.",
|
'ticket_quantity' => 10,
|
||||||
])->addTickets(10);
|
]);
|
||||||
|
|
||||||
factory(App\Concert::class)->create([
|
factory(App\Concert::class)->create([
|
||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
'title' => "Slayer",
|
'title' => "Slayer",
|
||||||
'subtitle' => "with Forbidden and Testament",
|
'subtitle' => "with Forbidden and Testament",
|
||||||
|
'additional_information' => null,
|
||||||
'venue' => "The Rock Pile",
|
'venue' => "The Rock Pile",
|
||||||
'venue_address' => "55 Sample Blvd",
|
'venue_address' => "55 Sample Blvd",
|
||||||
'city' => "Laraville",
|
'city' => "Laraville",
|
||||||
@@ -43,7 +45,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
'zip' => "19276",
|
'zip' => "19276",
|
||||||
'date' => Carbon::parse('2017-10-05 7:00pm'),
|
'date' => Carbon::parse('2017-10-05 7:00pm'),
|
||||||
'ticket_price' => 5500,
|
'ticket_price' => 5500,
|
||||||
'additional_information' => null,
|
'ticket_quantity' => 10,
|
||||||
])->addTickets(10);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
OrderConfirmationNumber::shouldReceive('generate')->andReturn('ORDERCONFIRMATION1234');
|
OrderConfirmationNumber::shouldReceive('generate')->andReturn('ORDERCONFIRMATION1234');
|
||||||
TicketCode::shouldReceive('generateFor')->andReturn('TICKETCODE1', 'TICKETCODE2', 'TICKETCODE3');
|
TicketCode::shouldReceive('generateFor')->andReturn('TICKETCODE1', 'TICKETCODE2', 'TICKETCODE3');
|
||||||
|
|
||||||
$concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250])->addTickets(3);
|
$concert = \ConcertFactory::createPublished(['ticket_price' => 3250, 'ticket_quantity' => 3]);
|
||||||
|
|
||||||
$this->orderTickets($concert, [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
@@ -96,7 +96,7 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
/** @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()->addTickets(3);
|
$concert = factory(Concert::class)->states('unpublished')->create(['ticket_quantity' => 3]);
|
||||||
|
|
||||||
$this->orderTickets($concert, [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
@@ -112,7 +112,7 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
/** @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])->addTickets(3);
|
$concert = \ConcertFactory::createPublished(['ticket_price' => 3250, 'ticket_quantity' => 3]);
|
||||||
|
|
||||||
$this->orderTickets($concert, [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
@@ -128,7 +128,7 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
function cannot_purchase_more_tickets_than_remain()
|
function cannot_purchase_more_tickets_than_remain()
|
||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->states('published')->create()->addTickets(50);
|
$concert = \ConcertFactory::createPublished(['ticket_price' => 3250, 'ticket_quantity' => 50]);
|
||||||
|
|
||||||
$this->orderTickets($concert, [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
@@ -147,9 +147,7 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->disableExceptionHandling();
|
$this->disableExceptionHandling();
|
||||||
|
|
||||||
$concert = factory(Concert::class)->states('published')->create([
|
$concert = \ConcertFactory::createPublished(['ticket_price' => 1200, 'ticket_quantity' => 3]);
|
||||||
'ticket_price' => 1200
|
|
||||||
])->addTickets(3);
|
|
||||||
|
|
||||||
$this->paymentGateway->beforeFirstCharge(function ($paymentGateway) use ($concert) {
|
$this->paymentGateway->beforeFirstCharge(function ($paymentGateway) use ($concert) {
|
||||||
$this->orderTickets($concert, [
|
$this->orderTickets($concert, [
|
||||||
|
|||||||
@@ -74,16 +74,6 @@ class ConcertTest extends TestCase
|
|||||||
$this->assertEquals(5, $concert->ticketsRemaining());
|
$this->assertEquals(5, $concert->ticketsRemaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
|
||||||
function can_add_tickets()
|
|
||||||
{
|
|
||||||
$concert = factory(Concert::class)->create();
|
|
||||||
|
|
||||||
$concert->addTickets(50);
|
|
||||||
|
|
||||||
$this->assertEquals(50, $concert->ticketsRemaining());
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
function tickets_remaining_does_not_include_tickets_associated_with_an_order()
|
function tickets_remaining_does_not_include_tickets_associated_with_an_order()
|
||||||
{
|
{
|
||||||
@@ -97,7 +87,7 @@ class ConcertTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
function trying_to_reserve_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 = \ConcertFactory::createPublished(['ticket_quantity' => 10]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$reservation = $concert->reserveTickets(11, 'john@example.com');
|
$reservation = $concert->reserveTickets(11, 'john@example.com');
|
||||||
@@ -112,7 +102,7 @@ class ConcertTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
function can_reserve_available_tickets()
|
function can_reserve_available_tickets()
|
||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create()->addTickets(3);
|
$concert = \ConcertFactory::createPublished(['ticket_quantity' => 3]);
|
||||||
$this->assertEquals(3, $concert->ticketsRemaining());
|
$this->assertEquals(3, $concert->ticketsRemaining());
|
||||||
|
|
||||||
$reservation = $concert->reserveTickets(2, 'john@example.com');
|
$reservation = $concert->reserveTickets(2, 'john@example.com');
|
||||||
@@ -125,7 +115,7 @@ class ConcertTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
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 = \ConcertFactory::createPublished(['ticket_quantity' => 3]);
|
||||||
$order = factory(Order::class)->create();
|
$order = factory(Order::class)->create();
|
||||||
$order->tickets()->saveMany($concert->tickets->take(2));
|
$order->tickets()->saveMany($concert->tickets->take(2));
|
||||||
|
|
||||||
@@ -142,7 +132,7 @@ class ConcertTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
function cannot_reserve_tickets_that_have_already_been_reserved()
|
function cannot_reserve_tickets_that_have_already_been_reserved()
|
||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create()->addTickets(3);
|
$concert = \ConcertFactory::createPublished(['ticket_quantity' => 3]);
|
||||||
$concert->reserveTickets(2, 'jane@example.com');
|
$concert->reserveTickets(2, 'jane@example.com');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user