Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32c7362

Browse files
keradusSpacePossum
authored andcommitted
AssertTokens Trait
1 parent c9700cf commit 32c7362

4 files changed

Lines changed: 56 additions & 24 deletions

File tree

src/Test/AbstractFixerTestCase.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PhpCsFixer\Linter\Linter;
1919
use PhpCsFixer\Linter\LinterInterface;
2020
use PhpCsFixer\RuleSet;
21+
use PhpCsFixer\Test\Assert\AssertTokensTrait;
2122
use PhpCsFixer\Tokenizer\Token;
2223
use PhpCsFixer\Tokenizer\Tokens;
2324
use PhpCsFixer\Utils;
@@ -29,6 +30,8 @@
2930
*/
3031
abstract class AbstractFixerTestCase extends TestCase
3132
{
33+
use AssertTokensTrait;
34+
3235
/**
3336
* @var LinterInterface
3437
*/
@@ -188,29 +191,6 @@ protected function lintSource($source)
188191
}
189192
}
190193

191-
private function assertTokens(Tokens $expectedTokens, Tokens $inputTokens)
192-
{
193-
foreach ($expectedTokens as $index => $expectedToken) {
194-
$inputToken = $inputTokens[$index];
195-
$option = ['JSON_PRETTY_PRINT'];
196-
$this->assertTrue(
197-
$expectedToken->equals($inputToken),
198-
sprintf("The token at index %d must be:\n%s,\ngot:\n%s.", $index, $expectedToken->toJson($option), $inputToken->toJson($option))
199-
);
200-
}
201-
202-
$this->assertSame($expectedTokens->count(), $inputTokens->count(), 'The collection must have the same length than the expected one.');
203-
204-
$foundTokenKinds = array_keys(AccessibleObject::create($expectedTokens)->foundTokenKinds);
205-
206-
foreach ($foundTokenKinds as $tokenKind) {
207-
$this->assertTrue(
208-
$inputTokens->isTokenKindFound($tokenKind),
209-
sprintf('The token kind %s must be found in fixed tokens collection.', $tokenKind)
210-
);
211-
}
212-
}
213-
214194
/**
215195
* @return LinterInterface
216196
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHP CS Fixer.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
* Dariusz Rumiński <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace PhpCsFixer\Test\Assert;
14+
15+
use PhpCsFixer\Test\AccessibleObject;
16+
use PhpCsFixer\Tokenizer\Token;
17+
use PhpCsFixer\Tokenizer\Tokens;
18+
19+
/**
20+
* @author Dariusz Rumiński <[email protected]>
21+
*
22+
* @internal
23+
*/
24+
trait AssertTokensTrait
25+
{
26+
private function assertTokens(Tokens $expectedTokens, Tokens $inputTokens)
27+
{
28+
foreach ($expectedTokens as $index => $expectedToken) {
29+
$inputToken = $inputTokens[$index];
30+
$option = ['JSON_PRETTY_PRINT'];
31+
$this->assertTrue(
32+
$expectedToken->equals($inputToken),
33+
sprintf("The token at index %d must be:\n%s,\ngot:\n%s.", $index, $expectedToken->toJson($option), $inputToken->toJson($option))
34+
);
35+
}
36+
37+
$this->assertSame($expectedTokens->count(), $inputTokens->count(), 'The collection must have the same length than the expected one.');
38+
39+
$foundTokenKinds = array_keys(AccessibleObject::create($expectedTokens)->foundTokenKinds);
40+
41+
foreach ($foundTokenKinds as $tokenKind) {
42+
$this->assertTrue(
43+
$inputTokens->isTokenKindFound($tokenKind),
44+
sprintf('The token kind %s must be found in fixed tokens collection.', $tokenKind)
45+
);
46+
}
47+
}
48+
}

tests/AutoReview/ProjectCodeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ final class ProjectCodeTest extends TestCase
6565
\PhpCsFixer\Runner\FileFilterIterator::class,
6666
\PhpCsFixer\Runner\FileLintingIterator::class,
6767
\PhpCsFixer\StdinFileInfo::class,
68+
\PhpCsFixer\Test\Assert\AssertTokensTrait::class,
6869
\PhpCsFixer\Test\IntegrationCaseFactory::class,
6970
\PhpCsFixer\Tokenizer\Transformers::class,
7071
];

tests/Tokenizer/TokensTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace PhpCsFixer\Tests\Tokenizer;
1414

15+
use PhpCsFixer\Test\Assert\AssertTokensTrait;
1516
use PhpCsFixer\Tokenizer\Token;
1617
use PhpCsFixer\Tokenizer\Tokens;
1718
use PHPUnit\Framework\TestCase;
@@ -26,6 +27,8 @@
2627
*/
2728
final class TokensTest extends TestCase
2829
{
30+
use AssertTokensTrait;
31+
2932
public function testReadFromCacheAfterClearing()
3033
{
3134
$code = '<?php echo 1;';
@@ -873,7 +876,7 @@ public function testEnsureWhitespaceAtIndex($expected, $input, $index, $offset,
873876
$tokens = Tokens::fromCode($input);
874877
$tokens->ensureWhitespaceAtIndex($index, $offset, $whiteSpace);
875878

876-
$this->assertSame($expected, $tokens->generateCode());
879+
$this->assertTokens(Tokens::fromCode($expected), $tokens);
877880
}
878881

879882
public function provideEnsureWhitespaceAtIndexCases()

0 commit comments

Comments
 (0)