DocblockTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test\Util;
  11. use Psy\Util\Docblock;
  12. class DocblockTest extends \Psy\Test\TestCase
  13. {
  14. /**
  15. * @dataProvider comments
  16. */
  17. public function testDocblockParsing($comment, $body, $tags)
  18. {
  19. $reflector = $this
  20. ->getMockBuilder(\ReflectionClass::class)
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $reflector->expects($this->once())
  24. ->method('getDocComment')
  25. ->willReturn($comment);
  26. $docblock = new Docblock($reflector);
  27. $this->assertSame($body, $docblock->desc);
  28. foreach ($tags as $tag => $value) {
  29. $this->assertTrue($docblock->hasTag($tag));
  30. $this->assertEquals($value, $docblock->tag($tag));
  31. }
  32. }
  33. public function comments()
  34. {
  35. return [
  36. ['', '', []],
  37. [
  38. '/**
  39. * This is a docblock
  40. *
  41. * @throws \Exception with a description
  42. */',
  43. 'This is a docblock',
  44. [
  45. 'throws' => [['type' => '\Exception', 'desc' => 'with a description']],
  46. ],
  47. ],
  48. [
  49. '/**
  50. * This is a slightly longer docblock
  51. *
  52. * @param int $foo Is a Foo
  53. * @param string $bar With some sort of description
  54. * @param \ClassName $baz is cool too
  55. *
  56. * @return int At least it isn\'t a string
  57. */',
  58. 'This is a slightly longer docblock',
  59. [
  60. 'param' => [
  61. ['type' => 'int', 'desc' => 'Is a Foo', 'var' => '$foo'],
  62. ['type' => 'string', 'desc' => 'With some sort of description', 'var' => '$bar'],
  63. ['type' => '\ClassName', 'desc' => 'is cool too', 'var' => '$baz'],
  64. ],
  65. 'return' => [
  66. ['type' => 'int', 'desc' => 'At least it isn\'t a string'],
  67. ],
  68. ],
  69. ],
  70. [
  71. '/**
  72. * This is a docblock!
  73. *
  74. * It spans lines, too!
  75. *
  76. * @tagname plus a description
  77. *
  78. * @return
  79. */',
  80. "This is a docblock!\n\nIt spans lines, too!",
  81. [
  82. 'tagname' => ['plus a description'],
  83. ],
  84. ],
  85. [
  86. '/**
  87. * This is a single-line docblock.
  88. */',
  89. 'This is a single-line docblock.',
  90. [],
  91. ],
  92. [
  93. '/** This is a single-line docblock. */',
  94. 'This is a single-line docblock.',
  95. [],
  96. ],
  97. ];
  98. }
  99. }