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