File: CalculatorTest.php

package info (click to toggle)
phpunit-complexity 4.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,808 kB
  • sloc: php: 800; xml: 49; makefile: 14
file content (79 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php declare(strict_types=1);
/*
 * This file is part of sebastian/complexity.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace SebastianBergmann\Complexity;

use function file_get_contents;
use PhpParser\ParserFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Medium;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Calculator::class)]
#[CoversClass(ComplexityCalculatingVisitor::class)]
#[CoversClass(CyclomaticComplexityCalculatingVisitor::class)]
#[UsesClass(Complexity::class)]
#[UsesClass(ComplexityCollection::class)]
#[UsesClass(ComplexityCollectionIterator::class)]
#[Medium]
final class CalculatorTest extends TestCase
{
    public function testCalculatesCyclomaticComplexityOfClassMethodInSourceFile(): void
    {
        $result = (new Calculator)->calculateForSourceFile(__DIR__ . '/../_fixture/ExampleClass.php')->asArray();

        $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleClass::method', $result[0]->name());
        $this->assertSame(14, $result[0]->cyclomaticComplexity());
    }

    public function testCalculatesCyclomaticComplexityOfTraitMethodInSourceFile(): void
    {
        $result = (new Calculator)->calculateForSourceFile(__DIR__ . '/../_fixture/ExampleTrait.php')->asArray();

        $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleTrait::method', $result[0]->name());
        $this->assertSame(14, $result[0]->cyclomaticComplexity());
    }

    public function testCalculatesCyclomaticComplexityOfFunctionInSourceFile(): void
    {
        $result = (new Calculator)->calculateForSourceFile(__DIR__ . '/../_fixture/example_function.php')->asArray();

        $this->assertSame('SebastianBergmann\Complexity\TestFixture\example_function', $result[0]->name());
        $this->assertSame(16, $result[0]->cyclomaticComplexity());
    }

    public function testCalculatesCyclomaticComplexityInSourceString(): void
    {
        $source = file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php');

        $this->assertIsString($source);

        $result = (new Calculator)->calculateForSourceString($source)->asArray();

        $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleClass::method', $result[0]->name());
        $this->assertSame(14, $result[0]->cyclomaticComplexity());
    }

    public function testCalculatesCyclomaticComplexityInAbstractSyntaxTree(): void
    {
        $source = file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php');

        $this->assertIsString($source);

        $nodes = (new ParserFactory)->createForHostVersion()->parse($source);

        $this->assertNotNull($nodes);

        $result = (new Calculator)->calculateForAbstractSyntaxTree($nodes)->asArray();

        $this->assertSame('SebastianBergmann\Complexity\TestFixture\ExampleClass::method', $result[0]->name());
        $this->assertSame(14, $result[0]->cyclomaticComplexity());
    }
}