mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
1.7 - Testing Query Scopes
This commit is contained in:
@@ -9,6 +9,11 @@ class Concert extends Model
|
||||
protected $guarded = [];
|
||||
protected $dates = ['date'];
|
||||
|
||||
public function scopePublished($query)
|
||||
{
|
||||
return $query->whereNotNull('published_at');
|
||||
}
|
||||
|
||||
public function getFormattedDateAttribute()
|
||||
{
|
||||
return $this->date->format('F j, Y');
|
||||
|
||||
@@ -9,7 +9,7 @@ class ConcertsController extends Controller
|
||||
{
|
||||
public function show($id)
|
||||
{
|
||||
$concert = Concert::whereNotNull('published_at')->findOrFail($id);
|
||||
$concert = Concert::published()->findOrFail($id);
|
||||
return view('concerts.show', ['concert' => $concert]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ class ViewConcertListingTest extends TestCase
|
||||
use DatabaseMigrations;
|
||||
|
||||
/** @test */
|
||||
function user_can_view_a_concert_listing()
|
||||
function user_can_view_a_published_concert_listing()
|
||||
{
|
||||
$concert = Concert::create([
|
||||
'title' => 'The Red Chord',
|
||||
@@ -24,6 +24,7 @@ class ViewConcertListingTest extends TestCase
|
||||
'state' => 'ON',
|
||||
'zip' => '17916',
|
||||
'additional_information' => 'For tickets, call (555) 555-5555.',
|
||||
'published_at' => Carbon::parse('-1 week'),
|
||||
]);
|
||||
|
||||
$this->visit('/concerts/'.$concert->id);
|
||||
|
||||
@@ -8,6 +8,8 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class ConcertTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
/** @test */
|
||||
function can_get_formatted_date()
|
||||
{
|
||||
@@ -37,4 +39,18 @@ class ConcertTest extends TestCase
|
||||
|
||||
$this->assertEquals('67.50', $concert->ticket_price_in_dollars);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function concerts_with_a_published_at_date_are_published()
|
||||
{
|
||||
$publishedConcertA = factory(Concert::class)->create(['published_at' => Carbon::parse('-1 week')]);
|
||||
$publishedConcertB = factory(Concert::class)->create(['published_at' => Carbon::parse('-1 week')]);
|
||||
$unpublishedConcert = factory(Concert::class)->create(['published_at' => null]);
|
||||
|
||||
$publishedConcerts = Concert::published()->get();
|
||||
|
||||
$this->assertTrue($publishedConcerts->contains($publishedConcertA));
|
||||
$this->assertTrue($publishedConcerts->contains($publishedConcertB));
|
||||
$this->assertFalse($publishedConcerts->contains($unpublishedConcert));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user