AssertTokensTrait.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Test\Assert;
  12. use PhpCsFixer\Tokenizer\Token;
  13. use PhpCsFixer\Tokenizer\Tokens;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. */
  19. trait AssertTokensTrait
  20. {
  21. private static function assertTokens(Tokens $expectedTokens, Tokens $inputTokens)
  22. {
  23. foreach ($expectedTokens as $index => $expectedToken) {
  24. if (!isset($inputTokens[$index])) {
  25. static::fail(sprintf("The token at index %d must be:\n%s, but is not set in the input collection.", $index, $expectedToken->toJson()));
  26. }
  27. $inputToken = $inputTokens[$index];
  28. static::assertTrue(
  29. $expectedToken->equals($inputToken),
  30. sprintf("The token at index %d must be:\n%s,\ngot:\n%s.", $index, $expectedToken->toJson(), $inputToken->toJson())
  31. );
  32. $expectedTokenKind = $expectedToken->isArray() ? $expectedToken->getId() : $expectedToken->getContent();
  33. static::assertTrue(
  34. $inputTokens->isTokenKindFound($expectedTokenKind),
  35. sprintf(
  36. 'The token kind %s (%s) must be found in tokens collection.',
  37. $expectedTokenKind,
  38. \is_string($expectedTokenKind) ? $expectedTokenKind : Token::getNameForId($expectedTokenKind)
  39. )
  40. );
  41. }
  42. static::assertSame($expectedTokens->count(), $inputTokens->count(), 'Both collections must have the same length.');
  43. }
  44. }