TagTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\DocBlock;
  13. use PhpCsFixer\DocBlock\Line;
  14. use PhpCsFixer\DocBlock\Tag;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author Graham Campbell <hello@gjcampbell.co.uk>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\DocBlock\Tag
  22. */
  23. final class TagTest extends TestCase
  24. {
  25. /**
  26. * @dataProvider provideNameCases
  27. */
  28. public function testName(string $expected, string $new, string $input): void
  29. {
  30. $tag = new Tag(new Line($input));
  31. self::assertSame($expected, $tag->getName());
  32. if ('other' === $expected) {
  33. $this->expectException(\RuntimeException::class);
  34. $this->expectExceptionMessage('Cannot set name on unknown tag');
  35. }
  36. $tag->setName($new);
  37. self::assertSame($new, $tag->getName());
  38. }
  39. /**
  40. * @return iterable<array{string, string, string}>
  41. */
  42. public static function provideNameCases(): iterable
  43. {
  44. yield ['param', 'var', ' * @param Foo $foo'];
  45. yield ['return', 'type', '* @return false'];
  46. yield ['thRoWs', 'throws', '*@thRoWs \Exception'];
  47. yield ['THROWSSS', 'throws', "\t@THROWSSS\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n"];
  48. yield ['other', 'foobar', ' * @\Foo\Bar(baz = 123)'];
  49. yield ['expectedException', 'baz', ' * @expectedException Exception'];
  50. yield ['property-read', 'property-write', ' * @property-read integer $daysInMonth number of days in the given month'];
  51. yield ['method', 'foo', ' * @method'];
  52. yield ['method', 'hi', ' * @method string getString()'];
  53. yield ['other', 'hello', ' * @method("GET")'];
  54. }
  55. /**
  56. * @dataProvider provideValidCases
  57. */
  58. public function testValid(bool $expected, string $input): void
  59. {
  60. $tag = new Tag(new Line($input));
  61. self::assertSame($expected, $tag->valid());
  62. }
  63. /**
  64. * @return iterable<array{bool, string}>
  65. */
  66. public static function provideValidCases(): iterable
  67. {
  68. yield [true, ' * @param Foo $foo'];
  69. yield [true, '* @return false'];
  70. yield [true, '*@throws \Exception'];
  71. yield [true, ' * @method'];
  72. yield [true, ' * @method string getString()'];
  73. yield [true, ' * @property-read integer $daysInMonth number of days in the given month'];
  74. yield [false, ' * @method("GET")'];
  75. yield [false, '*@thRoWs \InvalidArgumentException'];
  76. yield [false, "\t@THROWSSS\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n"];
  77. yield [false, ' * @\Foo\Bar(baz = 123)'];
  78. yield [false, ' * @expectedException Exception'];
  79. }
  80. }