Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.0, 8.1, 8.2]
php: [8.2, 8.3, 8.4]
dependency-version: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
cache/*
.php_cs
.php_cs.cache
.phpunit.result.cache
Expand Down
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
}
],
"require": {
"php": "^7.4|^8.0",
"twig/twig": "^3.0"
"php": "^8.2",
"twig/twig": "^3.21"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"pestphp/pest": "^1.0",
"phpunit/phpunit": "^9.3",
"symfony/var-dumper": "^5.2",
"vimeo/psalm": "^3.11"
"friendsofphp/php-cs-fixer": "^3.75",
"pestphp/pest": "^3.8",
"symfony/var-dumper": "^7.2",
"vimeo/psalm": "^6.10"
},
"autoload": {
"psr-4": {
Expand Down
29 changes: 9 additions & 20 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Digital Test Suite">
<testsuite name="Arch">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">./src</directory>
<directory>src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<exclude>
<directory>vendor</directory>
</exclude>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public function setup()
$this->twig->setLexer(new ComponentLexer($this->twig));
}

/** @var \Twig\Extension\EscaperExtension */
$escaper = $this->twig->getExtension(\Twig\Extension\EscaperExtension::class);
/** @var \Twig\Runtime\EscaperRuntime */
$escaper = $this->twig->getRuntime(\Twig\Runtime\EscaperRuntime::class);
$escaper->addSafeClass(ComponentAttributeBag::class, ['all']);
$escaper->addSafeClass(ComponentSlot::class, ['all']);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Node/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,12 @@ protected function addGetTemplate(Compiler $compiler)
$repr = $this->isDynamicComponent() ? 'raw' : 'repr';

$compiler
->raw('$this->loadTemplate(' . PHP_EOL)
->raw('$this->load(' . PHP_EOL)
->indent(1)
->write('')
->$repr($this->getTemplateName())
->raw(', ' . PHP_EOL)
->write('')
->$repr($this->getTemplateName())
->raw(', ' . PHP_EOL)
->write('')
->repr($this->getTemplateLine())
->indent(-1)
->raw(PHP_EOL . ');' . PHP_EOL . PHP_EOL);
Expand Down Expand Up @@ -97,17 +94,19 @@ public function getDynamicComponent()
if ($value->hasAttribute('value')) {
// Returns the component string value
$component = '\'' . $value->getAttribute('value') . '\'';

break;
}

if ($value->hasAttribute('name')) {
// Uses the context to get the component value
$component = '($context[\'' . $value->getAttribute('name') . '\'] ?? null)';

break;
}
}

if (!$component) {
if (! $component) {
throw new Exception('Dynamic component must have a component attribute');
}

Expand All @@ -130,6 +129,7 @@ public static function parseDynamicComponent($path, $component)
// Strip anything from the path before the dynamic component name, so it begins with the namespace
$dynamicComponentEndPosition = strpos($path, self::DYNAMIC_COMPONENT_NAME) + strlen(self::DYNAMIC_COMPONENT_NAME);
$pathEnd = substr($path, $dynamicComponentEndPosition);

return $component . $pathEnd;
}

Expand Down Expand Up @@ -185,7 +185,7 @@ protected function addTemplateArguments(Compiler $compiler)

