feat: add Liquid filters 'sample', 'days_ago'

This commit is contained in:
Benjamin Nussbaum 2025-08-27 21:18:22 +02:00
parent 2eee024b36
commit f38ac778f1
5 changed files with 117 additions and 2 deletions

View file

@ -237,3 +237,43 @@ test('group_by filter handles mixed data types as keys', function () {
'' => [['name' => 'Alice', 'active' => null]], // PHP converts null keys to empty string
]);
});
test('sample filter returns a random element from array', function () {
$filter = new Data();
$array = ['1', '2', '3', '4', '5'];
$result = $filter->sample($array);
expect($result)->toBeIn($array);
});
test('sample filter returns a random element from string array', function () {
$filter = new Data();
$array = ['cat', 'dog'];
$result = $filter->sample($array);
expect($result)->toBeIn($array);
});
test('sample filter returns null for empty array', function () {
$filter = new Data();
$array = [];
$result = $filter->sample($array);
expect($result)->toBeNull();
});
test('sample filter returns the only element from single element array', function () {
$filter = new Data();
$array = ['single'];
$result = $filter->sample($array);
expect($result)->toBe('single');
});
test('sample filter works with mixed data types', function () {
$filter = new Data();
$array = [1, 'string', true, null, ['nested']];
$result = $filter->sample($array);
expect($result)->toBeIn($array);
});

View file

@ -0,0 +1,32 @@
<?php
use App\Liquid\Filters\Date;
use Carbon\Carbon;
test('days_ago filter returns correct date', function () {
$filter = new Date();
$threeDaysAgo = Carbon::now()->subDays(3)->toDateString();
expect($filter->days_ago(3))->toBe($threeDaysAgo);
});
test('days_ago filter handles string input', function () {
$filter = new Date();
$fiveDaysAgo = Carbon::now()->subDays(5)->toDateString();
expect($filter->days_ago('5'))->toBe($fiveDaysAgo);
});
test('days_ago filter with zero days returns today', function () {
$filter = new Date();
$today = Carbon::now()->toDateString();
expect($filter->days_ago(0))->toBe($today);
});
test('days_ago filter with large number works correctly', function () {
$filter = new Date();
$hundredDaysAgo = Carbon::now()->subDays(100)->toDateString();
expect($filter->days_ago(100))->toBe($hundredDaysAgo);
});