AssertTokensTrait.php 1.8 KB

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