DocblockTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\analysis;
  9. use lithium\analysis\Docblock;
  10. use lithium\analysis\Inspector;
  11. class DocblockTest extends \lithium\test\Unit {
  12. public function testComment() {
  13. $expected = array(
  14. 'description' => '',
  15. 'text' => null,
  16. 'tags' => array()
  17. );
  18. $result = Docblock::comment('');
  19. $this->assertEqual($expected, $result);
  20. $comment = "/**\n * Lithium is cool\n * @foo bar\n * @baz qux\n */";
  21. $expected = array('description' => 'Lithium is cool', 'text' => '', 'tags' => array());
  22. $result = Docblock::comment($comment);
  23. $this->assertEqual($expected, $result);
  24. Docblock::$tags[] = 'foo';
  25. Docblock::$tags[] = 'baz';
  26. $expected['tags'] = array('foo' => 'bar', 'baz' => 'qux');
  27. $result = Docblock::comment($comment);
  28. $this->assertEqual($expected, $result);
  29. $comment = "/**\n * Lithium is cool\n *\n * Very cool\n * @foo bar\n * @baz qux\n */";
  30. $expected = array(
  31. 'description' => 'Lithium is cool',
  32. 'text' => 'Very cool',
  33. 'tags' => array('foo' => 'bar', 'baz' => 'qux')
  34. );
  35. $result = Docblock::comment($comment);
  36. $this->assertEqual($expected, $result);
  37. }
  38. public function testParamTag() {
  39. $comment = "/**\n * Lithium is cool\n * @param string \$str Some string\n */";
  40. $expected = array(
  41. 'description' => 'Lithium is cool',
  42. 'text' => '',
  43. 'tags' => array('params' => array(
  44. '$str' => array('type' => 'string', 'text' => 'Some string')
  45. ))
  46. );
  47. $result = Docblock::comment($comment);
  48. $this->assertEqual($expected, $result);
  49. }
  50. /**
  51. * This is a short description.
  52. *
  53. * This is a longer description...
  54. * That contains
  55. * multiple lines
  56. *
  57. * @important This is a tag that spans a single line.
  58. * @discuss This is a tag that
  59. * spans
  60. * several
  61. * lines.
  62. * @discuss The second discussion item
  63. * @link http://example.com/
  64. * @see lithium\analysis\Docblock
  65. * @return void This tag contains a [email protected].
  66. */
  67. public function testTagParsing() {
  68. $info = Inspector::info(__METHOD__ . '()');
  69. $result = Docblock::comment($info['comment']);
  70. $this->assertEqual('This is a short description.', $result['description']);
  71. $expected = "This is a longer description...\nThat contains\nmultiple lines";
  72. $this->assertEqual($expected, $result['text']);
  73. $tags = $result['tags'];
  74. $expected = array('important', 'discuss', 'link', 'see', 'return');
  75. $this->assertEqual($expected, array_keys($tags));
  76. $this->assertEqual("This is a tag that\n spans\nseveral\nlines.", $tags['discuss'][0]);
  77. $this->assertEqual("The second discussion item", $tags['discuss'][1]);
  78. $this->assertEqual('void This tag contains a [email protected].', $tags['return']);
  79. $this->assertEqual(array(), Docblock::tags(null));
  80. $this->assertEqual(array('params' => array()), Docblock::tags("Foobar\n\n@param string"));
  81. }
  82. public function testDocblockNewlineHandling() {
  83. $doc = " * This line as well as the line below it,\r\n";
  84. $doc .= " * are part of the description.\r\n *\r\n * This line isn't.";
  85. $result = Docblock::comment($doc);
  86. $description = "This line as well as the line below it,\nare part of the description.";
  87. $this->assertEqual($description, $result['description']);
  88. $this->assertEqual('This line isn\'t.', $result['text']);
  89. }
  90. /**
  91. * This docblock has an extra * in the closing element.
  92. *
  93. */
  94. public function testBadlyClosedDocblock() {
  95. $info = Inspector::info(__METHOD__ . '()');
  96. $description = 'This docblock has an extra * in the closing element.';
  97. $this->assertEqual($description, $info['description']);
  98. $this->assertEqual('', $info['text']);
  99. }
  100. }
  101. ?>