mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-07 01:50:07 +00:00
72 - Extracting a Finder Method
This commit is contained in:
@@ -9,7 +9,7 @@ class OrdersController extends Controller
|
|||||||
{
|
{
|
||||||
public function show($confirmationNumber)
|
public function show($confirmationNumber)
|
||||||
{
|
{
|
||||||
$order = Order::where('confirmation_number', $confirmationNumber)->first();
|
$order = Order::findByConfirmationNumber($confirmationNumber);
|
||||||
return view('orders.show', ['order' => $order]);
|
return view('orders.show', ['order' => $order]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ class Order extends Model
|
|||||||
return $order;
|
return $order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function findByConfirmationNumber($confirmationNumber)
|
||||||
|
{
|
||||||
|
return self::where('confirmation_number', $confirmationNumber)->firstOrFail();
|
||||||
|
}
|
||||||
|
|
||||||
public function concert()
|
public function concert()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Concert::class);
|
return $this->belongsTo(Concert::class);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use App\Reservation;
|
|||||||
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
|
|
||||||
class OrderTest extends TestCase
|
class OrderTest extends TestCase
|
||||||
{
|
{
|
||||||
@@ -26,6 +27,30 @@ class OrderTest extends TestCase
|
|||||||
$this->assertEquals(2, $concert->ticketsRemaining());
|
$this->assertEquals(2, $concert->ticketsRemaining());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function retrieving_an_order_by_confirmation_number()
|
||||||
|
{
|
||||||
|
$order = factory(Order::class)->create([
|
||||||
|
'confirmation_number' => 'ORDERCONFIRMATION1234',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$foundOrder = Order::findByConfirmationNumber('ORDERCONFIRMATION1234');
|
||||||
|
|
||||||
|
$this->assertEquals($order->id, $foundOrder->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function retrieving_a_nonexistent_order_by_confirmation_number_throws_an_exception()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Order::findByConfirmationNumber('NONEXISTENTCONFIRMATIONNUMBER');
|
||||||
|
} catch (ModelNotFoundException $e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail('No matching order was found for the specified confirmation number, but an exception was not thrown.');
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
function converting_to_an_array()
|
function converting_to_an_array()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user