LineCountingVisitorTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/lines-of-code.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\LinesOfCode;
  11. use function file_get_contents;
  12. use PhpParser\NodeTraverser;
  13. use PhpParser\ParserFactory;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * @covers \SebastianBergmann\LinesOfCode\LineCountingVisitor
  17. *
  18. * @uses \SebastianBergmann\LinesOfCode\LinesOfCode
  19. *
  20. * @small
  21. */
  22. final class LineCountingVisitorTest extends TestCase
  23. {
  24. /**
  25. * @dataProvider provideData
  26. */
  27. public function testCountsLinesOfCodeInAbstractSyntaxTree(string $sourceFile, int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode, int $logicalLinesOfCode): void
  28. {
  29. $nodes = (new ParserFactory)->createForHostVersion()->parse(
  30. file_get_contents($sourceFile)
  31. );
  32. $traverser = new NodeTraverser;
  33. $visitor = new LineCountingVisitor($linesOfCode);
  34. $traverser->addVisitor($visitor);
  35. /* @noinspection UnusedFunctionResultInspection */
  36. $traverser->traverse($nodes);
  37. $this->assertSame($linesOfCode, $visitor->result()->linesOfCode());
  38. $this->assertSame($commentLinesOfCode, $visitor->result()->commentLinesOfCode());
  39. $this->assertSame($nonCommentLinesOfCode, $visitor->result()->nonCommentLinesOfCode());
  40. $this->assertSame($logicalLinesOfCode, $visitor->result()->logicalLinesOfCode());
  41. }
  42. public function provideData(): array
  43. {
  44. return [
  45. [
  46. __DIR__ . '/../_fixture/ExampleClass.php',
  47. 51,
  48. 5,
  49. 46,
  50. 13,
  51. ],
  52. [
  53. __DIR__ . '/../_fixture/source_with_ignore.php',
  54. 44,
  55. 9,
  56. 35,
  57. 4,
  58. ],
  59. [
  60. __DIR__ . '/../_fixture/source_without_newline.php',
  61. 1,
  62. 1,
  63. 0,
  64. 0,
  65. ],
  66. ];
  67. }
  68. }