AssertTokensTrait.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 function assertTokens(Tokens $expectedTokens, Tokens $inputTokens)
  22. {
  23. $option = ['JSON_PRETTY_PRINT'];
  24. foreach ($expectedTokens as $index => $expectedToken) {
  25. $inputToken = $inputTokens[$index];
  26. $this->assertTrue(
  27. $expectedToken->equals($inputToken),
  28. sprintf("The token at index %d must be:\n%s,\ngot:\n%s.", $index, $expectedToken->toJson($option), $inputToken->toJson($option))
  29. );
  30. $expectedTokenKind = $expectedToken->isArray() ? $expectedToken->getId() : $expectedToken->getContent();
  31. $this->assertTrue(
  32. $inputTokens->isTokenKindFound($expectedTokenKind),
  33. sprintf(
  34. 'The token kind %s (%s) must be found in tokens collection.',
  35. $expectedTokenKind,
  36. is_string($expectedTokenKind) ? $expectedTokenKind : Token::getNameForId($expectedTokenKind)
  37. )
  38. );
  39. }
  40. $this->assertSame($expectedTokens->count(), $inputTokens->count(), 'Both collections must have the same length.');
  41. }
  42. }