chore: refactor PluginInlineTemplatesTest to Pest

This commit is contained in:
Benjamin Nussbaum 2025-08-27 20:38:28 +02:00
parent a129c71d79
commit 2eee024b36

View file

@ -1,23 +1,15 @@
<?php <?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Plugin; use App\Models\Plugin;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PluginInlineTemplatesTest extends TestCase uses(RefreshDatabase::class);
{
use RefreshDatabase;
public function test_plugin_with_inline_templates(): void test('renders plugin with inline templates', function () {
{ $plugin = Plugin::factory()->create([
$plugin = Plugin::factory()->create([ 'name' => 'Test Plugin',
'name' => 'Test Plugin', 'markup_language' => 'liquid',
'markup_language' => 'liquid', 'render_markup' => <<<'LIQUID'
'render_markup' => <<<'LIQUID'
{% assign min = 1 %} {% assign min = 1 %}
{% assign max = facts | size %} {% assign max = facts | size %}
{% assign diff = max | minus: min %} {% assign diff = max | minus: min %}
@ -39,7 +31,6 @@ class PluginInlineTemplatesTest extends TestCase
{% template title_bar %} {% template title_bar %}
<div class="title_bar"> <div class="title_bar">
<img class="image" src="https://res.jwq.lol/img/lumon.svg">
<span class="title">{{ trmnl.plugin_settings.instance_name }}</span> <span class="title">{{ trmnl.plugin_settings.instance_name }}</span>
<span class="instance">{{ instance }}</span> <span class="instance">{{ instance }}</span>
</div> </div>
@ -59,32 +50,31 @@ class PluginInlineTemplatesTest extends TestCase
%} %}
</div> </div>
LIQUID LIQUID
, ,
'data_payload' => [ 'data_payload' => [
'facts' => ['Fact 1', 'Fact 2', 'Fact 3'], 'facts' => ['Fact 1', 'Fact 2', 'Fact 3'],
], ],
]); ]);
$result = $plugin->render('full'); $result = $plugin->render('full');
// Should render both templates // Should render both templates
// Check for any of the facts (since random number generation is non-deterministic) // Check for any of the facts (since random number generation is non-deterministic)
$this->assertTrue( $this->assertTrue(
str_contains($result, 'Fact 1') || str_contains($result, 'Fact 1') ||
str_contains($result, 'Fact 2') || str_contains($result, 'Fact 2') ||
str_contains($result, 'Fact 3') str_contains($result, 'Fact 3')
); );
$this->assertStringContainsString('Test Plugin', $result); $this->assertStringContainsString('Test Plugin', $result);
$this->assertStringContainsString('Please try to enjoy each fact equally', $result); $this->assertStringContainsString('Please try to enjoy each fact equally', $result);
$this->assertStringContainsString('class="view view--full"', $result); $this->assertStringContainsString('class="view view--full"', $result);
} });
public function test_plugin_with_inline_templates_using_with_syntax(): void test('renders plugin with inline templates using with syntax', function () {
{ $plugin = Plugin::factory()->create([
$plugin = Plugin::factory()->create([ 'name' => 'Test Plugin',
'name' => 'Test Plugin', 'markup_language' => 'liquid',
'markup_language' => 'liquid', 'render_markup' => <<<'LIQUID'
'render_markup' => <<<'LIQUID'
{% assign min = 1 %} {% assign min = 1 %}
{% assign max = facts | size %} {% assign max = facts | size %}
{% assign diff = max | minus: min %} {% assign diff = max | minus: min %}
@ -126,31 +116,30 @@ LIQUID
%} %}
</div> </div>
LIQUID LIQUID
, ,
'data_payload' => [ 'data_payload' => [
'facts' => ['Fact 1', 'Fact 2', 'Fact 3'], 'facts' => ['Fact 1', 'Fact 2', 'Fact 3'],
], ],
]); ]);
$result = $plugin->render('full'); $result = $plugin->render('full');
// Should render both templates // Should render both templates
// Check for any of the facts (since random number generation is non-deterministic) // Check for any of the facts (since random number generation is non-deterministic)
$this->assertTrue( $this->assertTrue(
str_contains($result, 'Fact 1') || str_contains($result, 'Fact 1') ||
str_contains($result, 'Fact 2') || str_contains($result, 'Fact 2') ||
str_contains($result, 'Fact 3') str_contains($result, 'Fact 3')
); );
$this->assertStringContainsString('Test Plugin', $result); $this->assertStringContainsString('Test Plugin', $result);
$this->assertStringContainsString('Please try to enjoy each fact equally', $result); $this->assertStringContainsString('Please try to enjoy each fact equally', $result);
$this->assertStringContainsString('class="view view--full"', $result); $this->assertStringContainsString('class="view view--full"', $result);
} });
public function test_plugin_with_simple_inline_template(): void test('renders plugin with simple inline template', function () {
{ $plugin = Plugin::factory()->create([
$plugin = Plugin::factory()->create([ 'markup_language' => 'liquid',
'markup_language' => 'liquid', 'render_markup' => <<<'LIQUID'
'render_markup' => <<<'LIQUID'
{% template simple %} {% template simple %}
<div class="simple"> <div class="simple">
<h1>{{ title }}</h1> <h1>{{ title }}</h1>
@ -163,21 +152,20 @@ LIQUID
content: "This is a test" content: "This is a test"
%} %}
LIQUID LIQUID
, ,
]); ]);
$result = $plugin->render('full'); $result = $plugin->render('full');
$this->assertStringContainsString('Hello World', $result); $this->assertStringContainsString('Hello World', $result);
$this->assertStringContainsString('This is a test', $result); $this->assertStringContainsString('This is a test', $result);
$this->assertStringContainsString('class="simple"', $result); $this->assertStringContainsString('class="simple"', $result);
} });
public function test_plugin_with_find_by_filter(): void test('renders plugin with liquid filter find_by', function () {
{ $plugin = Plugin::factory()->create([
$plugin = Plugin::factory()->create([ 'markup_language' => 'liquid',
'markup_language' => 'liquid', 'render_markup' => <<<'LIQUID'
'render_markup' => <<<'LIQUID'
{% template user_info %} {% template user_info %}
<div class="user"> <div class="user">
<h2>{{ user.name }}</h2> <h2>{{ user.name }}</h2>
@ -188,68 +176,65 @@ LIQUID
{% assign found_user = collection | find_by: 'name', 'Ryan' %} {% assign found_user = collection | find_by: 'name', 'Ryan' %}
{% render "user_info", user: found_user %} {% render "user_info", user: found_user %}
LIQUID LIQUID
, ,
'data_payload' => [ 'data_payload' => [
'collection' => [ 'collection' => [
['name' => 'Ryan', 'age' => 35], ['name' => 'Ryan', 'age' => 35],
['name' => 'Sara', 'age' => 29], ['name' => 'Sara', 'age' => 29],
['name' => 'Jimbob', 'age' => 29], ['name' => 'Jimbob', 'age' => 29],
],
], ],
]); ],
]);
$result = $plugin->render('full'); $result = $plugin->render('full');
// Should render the user info for Ryan // Should render the user info for Ryan
$this->assertStringContainsString('Ryan', $result); $this->assertStringContainsString('Ryan', $result);
$this->assertStringContainsString('Age: 35', $result); $this->assertStringContainsString('Age: 35', $result);
$this->assertStringContainsString('class="user"', $result); $this->assertStringContainsString('class="user"', $result);
} });
public function test_plugin_with_find_by_filter_and_fallback(): void test('renders plugin with liquid filter find_by and fallback', function () {
{ $plugin = Plugin::factory()->create([
$plugin = Plugin::factory()->create([ 'markup_language' => 'liquid',
'markup_language' => 'liquid', 'render_markup' => <<<'LIQUID'
'render_markup' => <<<'LIQUID'
{{ collection | find_by: 'name', 'ronak', 'Not Found' }} {{ collection | find_by: 'name', 'ronak', 'Not Found' }}
LIQUID LIQUID
, ,
'data_payload' => [ 'data_payload' => [
'collection' => [ 'collection' => [
['name' => 'Ryan', 'age' => 35], ['name' => 'Ryan', 'age' => 35],
['name' => 'Sara', 'age' => 29], ['name' => 'Sara', 'age' => 29],
['name' => 'Jimbob', 'age' => 29], ['name' => 'Jimbob', 'age' => 29],
],
], ],
]); ],
]);
$result = $plugin->render('full'); $result = $plugin->render('full');
// Should return the fallback value // Should return the fallback value
$this->assertStringContainsString('Not Found', $result); $this->assertStringContainsString('Not Found', $result);
} });
public function test_plugin_with_group_by_filter(): void test('renders plugin with liquid filter group_by', function () {
{ $plugin = Plugin::factory()->create([
$plugin = Plugin::factory()->create([ 'markup_language' => 'liquid',
'markup_language' => 'liquid', 'render_markup' => <<<'LIQUID'
'render_markup' => <<<'LIQUID'
{{ collection | group_by: 'age' | json }} {{ collection | group_by: 'age' | json }}
LIQUID LIQUID
, ,
'data_payload' => [ 'data_payload' => [
'collection' => [ 'collection' => [
['name' => 'Ryan', 'age' => 35], ['name' => 'Ryan', 'age' => 35],
['name' => 'Sara', 'age' => 29], ['name' => 'Sara', 'age' => 29],
['name' => 'Jimbob', 'age' => 29], ['name' => 'Jimbob', 'age' => 29],
],
], ],
]); ],
]);
$result = $plugin->render('full'); $result = $plugin->render('full');
// Should output JSON representation of grouped data // Should output JSON representation of grouped data
$this->assertStringContainsString('"35":[{"name":"Ryan","age":35}]', $result); $this->assertStringContainsString('"35":[{"name":"Ryan","age":35}]', $result);
$this->assertStringContainsString('"29":[{"name":"Sara","age":29},{"name":"Jimbob","age":29}]', $result); $this->assertStringContainsString('"29":[{"name":"Sara","age":29},{"name":"Jimbob","age":29}]', $result);
} });
}