mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
50 - Refactor "Long Parameter List" Using "Preserve Whole Object"
This commit is contained in:
@@ -22,6 +22,18 @@ class Order extends Model
|
||||
return $order;
|
||||
}
|
||||
|
||||
public static function fromReservation($reservation)
|
||||
{
|
||||
$order = self::create([
|
||||
'email' => $reservation->email(),
|
||||
'amount' => $reservation->totalCost(),
|
||||
]);
|
||||
|
||||
$order->tickets()->saveMany($reservation->tickets());
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
public function concert()
|
||||
{
|
||||
return $this->belongsTo(Concert::class);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
<?php
|
||||
|
||||
use App\Order;
|
||||
use App\Ticket;
|
||||
use App\Concert;
|
||||
use App\Reservation;
|
||||
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
@@ -24,6 +26,20 @@ class OrderTest extends TestCase
|
||||
$this->assertEquals(2, $concert->ticketsRemaining());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function creating_an_order_from_a_reservation()
|
||||
{
|
||||
$concert = factory(Concert::class)->create(['ticket_price' => 1200]);
|
||||
$tickets = factory(Ticket::class, 3)->create(['concert_id' => $concert->id]);
|
||||
$reservation = new Reservation($tickets, 'john@example.com');
|
||||
|
||||
$order = Order::fromReservation($reservation);
|
||||
|
||||
$this->assertEquals('john@example.com', $order->email);
|
||||
$this->assertEquals(3, $order->ticketQuantity());
|
||||
$this->assertEquals(3600, $order->amount);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function converting_to_an_array()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user