Scape/tests/FilterTest.php

145 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Scape\Tests;
use PHPUnit\Framework\TestCase;
use Scape\Engine;
class FilterTest extends TestCase
{
private string $templatesDir;
private string $filtersDir;
protected function setUp(): void
{
$this->templatesDir = __DIR__ . '/fixtures';
$this->filtersDir = __DIR__ . '/fixtures/filters';
}
public function testInternalFilters(): void
{
file_put_contents($this->templatesDir . '/tests/filters_internal.scape.php',
'{( uses filters:string )}' . "\n" .
'{{ name | lower }}' . "\n" .
'{{ name | upper }}' . "\n" .
'{{ name | lower | ucfirst }}' . "\n" .
'{{ price | currency(\'USD\') }}' . "\n" .
'{{ val | float(3) }}' . "\n" .
'{{ date_val | date(\'Y-m-d\') }}' . "\n" .
'{{ bio | truncate(10) }}' . "\n" .
'{{ missing | default(\'N/A\') }}' . "\n" .
'{{ tags | join(\', \') }}' . "\n" .
'{{ tags | first }}' . "\n" .
'{{ tags | last }}' . "\n" .
'{{ bio | word_count }}' . "\n" .
'{{ user_data | keys | join(\',\') }}' . "\n" .
'{{ query | url_encode }}' . "\n" .
'{{{ user_data | json }}}'
);
$engine = new Engine(['templates_dir' => $this->templatesDir]);
$output = $engine->render('tests.filters_internal', [
'name' => 'fUnKy',
'price' => 1234.56,
'val' => 123.45678,
'date_val' => 1707693300,
'bio' => 'A very long biography indeed.',
'missing' => '',
'tags' => ['PHP', 'Scape', 'Templates'],
'user_data' => ['id' => 1, 'name' => 'Funky'],
'query' => 'hello world'
]);
$lines = explode("\n", $output);
$this->assertEquals('funky', $lines[0]);
$this->assertEquals('FUNKY', $lines[1]);
$this->assertEquals('Funky', $lines[2]);
$this->assertEquals('$1,234.56', $lines[3]);
$this->assertEquals('123.457', $lines[4]);
$this->assertStringContainsString('2024-02-11', $output);
$this->assertStringContainsString('A very lon...', $output);
$this->assertStringContainsString('N/A', $output);
$this->assertStringContainsString('PHP, Scape, Templates', $output);
$this->assertStringContainsString('PHP', $output);
$this->assertStringContainsString('Templates', $output);
$this->assertStringContainsString('5', $output);
$this->assertStringContainsString('id,name', $output);
$this->assertStringContainsString('hello+world', $output);
$this->assertStringContainsString('{"id":1,"name":"Funky"}', $output);
unlink($this->templatesDir . '/tests/filters_internal.scape.php');
}
public function testCustomFilterWithArguments(): void
{
file_put_contents($this->templatesDir . '/tests/filters_custom.scape.php',
'{( load_filter(\'currency\') )}' . "\n" .
'{{ price | currency }}' . "\n" .
'{{ price | currency(\'£\') }}' . "\n" .
'{{ price | currency(sym) }}'
);
$engine = new Engine([
'templates_dir' => $this->templatesDir,
'filters_dir' => $this->filtersDir
]);
$output = $engine->render('tests.filters_custom', [
'price' => 1234.567,
'sym' => '€'
]);
$expected = "$1,234.57\n£1,234.57\n€1,234.57";
$this->assertEquals($expected, $output);
unlink($this->templatesDir . '/tests/filters_custom.scape.php');
}
public function testFiltersInForeach(): void
{
file_put_contents($this->templatesDir . '/tests/filters_foreach.scape.php',
'{( uses filters:string )}' . "\n" .
'{( foreach key in user_data | keys )}' . "\n" .
'{{ key }}: {{ user_data[key] }}' . "\n" .
'{( endforeach )}'
);
$engine = new Engine(['templates_dir' => $this->templatesDir]);
$output = $engine->render('tests.filters_foreach', [
'user_data' => ['id' => 1, 'name' => 'Funky']
]);
$this->assertStringContainsString('id: 1', $output);
$this->assertStringContainsString('name: Funky', $output);
unlink($this->templatesDir . '/tests/filters_foreach.scape.php');
}
public function testFiltersInInclude(): void
{
file_put_contents($this->templatesDir . '/partials/keys_list.scape.php',
'Keys: {{ context | join(\', \') }}'
);
file_put_contents($this->templatesDir . '/tests/filters_include.scape.php',
'{( uses filters:string )}' . "\n" .
'{[ include \'keys_list\' with user_data | keys ]}'
);
$engine = new Engine([
'templates_dir' => $this->templatesDir,
'partials_dir' => $this->templatesDir . '/partials'
]);
$output = $engine->render('tests.filters_include', [
'user_data' => ['id' => 1, 'name' => 'Funky']
]);
$this->assertEquals('Keys: id, name', trim($output));
unlink($this->templatesDir . '/partials/keys_list.scape.php');
unlink($this->templatesDir . '/tests/filters_include.scape.php');
}
}