TokensTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Doctrine\Annotation;
  13. use PhpCsFixer\Doctrine\Annotation\Tokens;
  14. use PhpCsFixer\Tests\TestCase;
  15. use PhpCsFixer\Tokenizer\Token;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Doctrine\Annotation\Tokens
  20. */
  21. final class TokensTest extends TestCase
  22. {
  23. public function testCreateFromEmptyPhpdocComment(): void
  24. {
  25. $docComment = '/** */';
  26. $token = new Token([T_DOC_COMMENT, $docComment]);
  27. $tokens = Tokens::createFromDocComment($token);
  28. self::assertCount(1, $tokens);
  29. self::assertSame($docComment, $tokens->getCode());
  30. }
  31. /**
  32. * @dataProvider provideOffSetOtherThanTokenCases
  33. */
  34. public function testOffSetOtherThanToken(string $message, ?string $wrongType): void
  35. {
  36. $docComment = '/** */';
  37. $token = new Token([T_DOC_COMMENT, $docComment]);
  38. $tokens = Tokens::createFromDocComment($token);
  39. $this->expectException(\InvalidArgumentException::class);
  40. $this->expectExceptionMessage($message);
  41. // @phpstan-ignore-next-line as we are testing the type error
  42. $tokens[1] = $wrongType;
  43. }
  44. /**
  45. * @return iterable<array{string, null|string}>
  46. */
  47. public static function provideOffSetOtherThanTokenCases(): iterable
  48. {
  49. yield [
  50. 'Token must be an instance of PhpCsFixer\Doctrine\Annotation\Token, "null" given.',
  51. null,
  52. ];
  53. yield [
  54. 'Token must be an instance of PhpCsFixer\Doctrine\Annotation\Token, "string" given.',
  55. 'foo',
  56. ];
  57. }
  58. }