File: ComplexityCollectionTest.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 (108 lines) | stat: -rw-r--r-- 3,210 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?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 PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(ComplexityCollection::class)]
#[CoversClass(ComplexityCollectionIterator::class)]
#[UsesClass(Complexity::class)]
#[Small]
final class ComplexityCollectionTest extends TestCase
{
    /**
     * @var list<Complexity>
     */
    private array $array;

    protected function setUp(): void
    {
        $this->array = [
            new Complexity('Class::method', 1),
            new Complexity('function', 2),
        ];
    }

    #[TestDox('Can be created from list of Complexity objects')]
    public function testCanBeCreatedFromListOfObjects(): void
    {
        $collection = ComplexityCollection::fromList($this->array[0], $this->array[1]);

        $this->assertSame($this->array, $collection->asArray());
    }

    public function testCanBeCounted(): void
    {
        $collection = ComplexityCollection::fromList($this->array[0], $this->array[1]);

        $this->assertCount(2, $collection);
        $this->assertFalse($collection->isEmpty());
    }

    public function testCanBeIterated(): void
    {
        $array = [];

        foreach (ComplexityCollection::fromList($this->array[0], $this->array[1]) as $key => $value) {
            $array[$key] = $value;
        }

        $this->assertCount(2, $array);

        $this->assertArrayHasKey(0, $array);
        $this->assertSame($this->array[0], $array[0]);

        $this->assertArrayHasKey(1, $array);
        $this->assertSame($this->array[1], $array[1]);
    }

    public function testHasCyclomaticComplexity(): void
    {
        $collection = ComplexityCollection::fromList($this->array[0], $this->array[1]);

        $this->assertSame(3, $collection->cyclomaticComplexity());
    }

    public function testCanBeMerged(): void
    {
        $a = ComplexityCollection::fromList($this->array[0]);
        $b = ComplexityCollection::fromList($this->array[1]);

        $c = $a->mergeWith($b);

        $this->assertSame(
            [
                $this->array[0],
                $this->array[1],
            ],
            $c->asArray(),
        );
    }

    public function testCanBeFiltered(): void
    {
        $collection = ComplexityCollection::fromList($this->array[0], $this->array[1]);

        $this->assertTrue($collection->isFunction()->asArray()[0]->isFunction());
        $this->assertTrue($collection->isMethod()->asArray()[0]->isMethod());
    }

    public function testCanBeSorted(): void
    {
        $collection = ComplexityCollection::fromList($this->array[0], $this->array[1])->sortByDescendingCyclomaticComplexity();

        $this->assertSame('function', $collection->asArray()[0]->name());
        $this->assertSame('Class::method', $collection->asArray()[1]->name());
    }
}