Skip to content

Commit

Permalink
Add test for phar (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Jul 21, 2024
1 parent a4466a4 commit 5846e15
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/phar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
- name: Ensure the PHAR works
run: bin/twig-cs-fixer.phar --ansi --version

- name: E2E test
run: bin/twig-cs-fixer.phar lint -c tests/Binary/Fixtures/.twig-cs-fixer.php tests/Binary/Fixtures/file.twig

- name: Import GPG key
if: github.event_name == 'release'
uses: crazy-max/ghaction-import-gpg@v6
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ infection.html

## Box
bin/twig-cs-fixer.phar
.twig-cs-fixer.cache
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ vendor/bin/twig-cs-fixer lint /path/to/code
vendor/bin/twig-cs-fixer lint --fix /path/to/code
```

Although [bin-dependencies may have composer conflicts](https://github.com/bamarni/composer-bin-plugin#why-a-hard-problem-with-a-simple-solution),
this is the recommended way because it will autoload everything you need.

### As a PHAR

You can always fetch the stable version as a Phar archive through the following
Expand All @@ -39,10 +42,13 @@ wget -c https://github.com/VincentLanglet/Twig-CS-Fixer/releases/download/VERSIO
The PHAR files are signed with a public key which can be queried at
`keys.openpgp.org` with the id `AC0E7FD8858D80003AA88FF8DEBB71EDE9601234`.

NB: If you want to [use node based rules](docs/configuration.md#node-based-rules) with the PHAR,
you may need to [configure token parsers](docs/configuration.md#token-parsers--more).
You can take a look into [StubbedEnvironment::handleOptionalDependencies()](https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/src/Environment/StubbedEnvironment.php)
method to know which ones are generally needed.
NB: You will certainly need to add
```php
require_once __DIR__.'/vendor/autoload.php';
```
in your [config file](docs/configuration.md) in order to:
- Use existing [node based rules](docs/configuration.md#node-based-rules).
- Write your own custom rules.

## Twig Coding Standard Rules

Expand Down
14 changes: 2 additions & 12 deletions scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,15 @@

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Twig\Environment;
use Twig\Extension\ExtensionInterface;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\Source;
use Twig\TokenParser\TokenParserInterface;

return [
'exclude-namespaces' => ['TwigCsFixer'],
'expose-global-constants' => false,
'expose-global-classes' => false,
'expose-global-functions' => false,
'expose-namespaces' => ['Twig\Node'],
'expose-namespaces' => ['/^Twig(?!\\\\Test(\\\\|$))/'],
'expose-classes' => [
OutputInterface::class,
Finder::class,
Environment::class,
ExtensionInterface::class,
NodeVisitorInterface::class,
Source::class,
TokenParserInterface::class,
OutputInterface::class,
],
];
136 changes: 136 additions & 0 deletions tests/Binary/Fixtures/.twig-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Twig\Environment;
use Twig\Extension\ExtensionInterface;
use Twig\Node\Node;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\Parser;
use Twig\Token;
use Twig\TokenParser\TokenParserInterface;
use TwigCsFixer\Config\Config;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\Reporter\ReporterInterface;
use TwigCsFixer\Rules\AbstractRule;
use TwigCsFixer\Rules\Node\AbstractNodeRule;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Standard\StandardInterface;
use TwigCsFixer\Token\Tokens;

$rule = new class() extends AbstractRule {
protected function process(int $tokenIndex, Tokens $tokens): void
{
}
};

$nodeRule = new class() extends AbstractNodeRule {
public function enterNode(Node $node, Environment $env): Node
{
return $node;
}
};

$standard = new class() implements StandardInterface {
public function getRules(): array
{
return [];
}
};

$reporter = new class() implements ReporterInterface {
public function display(
OutputInterface $output,
Report $report,
?string $level,
bool $debug
): void {
}

public function getName(): string
{
return 'custom';
}
};

$twigExtension = new class() implements ExtensionInterface {
public function getTokenParsers(): array
{
return [];
}

public function getNodeVisitors(): array
{
return [];
}

public function getFilters(): array
{
return [];
}

public function getTests(): array
{
return [];
}

public function getFunctions(): array
{
return [];
}

public function getOperators(): array
{
return [[], []];
}
};

$tokenParser = new class() implements TokenParserInterface {
public function setParser(Parser $parser): void
{
}

public function parse(Token $token): Node
{
return new Node();
}

public function getTag(): string
{
return 'custom';
}
};

$nodeVisitor = new class() implements NodeVisitorInterface {
public function enterNode(Node $node, Environment $env): Node
{
return $node;
}

public function leaveNode(Node $node, Environment $env): ?Node
{
return $node;
}

public function getPriority(): int
{
return 0;
}
};

$ruleset = new Ruleset();
$ruleset->addStandard($standard);
$ruleset->addRule($rule);
$ruleset->addRule($nodeRule);

$config = new Config('Custom');
$config->setFinder(new Finder());
$config->setRuleset($ruleset);
$config->addCustomReporter($reporter);
$config->addTwigExtension($twigExtension);
$config->addTokenParser($tokenParser);
$config->addNodeVisitor($nodeVisitor);

return $config;

0 comments on commit 5846e15

Please sign in to comment.