AssertTokensTrait.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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('The token kind %s must be found in fixed tokens collection.', $expectedTokenKind)
  34. );
  35. }
  36. $this->assertSame($expectedTokens->count(), $inputTokens->count(), 'Both collections must have the same length.');
  37. }
  38. }