public function filterVariables()
{
if (!$this->isDynamicComponent()) {
if (! $this->isDynamicComponent()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Node/SlotNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SlotNode extends Node implements NodeOutputInterface
{
public function __construct($name, $body, ?AbstractExpression $variables, int $lineno = 0)
{
parent::__construct(['body' => $body], ['name' => $name], $lineno, null);
parent::__construct(['body' => $body], ['name' => $name], $lineno);

if ($variables) {
$this->setNode('variables', $variables);
Expand Down
10 changes: 5 additions & 5 deletions src/TokenParser/ComponentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function parseArguments()
}

if ($stream->nextIf(/* Token::NAME_TYPE */5, 'with')) {
$variables = $this->parser->getExpressionParser()->parseExpression();
$variables = $this->parser->parseExpression();
}

$stream->expect(/* Token::BLOCK_END_TYPE */3);
Expand All @@ -80,9 +80,9 @@ public function parseComponentName(): string

$path = [];

if ($this->parser->getCurrentToken()->getType() != /** Token::NAME_TYPE */ 5) {
throw new Exception('First token must be a name type');
}
// if ($this->parser->getCurrentToken()->getType() !== Token::NAME_TYPE) {
// throw new Exception('First token must be a name type');
// }

$name = $this->getNameSection();

Expand All @@ -93,7 +93,7 @@ public function parseComponentName(): string

$path[] = $name;

while ($stream->nextIf(9 /** Token::PUNCTUATION_TYPE */, '.')) {
while ($stream->nextIf(8 /** Token::PUNCTUATION_TYPE */, '.')) {
$path[] = $this->getNameSection();
}

Expand Down
9 changes: 5 additions & 4 deletions src/TokenParser/SlotTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function parseArguments(): array
}

if ($stream->nextIf(/* Token::NAME_TYPE */5, 'with')) {
$variables = $this->parser->getExpressionParser()->parseExpression();
$variables = $this->parser->parseExpression();
}

$stream->expect(/* Token::BLOCK_END_TYPE */3);
Expand All @@ -45,9 +45,10 @@ public function parseSlotName(): string
{
$stream = $this->parser->getStream();

if ($this->parser->getCurrentToken()->getType() != /** Token::NAME_TYPE */ 5) {
throw new Exception('First token must be a name type');
}
//$stream->expect(5);
// if ($this->parser->getCurrentToken()->getType() != /** Token::NAME_TYPE */ 5) {
// throw new Exception('First token must be a name type');
// }

return $stream->next()->getValue();
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ComponentTokenParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Performing\TwigComponents\Configuration;
use Performing\TwigComponents\TokenParser\ComponentTokenParser;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class ComponentTokenParserTest extends TestCase
{
#[Test]
public function testGetComponentPathWithHintPath()
{
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
Expand All @@ -27,6 +29,7 @@ public function testGetComponentPathWithHintPath()
$this->assertEquals('mynamespace.myplugin::components.test.component', $componentPath);
}

#[Test]
public function testGetComponentPathWithoutHintPath()
{
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
Expand Down
36 changes: 19 additions & 17 deletions tests/ComponentsTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Performing\TwigComponents\Tests;

use PHPUnit\Framework\Attributes\Test;

trait ComponentsTestTrait
{
/** @test */
#[Test]
public function render_simple_component()
{
$html = $this->twig->render('test_simple_component.twig');
Expand All @@ -14,7 +16,7 @@ public function render_simple_component()
HTML, $html);
}

/** @test */
#[Test]
public function render_simple_component_with_dash()
{
$html = $this->twig->render('test_simple_component_with_dash.twig');
Expand All @@ -24,7 +26,7 @@ public function render_simple_component_with_dash()
HTML, $html);
}

/** @test */
#[Test]
public function render_simple_component_in_folder()
{
$html = $this->twig->render('test_simple_component_in_folder.twig');
Expand All @@ -34,7 +36,7 @@ public function render_simple_component_in_folder()
HTML, $html);
}

/** @test */
#[Test]
public function render_component_with_slots()
{
$html = $this->twig->render('test_with_slots.twig');
Expand All @@ -44,7 +46,7 @@ public function render_component_with_slots()
HTML, $html);
}

/** @test */
#[Test]
public function render_xtags_with_slots()
{
$html = $this->twig->render('test_xtags_with_slots.twig');
Expand All @@ -54,7 +56,7 @@ public function render_xtags_with_slots()
HTML, $html);
}

/** @test */
#[Test]
public function render_nested_xtags_with_slots()
{
$html = $this->twig->render('test_nested_xtags_with_slots.twig');
Expand All @@ -64,7 +66,7 @@ public function render_nested_xtags_with_slots()
HTML, $html);
}

/** @test */
#[Test]
public function render_deeply_nested_xtags_with_slots()
{
$html = $this->twig->render('test_deeply_nested_xtags_with_slots.twig');
Expand All @@ -75,7 +77,7 @@ public function render_deeply_nested_xtags_with_slots()
HTML, $html);
}

/** @test */
#[Test]
public function render_component_with_xtags()
{
$html = $this->twig->render('test_xtags_component.twig');
Expand All @@ -87,7 +89,7 @@ public function render_component_with_xtags()
HTML, $html);
}

/** @test */
#[Test]
public function render_component_with_attributes()
{
$html = $this->twig->render('test_with_attributes.twig');
Expand All @@ -101,7 +103,7 @@ public function render_component_with_attributes()
HTML, $html);
}

/** @test */
#[Test]
public function render_namespaced_component()
{
$html = $this->twig->render('test_namespaced_component.twig');
Expand All @@ -111,7 +113,7 @@ public function render_namespaced_component()
HTML, $html);
}

/** @test */
#[Test]
public function render_namespaced_xtags_component()
{
$html = $this->twig->render('test_namespaced_xtags_component.twig');
Expand All @@ -123,7 +125,7 @@ public function render_namespaced_xtags_component()
HTML, $html);
}

/** @test */
#[Test]
public function test_class_merge_works_with_components_in_components()
{
$template = $this->twig->createTemplate(<<<HTML
Expand All @@ -134,7 +136,7 @@ public function test_class_merge_works_with_components_in_components()
$this->assertEquals('<button class="mb-5 bg-red-500 text-white">Click me</button>', $html);
}

/** @test */
#[Test]
public function test_attributes_dont_conflict_with_components_in_components()
{
$template = $this->twig->createTemplate(<<<HTML
Expand All @@ -145,7 +147,7 @@ public function test_attributes_dont_conflict_with_components_in_components()
$this->assertEquals('<div class="mb-5 bg-red-500"><button class="text-white">Click me</button></div>', $html);
}

/** @test */
#[Test]
public function render_simple_dynamic_component()
{
$html = $this->twig->render('test_simple_dynamic_component.twig');
Expand All @@ -156,7 +158,7 @@ public function render_simple_dynamic_component()
HTML, $html);
}

/** @test */
#[Test]
public function render_dynamic_component_with_xtags()
{
$html = $this->twig->render('test_xtags_dynamic_component.twig');
Expand All @@ -168,7 +170,7 @@ public function render_dynamic_component_with_xtags()
HTML, $html);
}

/** @test */
#[Test]
public function render_namespaced_dynamic_component()
{
$html = $this->twig->render('test_namespaced_dynamic_component.twig');
Expand All @@ -178,7 +180,7 @@ public function render_namespaced_dynamic_component()
HTML, $html);
}

/** @test */
#[Test]
public function render_namespaced_xtags_dynamic_component()
{
$html = $this->twig->render('test_namespaced_xtags_dynamic_component.twig');
Expand Down
Loading