Skip to content

Commit

Permalink
Improve twig/twig support
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 21, 2024
1 parent 10bdf19 commit a16ad13
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"symfony/filesystem": "^5.4 || ^6.4 || ^7.0",
"symfony/finder": "^5.4 || ^6.4 || ^7.0",
"symfony/string": "^5.4.42 || ^6.4.10 || ~7.0.10 || ^7.1.3",
"twig/twig": "^3.10",
"twig/twig": "^3.4",
"webmozart/assert": "^1.10"
},
"require-dev": {
Expand Down
23 changes: 23 additions & 0 deletions src/Environment/StubbedEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ public function __construct(
}
}

/**
* Avoid dependency to composer/semver for twig version comparison.
*/
public static function satisfiesTwigVersion(int $major, int $minor = 0, int $patch = 0): bool
{
$version = explode('.', self::VERSION);

if ($major < $version[0]) {
return true;
}
if ($major > $version[0]) {
return false;
}
if ($minor < $version[1]) {
return true;
}
if ($minor > $version[1]) {
return false;
}

return $version[2] >= $patch;
}

/**
* @param string $name
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Runner/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Twig\Source;
use TwigCsFixer\Cache\Manager\CacheManagerInterface;
use TwigCsFixer\Cache\Manager\NullCacheManager;
use TwigCsFixer\Environment\StubbedEnvironment;
use TwigCsFixer\Exception\CannotFixFileException;
use TwigCsFixer\Exception\CannotTokenizeException;
use TwigCsFixer\Report\Report;
Expand Down Expand Up @@ -145,6 +146,17 @@ public function run(iterable $files, Ruleset $ruleset, ?FixerInterface $fixer =

private function parseTemplate(string $content, string $filePath, Report $report): ?ModuleNode
{
if (!StubbedEnvironment::satisfiesTwigVersion(3, 10)) {
$violation = new Violation(
Violation::LEVEL_WARNING,
'Node visitor rules require twig/twig >= 3.10.0',
$filePath,
);
$report->addViolation($violation);

return null;
}

try {
$twigSource = new Source($content, $filePath);

Expand Down
26 changes: 22 additions & 4 deletions tests/Environment/StubbedEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@

final class StubbedEnvironmentTest extends TestCase
{
public function testSatisfiesTwigVersion(): void
{
$version = InstalledVersions::getVersion('twig/twig');
static::assertNotNull($version);
$explodedVersion = explode('.', $version);
$major = (int) $explodedVersion[0];
$minor = (int) $explodedVersion[1];
$patch = (int) $explodedVersion[2];

static::assertTrue(StubbedEnvironment::satisfiesTwigVersion($major, $minor, $patch));
static::assertTrue(StubbedEnvironment::satisfiesTwigVersion($major - 1, $minor, $patch));
static::assertTrue(StubbedEnvironment::satisfiesTwigVersion($major - 1, $minor + 1, $patch + 1));
static::assertTrue(StubbedEnvironment::satisfiesTwigVersion($major, $minor - 1, $patch));
static::assertTrue(StubbedEnvironment::satisfiesTwigVersion($major, $minor - 1, $patch + 1));
static::assertTrue(StubbedEnvironment::satisfiesTwigVersion($major, $minor, $patch - 1));
static::assertFalse(StubbedEnvironment::satisfiesTwigVersion($major + 1, $minor, $patch));
static::assertFalse(StubbedEnvironment::satisfiesTwigVersion($major + 1, $minor - 1, $patch - 1));
static::assertFalse(StubbedEnvironment::satisfiesTwigVersion($major, $minor + 1, $patch));
static::assertFalse(StubbedEnvironment::satisfiesTwigVersion($major, $minor + 1, $patch - 1));
static::assertFalse(StubbedEnvironment::satisfiesTwigVersion($major, $minor, $patch + 1));
}

public function testFilterIsStubbed(): void
{
$env = new StubbedEnvironment();
Expand Down Expand Up @@ -126,10 +148,6 @@ public function testParse(): void

public function testParseCacheTag(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.2.0')) {
static::markTestSkipped('twig/twig ^3.2.0 is required.');
}

$content = file_get_contents(__DIR__.'/Fixtures/cache_tag.html.twig');
static::assertNotFalse($content);

Expand Down
11 changes: 11 additions & 0 deletions tests/Rules/Node/ForbiddenBlock/ForbiddenBlockRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@

namespace TwigCsFixer\Tests\Rules\Node\ForbiddenBlock;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ForbiddenBlockRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ForbiddenBlockRuleTest extends AbstractRuleTestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testConfiguration(): void
{
static::assertSame(
Expand Down
11 changes: 11 additions & 0 deletions tests/Rules/Node/ForbiddenFilter/ForbiddenFilterRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@

namespace TwigCsFixer\Tests\Rules\Node\ForbiddenFilter;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ForbiddenFilterRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ForbiddenFilterRuleTest extends AbstractRuleTestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testConfiguration(): void
{
static::assertSame(
Expand Down
11 changes: 11 additions & 0 deletions tests/Rules/Node/ForbiddenFunction/ForbiddenFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@

namespace TwigCsFixer\Tests\Rules\Node\ForbiddenFunction;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ForbiddenFunctionRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ForbiddenFunctionRuleTest extends AbstractRuleTestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testConfiguration(): void
{
static::assertSame(
Expand Down
11 changes: 11 additions & 0 deletions tests/Rules/Node/NodeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace TwigCsFixer\Tests\Rules\Node;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Node\Node;
Expand All @@ -20,6 +22,15 @@

final class NodeRuleTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testEnterNodeRule(): void
{
$report = new Report([new \SplFileInfo('fakeFile.html.twig')]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@

namespace TwigCsFixer\Tests\Rules\Node\ValidConstantFunction;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use TwigCsFixer\Rules\Node\ValidConstantFunctionRule;
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase;

final class ValidConstantFunctionRuleTest extends AbstractRuleTestCase
{
public const SOME_CONSTANT = 'Foo';

protected function setUp(): void
{
parent::setUp();

if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}
}

public function testRule(): void
{
$this->checkRule(new ValidConstantFunctionRule(), [
Expand Down
14 changes: 14 additions & 0 deletions tests/Runner/LinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace TwigCsFixer\Tests\Runner;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Twig\Environment;
use Twig\Error\SyntaxError;
use TwigCsFixer\Cache\Manager\CacheManagerInterface;
Expand Down Expand Up @@ -274,6 +276,10 @@ public function testFileIsNotCachedWhenReportHasErrors(): void

public function testViolationsFromNodeVisitorRule(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}

$filePath = $this->getTmpPath(__DIR__.'/Fixtures/Linter/node_visitor.twig');

$ruleset = new Ruleset();
Expand Down Expand Up @@ -318,6 +324,10 @@ public function testViolationsFromNodeVisitorRule(): void

public function testNodeVisitorWithInvalidFiles(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}

$filePath = $this->getTmpPath(__DIR__.'/Fixtures/Linter/file.twig');
$filePath2 = $this->getTmpPath(__DIR__.'/Fixtures/Linter/file2.twig');

Expand Down Expand Up @@ -350,6 +360,10 @@ public function testNodeVisitorWithInvalidFiles(): void

public function testNodeVisitorWithBuggyFixer(): void
{
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.10.0')) {
static::markTestSkipped('twig/twig ^3.10.0 is required.');
}

$filePath = $this->getTmpPath(__DIR__.'/Fixtures/Linter/file.twig');

$ruleset = new Ruleset();
Expand Down

0 comments on commit a16ad13

Please sign in to comment.