6.1 - Uncovering a New Domain Object

This commit is contained in:
Adam Wathan
2016-11-18 18:35:08 -05:00
parent 510a60fcd3
commit 12d91dab85
3 changed files with 45 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Order;
use App\Concert;
use App\Reservation;
use Illuminate\Http\Request;
use App\Billing\PaymentGateway;
use App\Billing\PaymentFailedException;
@@ -31,12 +32,13 @@ class ConcertOrdersController extends Controller
try {
// Find some tickets
$tickets = $concert->findTickets(request('ticket_quantity'));
$reservation = new Reservation($tickets);
// Charge the customer for the tickets
$this->paymentGateway->charge($tickets->sum('price'), request('payment_token'));
$this->paymentGateway->charge($reservation->totalCost(), request('payment_token'));
// Create an order for those tickets
$order = Order::forTickets($tickets, request('email'), $tickets->sum('price'));
$order = Order::forTickets($tickets, request('email'), $reservation->totalCost());
return response()->json($order, 201);

18
app/Reservation.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace App;
class Reservation
{
private $tickets;
public function __construct($tickets)
{
$this->tickets = $tickets;
}
public function totalCost()
{
return $this->tickets->sum('price');
}
}

View File

@@ -0,0 +1,23 @@
<?php
use App\Concert;
use App\Reservation;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ReservationTest extends TestCase
{
use DatabaseMigrations;
/** @test */
function calculating_the_total_cost()
{
$concert = factory(Concert::class)->create(['ticket_price' => 1200])->addTickets(3);
$tickets = $concert->findTickets(3);
$reservation = new Reservation($tickets);
$this->assertEquals(3600, $reservation->totalCost());
}
}