i want to test my scope in my Statistic Model --->
class Statistic extends Model
{
use HasTranslations;
use HasFactory;
public $translatable = ['country'];
protected $guarded = ['id'];
public function scopeFilter($query, array $filters): Builder
{
return $query->when($filters['search'] ?? false, fn ($query, $search) => $query-
>where(DB::raw('lower(country)'), 'like', '%' . strtolower($search) . '%'));
}
}
how can i organize this?
How to correctly write Feature test for this?
CodePudding user response:
What about this:
class StatisticScopesTest extends TestCase
{
/** @test */
public function is_has_a_scope_filter()
{
$this->assertEquals(0, Statistic::count());
$this->assertEquals(0, Statistic::filter(['search' => 'PT'])->count());
Statistic::factory()->create(['country' => 'PT']);
$this->assertEquals(1, Statistic::count());
$this->assertEquals(1, Statistic::filter(['search' => 'PT'])->count());
$this->assertEquals(0, Statistic::filter(['search' => 'EN'])->count());
}
}
- 2 assertions for the empty case
- create on entry for a country
- 3 assertions for the case with one entry