* Dariusz Rumiński * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Test\Assert; use PhpCsFixer\Tokenizer\Token; use PhpCsFixer\Tokenizer\Tokens; /** * @author Dariusz Rumiński * * @internal */ trait AssertTokensTrait { protected static function assertTokens(Tokens $expectedTokens, Tokens $inputTokens): void { foreach ($expectedTokens as $index => $expectedToken) { if (!isset($inputTokens[$index])) { static::fail(\sprintf("The token at index %d must be:\n%s, but is not set in the input collection.", $index, $expectedToken->toJson())); } $inputToken = $inputTokens[$index]; self::assertTrue( $expectedToken->equals($inputToken), \sprintf("The token at index %d must be:\n%s,\ngot:\n%s.", $index, $expectedToken->toJson(), $inputToken->toJson()) ); $expectedTokenKind = $expectedToken->isArray() ? $expectedToken->getId() : $expectedToken->getContent(); self::assertTrue( $inputTokens->isTokenKindFound($expectedTokenKind), \sprintf( 'The token kind %s (%s) must be found in tokens collection.', $expectedTokenKind, \is_string($expectedTokenKind) ? $expectedTokenKind : Token::getNameForId($expectedTokenKind) ) ); } self::assertSameSize($expectedTokens, $inputTokens, 'Both collections must have the same length.'); } }