CalculatorTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/complexity.
  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\Complexity;
  11. use function file_get_contents;
  12. use PhpParser\ParserFactory;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * @covers \SebastianBergmann\Complexity\Calculator
  16. * @covers \SebastianBergmann\Complexity\ComplexityCalculatingVisitor
  17. * @covers \SebastianBergmann\Complexity\CyclomaticComplexityCalculatingVisitor
  18. *
  19. * @uses \SebastianBergmann\Complexity\Complexity
  20. * @uses \SebastianBergmann\Complexity\ComplexityCollection
  21. * @uses \SebastianBergmann\Complexity\ComplexityCollectionIterator
  22. *
  23. * @medium
  24. */
  25. final class CalculatorTest extends TestCase
  26. {
  27. public function testCalculatesCyclomaticComplexityOfClassMethodInSourceFile(): void
  28. {
  29. $result = (new Calculator)->calculateForSourceFile(__DIR__ . '/../_fixture/ExampleClass.php')->asArray();
  30. $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleClass::method', $result[0]->name());
  31. $this->assertSame(14, $result[0]->cyclomaticComplexity());
  32. }
  33. public function testCalculatesCyclomaticComplexityOfTraitMethodInSourceFile(): void
  34. {
  35. $result = (new Calculator)->calculateForSourceFile(__DIR__ . '/../_fixture/ExampleTrait.php')->asArray();
  36. $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleTrait::method', $result[0]->name());
  37. $this->assertSame(14, $result[0]->cyclomaticComplexity());
  38. }
  39. public function testCalculatesCyclomaticComplexityOfFunctionInSourceFile(): void
  40. {
  41. $result = (new Calculator)->calculateForSourceFile(__DIR__ . '/../_fixture/example_function.php')->asArray();
  42. $this->assertSame('SebastianBergmann\Complexity\TestFixture\example_function', $result[0]->name());
  43. $this->assertSame(14, $result[0]->cyclomaticComplexity());
  44. }
  45. public function testCalculatesCyclomaticComplexityInSourceString(): void
  46. {
  47. $result = (new Calculator)->calculateForSourceString(file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php'))->asArray();
  48. $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleClass::method', $result[0]->name());
  49. $this->assertSame(14, $result[0]->cyclomaticComplexity());
  50. }
  51. public function testCalculatesCyclomaticComplexityInAbstractSyntaxTree(): void
  52. {
  53. $nodes = (new ParserFactory)->createForHostVersion()->parse(file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php'));
  54. assert($nodes !== null);
  55. $result = (new Calculator)->calculateForAbstractSyntaxTree($nodes)->asArray();
  56. $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleClass::method', $result[0]->name());
  57. $this->assertSame(14, $result[0]->cyclomaticComplexity());
  58. }
  59. }