1717use PhpCsFixer \Fixer \DefinedFixerInterface ;
1818use PhpCsFixer \Fixer \DeprecatedFixerInterface ;
1919use PhpCsFixer \Linter \Linter ;
20- use PhpCsFixer \Tests \ Test \ Assert \ AssertTokensTrait ;
20+ use PhpCsFixer \Tokenizer \ Token ;
2121use PhpCsFixer \Tokenizer \Tokens ;
2222use PHPUnit \Framework \TestCase ;
2323use Tests \AssertRegExpTrait ;
2828abstract class AbstractFixerTestCase extends TestCase
2929{
3030 use AssertRegExpTrait;
31- use AssertTokensTrait;
3231
3332 /** @var DefinedFixerInterface */
3433 protected $ fixer ;
@@ -130,32 +129,30 @@ final protected function doTest(string $expected, ?string $input = null, ?array
130129 throw new \InvalidArgumentException ('Expected must be different to input. ' );
131130 }
132131
132+ self ::assertNull ($ this ->lintSource ($ expected ));
133+
134+ Tokens::clearCache ();
135+ $ expectedTokens = Tokens::fromCode ($ expected );
136+
133137 if ($ input !== null ) {
134138 Tokens::clearCache ();
135- $ tokens = Tokens::fromCode ($ input );
136-
137- self ::assertTrue ($ this ->fixer ->isCandidate ($ tokens ));
139+ $ inputTokens = Tokens::fromCode ($ input );
138140
139- $ this ->fixer ->fix ( $ this -> createMock (\SplFileInfo::class), $ tokens );
141+ self :: assertTrue ( $ this ->fixer ->isCandidate ( $ inputTokens ) );
140142
141- $ tokens ->clearEmptyTokens ();
143+ $ this ->fixer ->fix ($ this ->createMock (\SplFileInfo::class), $ inputTokens );
144+ $ inputTokens ->clearEmptyTokens ();
142145
143- self ::assertSame ($ expected , $ tokens ->generateCode ());
146+ self ::assertSame ($ expected , $ inputTokens ->generateCode ());
144147
145- Tokens::clearCache ();
146- self ::assertTokens (Tokens::fromCode ($ expected ), $ tokens );
148+ self ::assertSameTokens ($ expectedTokens , $ inputTokens );
147149 }
148150
149- self :: assertNull ( $ this ->lintSource ( $ expected ) );
151+ $ this ->fixer -> fix ( $ this -> createMock (\SplFileInfo::class), $ expectedTokens );
150152
151- Tokens::clearCache ();
152- $ tokens = Tokens::fromCode ($ expected );
153+ self ::assertSame ($ expected , $ expectedTokens ->generateCode ());
153154
154- $ this ->fixer ->fix ($ this ->createMock (\SplFileInfo::class), $ tokens );
155-
156- self ::assertSame ($ expected , $ tokens ->generateCode ());
157-
158- self ::assertFalse ($ tokens ->isChanged ());
155+ self ::assertFalse ($ expectedTokens ->isChanged ());
159156 }
160157
161158 private function lintSource (string $ source ): ?string
@@ -174,4 +171,19 @@ private function lintSource(string $source): ?string
174171
175172 return null ;
176173 }
174+
175+ private static function assertSameTokens (Tokens $ expectedTokens , Tokens $ inputTokens ): void
176+ {
177+ self ::assertSame ($ expectedTokens ->count (), $ inputTokens ->count (), 'Both collections must have the same size. ' );
178+
179+ foreach ($ expectedTokens as $ index => $ expectedToken ) {
180+ /** @var Token $inputToken */
181+ $ inputToken = $ inputTokens [$ index ];
182+
183+ self ::assertTrue (
184+ $ expectedToken ->equals ($ inputToken ),
185+ \sprintf ("Token at index %d must be: \n%s, \ngot: \n%s. " , $ index , $ expectedToken ->toJson (), $ inputToken ->toJson ())
186+ );
187+ }
188+ }
177189}
0 commit comments