mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-14 07:27:47 +00:00
feat: add TRMNL custom Liquid filters
This commit is contained in:
parent
227f0e51c2
commit
2cfb345d48
12 changed files with 519 additions and 0 deletions
55
tests/Unit/Liquid/Filters/DataTest.php
Normal file
55
tests/Unit/Liquid/Filters/DataTest.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
use App\Liquid\Filters\Data;
|
||||
|
||||
test('json filter converts arrays to JSON', function () {
|
||||
$filter = new Data();
|
||||
$array = ['foo' => 'bar', 'baz' => 'qux'];
|
||||
|
||||
expect($filter->json($array))->toBe('{"foo":"bar","baz":"qux"}');
|
||||
});
|
||||
|
||||
test('json filter converts objects to JSON', function () {
|
||||
$filter = new Data();
|
||||
$object = new stdClass();
|
||||
$object->foo = 'bar';
|
||||
$object->baz = 'qux';
|
||||
|
||||
expect($filter->json($object))->toBe('{"foo":"bar","baz":"qux"}');
|
||||
});
|
||||
|
||||
test('json filter handles nested structures', function () {
|
||||
$filter = new Data();
|
||||
$nested = [
|
||||
'foo' => 'bar',
|
||||
'nested' => [
|
||||
'baz' => 'qux',
|
||||
'items' => [1, 2, 3],
|
||||
],
|
||||
];
|
||||
|
||||
expect($filter->json($nested))->toBe('{"foo":"bar","nested":{"baz":"qux","items":[1,2,3]}}');
|
||||
});
|
||||
|
||||
test('json filter handles scalar values', function () {
|
||||
$filter = new Data();
|
||||
|
||||
expect($filter->json('string'))->toBe('"string"');
|
||||
expect($filter->json(123))->toBe('123');
|
||||
expect($filter->json(true))->toBe('true');
|
||||
expect($filter->json(null))->toBe('null');
|
||||
});
|
||||
|
||||
test('json filter preserves unicode characters', function () {
|
||||
$filter = new Data();
|
||||
$data = ['message' => 'Hello, 世界'];
|
||||
|
||||
expect($filter->json($data))->toBe('{"message":"Hello, 世界"}');
|
||||
});
|
||||
|
||||
test('json filter does not escape slashes', function () {
|
||||
$filter = new Data();
|
||||
$data = ['url' => 'https://example.com/path'];
|
||||
|
||||
expect($filter->json($data))->toBe('{"url":"https://example.com/path"}');
|
||||
});
|
||||
62
tests/Unit/Liquid/Filters/LocalizationTest.php
Normal file
62
tests/Unit/Liquid/Filters/LocalizationTest.php
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
use App\Liquid\Filters\Localization;
|
||||
|
||||
test('l_date formats date with default format', function () {
|
||||
$filter = new Localization();
|
||||
$date = '2025-01-11';
|
||||
|
||||
$result = $filter->l_date($date);
|
||||
|
||||
// Default format is 'Y-m-d', which should output something like '2025-01-11'
|
||||
// The exact output might vary depending on the locale, but it should contain the year, month, and day
|
||||
expect($result)->toContain('2025');
|
||||
expect($result)->toContain('01');
|
||||
expect($result)->toContain('11');
|
||||
});
|
||||
|
||||
test('l_date formats date with custom format', function () {
|
||||
$filter = new Localization();
|
||||
$date = '2025-01-11';
|
||||
|
||||
$result = $filter->l_date($date, '%y %b');
|
||||
|
||||
// Format '%y %b' should output something like '25 Jan'
|
||||
// The month name might vary depending on the locale
|
||||
expect($result)->toContain('25');
|
||||
// We can't check for 'Jan' specifically as it might be localized
|
||||
});
|
||||
|
||||
test('l_date handles DateTime objects', function () {
|
||||
$filter = new Localization();
|
||||
$date = new DateTimeImmutable('2025-01-11');
|
||||
|
||||
$result = $filter->l_date($date, 'Y-m-d');
|
||||
|
||||
expect($result)->toContain('2025-01-11');
|
||||
});
|
||||
|
||||
test('l_word translates common words', function () {
|
||||
$filter = new Localization();
|
||||
|
||||
expect($filter->l_word('today', 'de'))->toBe('heute');
|
||||
});
|
||||
|
||||
test('l_word returns original word if no translation exists', function () {
|
||||
$filter = new Localization();
|
||||
|
||||
expect($filter->l_word('hello', 'es-ES'))->toBe('hello');
|
||||
expect($filter->l_word('world', 'ko'))->toBe('world');
|
||||
});
|
||||
|
||||
test('l_word is case-insensitive', function () {
|
||||
$filter = new Localization();
|
||||
|
||||
expect($filter->l_word('TODAY', 'de'))->toBe('heute');
|
||||
});
|
||||
|
||||
test('l_word returns original word for unknown locales', function () {
|
||||
$filter = new Localization();
|
||||
|
||||
expect($filter->l_word('today', 'unknown-locale'))->toBe('today');
|
||||
});
|
||||
47
tests/Unit/Liquid/Filters/NumbersTest.php
Normal file
47
tests/Unit/Liquid/Filters/NumbersTest.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
use App\Liquid\Filters\Numbers;
|
||||
|
||||
test('number_with_delimiter formats numbers with commas by default', function () {
|
||||
$filter = new Numbers();
|
||||
|
||||
expect($filter->number_with_delimiter(1234))->toBe('1,234');
|
||||
expect($filter->number_with_delimiter(1000000))->toBe('1,000,000');
|
||||
expect($filter->number_with_delimiter(0))->toBe('0');
|
||||
});
|
||||
|
||||
test('number_with_delimiter handles custom delimiters', function () {
|
||||
$filter = new Numbers();
|
||||
|
||||
expect($filter->number_with_delimiter(1234, '.'))->toBe('1.234');
|
||||
expect($filter->number_with_delimiter(1000000, ' '))->toBe('1 000 000');
|
||||
});
|
||||
|
||||
test('number_with_delimiter handles decimal values with custom separators', function () {
|
||||
$filter = new Numbers();
|
||||
|
||||
expect($filter->number_with_delimiter(1234.57, ' ', ','))->toBe('1 234,57');
|
||||
expect($filter->number_with_delimiter(1234.5, '.', ','))->toBe('1.234,50');
|
||||
});
|
||||
|
||||
test('number_to_currency formats numbers with dollar sign by default', function () {
|
||||
$filter = new Numbers();
|
||||
|
||||
expect($filter->number_to_currency(1234))->toBe('$1,234');
|
||||
expect($filter->number_to_currency(1234.5))->toBe('$1,234.50');
|
||||
expect($filter->number_to_currency(0))->toBe('$0');
|
||||
});
|
||||
|
||||
test('number_to_currency handles custom currency symbols', function () {
|
||||
$filter = new Numbers();
|
||||
|
||||
expect($filter->number_to_currency(1234, '£'))->toBe('£1,234');
|
||||
expect($filter->number_to_currency(152350.69, '€'))->toBe('€152,350.69');
|
||||
});
|
||||
|
||||
test('number_to_currency handles custom delimiters and separators', function () {
|
||||
$filter = new Numbers();
|
||||
|
||||
expect($filter->number_to_currency(1234.57, '£', '.', ','))->toBe('1.234,57 £');
|
||||
expect($filter->number_to_currency(1234.57, '€', ',', '.'))->toBe('€1,234.57');
|
||||
});
|
||||
90
tests/Unit/Liquid/Filters/StringMarkupTest.php
Normal file
90
tests/Unit/Liquid/Filters/StringMarkupTest.php
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
use App\Liquid\Filters\StringMarkup;
|
||||
|
||||
test('pluralize returns singular form with count 1', function () {
|
||||
$filter = new StringMarkup();
|
||||
|
||||
expect($filter->pluralize('book', 1))->toBe('1 book');
|
||||
expect($filter->pluralize('person', 1))->toBe('1 person');
|
||||
});
|
||||
|
||||
test('pluralize returns plural form with count greater than 1', function () {
|
||||
$filter = new StringMarkup();
|
||||
|
||||
expect($filter->pluralize('book', 2))->toBe('2 books');
|
||||
expect($filter->pluralize('person', 4))->toBe('4 people');
|
||||
});
|
||||
|
||||
test('pluralize handles irregular plurals correctly', function () {
|
||||
$filter = new StringMarkup();
|
||||
|
||||
expect($filter->pluralize('child', 3))->toBe('3 children');
|
||||
expect($filter->pluralize('sheep', 5))->toBe('5 sheep');
|
||||
});
|
||||
|
||||
test('pluralize uses default count of 2 when not specified', function () {
|
||||
$filter = new StringMarkup();
|
||||
|
||||
expect($filter->pluralize('book'))->toBe('2 books');
|
||||
expect($filter->pluralize('person'))->toBe('2 people');
|
||||
});
|
||||
|
||||
test('markdown_to_html converts basic markdown to HTML', function () {
|
||||
$filter = new StringMarkup();
|
||||
$markdown = 'This is *italic* and **bold**.';
|
||||
|
||||
// The exact HTML output might vary depending on the Parsedown implementation
|
||||
// So we'll check for the presence of HTML tags rather than the exact output
|
||||
$result = $filter->markdown_to_html($markdown);
|
||||
|
||||
expect($result)->toContain('<em>italic</em>');
|
||||
expect($result)->toContain('<strong>bold</strong>');
|
||||
});
|
||||
|
||||
test('markdown_to_html converts links correctly', function () {
|
||||
$filter = new StringMarkup();
|
||||
$markdown = 'This is [a link](https://example.com).';
|
||||
|
||||
$result = $filter->markdown_to_html($markdown);
|
||||
|
||||
expect($result)->toContain('<a href="https://example.com">a link</a>');
|
||||
});
|
||||
|
||||
test('markdown_to_html handles fallback when Parsedown is not available', function () {
|
||||
// Create a mock that simulates Parsedown not being available
|
||||
$filter = new class extends StringMarkup
|
||||
{
|
||||
public function markdown_to_html(string $markdown): string
|
||||
{
|
||||
// Force the fallback path
|
||||
return nl2br(htmlspecialchars($markdown));
|
||||
}
|
||||
};
|
||||
|
||||
$markdown = 'This is *italic* and [a link](https://example.com).';
|
||||
$result = $filter->markdown_to_html($markdown);
|
||||
|
||||
expect($result)->toBe('This is *italic* and [a link](https://example.com).');
|
||||
});
|
||||
|
||||
test('strip_html removes HTML tags', function () {
|
||||
$filter = new StringMarkup();
|
||||
$html = '<p>This is <strong>bold</strong> and <em>italic</em>.</p>';
|
||||
|
||||
expect($filter->strip_html($html))->toBe('This is bold and italic.');
|
||||
});
|
||||
|
||||
test('strip_html preserves text content', function () {
|
||||
$filter = new StringMarkup();
|
||||
$html = '<div>Hello, <span>world</span>!</div>';
|
||||
|
||||
expect($filter->strip_html($html))->toBe('Hello, world!');
|
||||
});
|
||||
|
||||
test('strip_html handles nested tags', function () {
|
||||
$filter = new StringMarkup();
|
||||
$html = '<div><p>Paragraph <strong>with <em>nested</em> tags</strong>.</p></div>';
|
||||
|
||||
expect($filter->strip_html($html))->toBe('Paragraph with nested tags.');
|
||||
});
|
||||
13
tests/Unit/Liquid/Filters/UniquenessTest.php
Normal file
13
tests/Unit/Liquid/Filters/UniquenessTest.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
use App\Liquid\Filters\Uniqueness;
|
||||
|
||||
test('append_random appends a random string with 4 characters', function () {
|
||||
$filter = new Uniqueness();
|
||||
$result = $filter->append_random('chart-');
|
||||
|
||||
// Check that the result starts with the prefix
|
||||
expect($result)->toStartWith('chart-');
|
||||
// Check that the result is longer than just the prefix (has random part)
|
||||
expect(mb_strlen($result))->toBe(mb_strlen('chart-') + 4);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue