Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 5, 2024
1 parent 2c309bc commit b8e952d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
28 changes: 28 additions & 0 deletions tests/Report/ViolationIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,32 @@ public static function toStringDataProvider(): iterable
yield ['short', 'id', 'token', 1, null, 'short.id.token:1'];
yield ['short', 'id', 'token', 1, 1, 'short.id.token:1:1'];
}

/**
* @dataProvider matchDataProvider
*/
public function testMatch(string $string1, string $string2, bool $expected,): void
{
$violationId1 = ViolationId::fromString($string1);
$violationId2 = ViolationId::fromString($string2);
static::assertSame($expected, $violationId1->match($violationId2));
}

/**
* @return iterable<array-key, array{string, string, bool}>
*/
public static function matchDataProvider(): iterable
{
yield ['short', 'short', true];
yield ['short', 'short.id.token:1:1', true];
yield ['short.id', 'short.id.token:1:1', true];
yield ['short.notId', 'short.id.token:1:1', false];
yield ['short.id', 'short', false];
yield ['SHORT.ID.TOKEN', 'short.id.token:1:1', true];
yield ['short.id.token:2:1', 'short.id.token:1:1', false];
yield ['short.id.token:1:2', 'short.id.token:1:1', false];
yield ['short::1', 'short.id.token:1:1', true];
yield ['short.id::1', 'short.id.token:1:1', true];
yield ['short.id.token::1', 'short.id.token:1:1', true];
}
}
1 change: 0 additions & 1 deletion tests/Rules/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace TwigCsFixer\Tests\Rules;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SplFileInfo;
use TwigCsFixer\Environment\StubbedEnvironment;
Expand Down
34 changes: 34 additions & 0 deletions tests/Runner/FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,40 @@ protected function process(int $tokenPosition, array $tokens): void
static::assertSame('test 2 2', $fixer->fixFile('test test test', $ruleset));
}

public function testIgnoredViolations(): void
{
$tokenizer = new Tokenizer(new StubbedEnvironment());

$rule = new class () extends AbstractRule {
public function getShortName(): string
{
return 'Rule';
}

protected function process(int $tokenPosition, array $tokens): void
{
$fixer = $this->addFixableWarning('Error', $tokens[$tokenPosition]);
if (null !== $fixer) {
$fixer->replaceToken($tokenPosition, 'a');
}

$fixer = $this->addFixableError('Error', $tokens[$tokenPosition]);
if (null !== $fixer) {
$fixer->replaceToken($tokenPosition, 'b');
}
}
};

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

$fixer = new Fixer($tokenizer);

$content = '{# twig-cs-fixer-disable Rule #}';
// The rule should produce an infinite loop but the comment disable it
static::assertSame($content, $fixer->fixFile('{# twig-cs-fixer-disable Rule #}', $ruleset));
}

/**
* @dataProvider addContentMethodsDataProvider
*/
Expand Down

0 comments on commit b8e952d

Please sign in to comment.