-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce1f16c
commit 9e33f78
Showing
20 changed files
with
204 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace TwigCsFixer\Token; | ||
|
||
use Twig\Error\SyntaxError; | ||
use Twig\Source; | ||
|
||
/** | ||
* Interface for Tokenizer. | ||
*/ | ||
interface TokenizerInterface | ||
{ | ||
/** | ||
* @param Source $source | ||
* | ||
* @return array<int, Token> | ||
* | ||
* @throws SyntaxError | ||
*/ | ||
public function tokenize(Source $source): array; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace TwigCsFixer\Tests\Runner\Fixtures; | ||
|
||
use TwigCsFixer\Sniff\AbstractSpacingSniff; | ||
use TwigCsFixer\Token\Token; | ||
|
||
/** | ||
* This Sniff is buggy because it can't decide how to solve `,]`. | ||
*/ | ||
class BuggySniff extends AbstractSpacingSniff | ||
{ | ||
/** | ||
* @param int $tokenPosition | ||
* @param array<int, Token> $tokens | ||
* | ||
* @return int|null | ||
*/ | ||
protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?int | ||
{ | ||
$token = $tokens[$tokenPosition]; | ||
if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [']'])) { | ||
return 0; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param int $tokenPosition | ||
* @param array<int, Token> $tokens | ||
* | ||
* @return int|null | ||
*/ | ||
protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int | ||
{ | ||
$token = $tokens[$tokenPosition]; | ||
if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [','])) { | ||
return 1; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ [1,] }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace TwigCsFixer\Tests\Runner; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Twig\Environment; | ||
use Twig\Error\SyntaxError; | ||
use TwigCsFixer\Environment\StubbedEnvironment; | ||
use TwigCsFixer\Ruleset\Ruleset; | ||
use TwigCsFixer\Runner\Linter; | ||
use TwigCsFixer\Tests\Runner\Fixtures\BuggySniff; | ||
use TwigCsFixer\Token\Tokenizer; | ||
use TwigCsFixer\Token\TokenizerInterface; | ||
|
||
/** | ||
* Test for Linter. | ||
*/ | ||
class LinterTest extends TestCase | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
public function testUnreadableFilesAreReported(): void | ||
{ | ||
$env = new StubbedEnvironment(); | ||
$tokenizer = $this->createStub(TokenizerInterface::class); | ||
$ruleset = new Ruleset(); | ||
|
||
$linter = new Linter($env, $tokenizer); | ||
|
||
// Suppress the warning sent by `file_get_content` during the test. | ||
$oldErrorLevel = error_reporting(E_ALL ^ E_WARNING); | ||
$report = $linter->run([__DIR__.'/Fixtures/file_not_readable.twig'], $ruleset); | ||
error_reporting($oldErrorLevel); | ||
|
||
$messages = $report->getMessages(); | ||
self::assertCount(1, $messages); | ||
self::assertSame('Unable to read file.', $messages[0]->getMessage()); | ||
self::assertSame( | ||
sprintf('%s/Fixtures/file_not_readable.twig', __DIR__), | ||
$messages[0]->getFilename() | ||
); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testInvalidFilesAreReported(): void | ||
{ | ||
$env = $this->createStub(Environment::class); | ||
$env->method('tokenize')->willThrowException(new SyntaxError('Error.')); | ||
$tokenizer = $this->createStub(TokenizerInterface::class); | ||
$ruleset = new Ruleset(); | ||
|
||
$linter = new Linter($env, $tokenizer); | ||
|
||
$report = $linter->run([__DIR__.'/Fixtures/file.twig'], $ruleset); | ||
|
||
$messages = $report->getMessages(); | ||
self::assertCount(1, $messages); | ||
self::assertSame('File is invalid: Error.', $messages[0]->getMessage()); | ||
self::assertSame( | ||
sprintf('%s/Fixtures/file.twig', __DIR__), | ||
$messages[0]->getFilename() | ||
); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testUntokenizableFilesAreReported(): void | ||
{ | ||
$env = new StubbedEnvironment(); | ||
$tokenizer = $this->createStub(TokenizerInterface::class); | ||
$tokenizer->method('tokenize')->willThrowException(new SyntaxError('Error.')); | ||
$ruleset = new Ruleset(); | ||
|
||
$linter = new Linter($env, $tokenizer); | ||
|
||
$report = $linter->run([__DIR__.'/Fixtures/file.twig'], $ruleset); | ||
|
||
$messages = $report->getMessages(); | ||
self::assertCount(1, $messages); | ||
self::assertSame('Unable to tokenize file: Error.', $messages[0]->getMessage()); | ||
self::assertSame( | ||
sprintf('%s/Fixtures/file.twig', __DIR__), | ||
$messages[0]->getFilename() | ||
); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testBuggyRulesetCannotBeFixed(): void | ||
{ | ||
$env = new StubbedEnvironment(); | ||
$tokenizer = new Tokenizer($env); | ||
$ruleset = new Ruleset(); | ||
$ruleset->addSniff(new BuggySniff()); | ||
|
||
$linter = new Linter($env, $tokenizer); | ||
|
||
self::expectExceptionMessage(sprintf('Cannot fix the file "%s/Fixtures/file.twig".', __DIR__)); | ||
$linter->run([__DIR__.'/Fixtures/file.twig'], $ruleset, true); | ||
} | ||
} |