Skip to content

Instantly share code, notes, and snippets.

@rachids
Created December 8, 2021 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachids/ab79bcd6d22225763d3f22368d4ad2bc to your computer and use it in GitHub Desktop.
Save rachids/ab79bcd6d22225763d3f22368d4ad2bc to your computer and use it in GitHub Desktop.
Bunny TestCase
<?php
namespace Tests\Feature;
use App\Models\Bunny;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class BunnyTest extends TestCase
{
use RefreshDatabase;
public function test_it_can_scope_older_rabbit()
{
// Given 10 random bunnies
Bunny::factory()->count(10)->create();
// And an old bunny
$wrinkleTheGranpa = Bunny::factory()->create([
'name' => 'Wrinkle',
'birth_date' => now()->subYears(7),
]);
// When fetching the bunnies using the "old" scope
$oldBunnies = Bunny::old()->get();
// Then Wrinkle should be in the results, coz he's old.
$this->assertTrue($oldBunnies->contains($wrinkleTheGranpa));
}
public function test_it_can_scope_old_and_fluffy()
{
// Given 10 random bunnies
Bunny::factory()->count(10)->create();
// And one fluffy old bunny
$blingTheFluffOne = Bunny::factory()->create([
'name' => 'Bling',
'fluffiness' => 99,
'birth_date' => now()->subYears(7)
]);
// And one fluffy young bunny
$arnabounTheFreshOne = Bunny::factory()->create([
'name' => 'Arnaboun', // This means "rabbit" in arabic.
'fluffiness' => 9001,
'birth_date' => now()->subYears(2),
]);
// And one not-so-fluffy old bunny
$felixTheOutcast = Bunny::factory()->create([
'name' => 'Félix',
'fluffiness' => 43,
'birth_date' => now()->subYears(7),
]);
// When fetching old and fluffy bunny
$oldButFluffyBunnies = Bunny::oldAndFluffy()->get();
// Then it should have Bling
$this->assertTrue($oldButFluffyBunnies->contains($blingTheFluffOne));
// And not Arnaboun nor Felix
$this->assertFalse($oldButFluffyBunnies->contains($arnabounTheFreshOne));
$this->assertFalse($oldButFluffyBunnies->contains($felixTheOutcast));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment