File: LinesOfCodeTest.php

package info (click to toggle)
phpunit-lines-of-code 3.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,772 kB
  • sloc: php: 444; xml: 49; makefile: 17
file content (107 lines) | stat: -rw-r--r-- 3,427 bytes parent folder | download
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
<?php declare(strict_types=1);
/*
 * This file is part of sebastian/lines-of-code.
 *
 * (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\LinesOfCode;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

#[CoversClass(LinesOfCode::class)]
#[Small]
final class LinesOfCodeTest extends TestCase
{
    #[TestDox('Has Lines of Code (LOC)')]
    public function testHasLinesOfCode(): void
    {
        $this->assertSame(1, $this->linesOfCode()->linesOfCode());
    }

    #[TestDox('Has Comment Lines of Code (CLOC)')]
    public function testHasCommentLinesOfCode(): void
    {
        $this->assertSame(1, $this->linesOfCode()->commentLinesOfCode());
    }

    #[TestDox('Has Non-Comment Lines of Code (NCLOC)')]
    public function testHasNonCommentLinesOfCode(): void
    {
        $this->assertSame(0, $this->linesOfCode()->nonCommentLinesOfCode());
    }

    #[TestDox('Has Logical Lines of Code (LLOC)')]
    public function testHasLogicalLinesOfCode(): void
    {
        $this->assertSame(0, $this->linesOfCode()->logicalLinesOfCode());
    }

    public function testLinesOfCodeCannotBeNegative(): void
    {
        $this->expectException(NegativeValueException::class);
        $this->expectExceptionMessage('$linesOfCode must not be negative');

        /** @phpstan-ignore argument.type */
        new LinesOfCode(-1, 0, 0, 0);
    }

    public function testCommentLinesOfCodeCannotBeNegative(): void
    {
        $this->expectException(NegativeValueException::class);
        $this->expectExceptionMessage('$commentLinesOfCode must not be negative');

        /** @phpstan-ignore argument.type */
        new LinesOfCode(0, -1, 0, 0);
    }

    public function testNonCommentLinesOfCodeCannotBeNegative(): void
    {
        $this->expectException(NegativeValueException::class);
        $this->expectExceptionMessage('$nonCommentLinesOfCode must not be negative');

        /** @phpstan-ignore argument.type */
        new LinesOfCode(0, 0, -1, 0);
    }

    public function testLogicalLinesOfCodeCannotBeNegative(): void
    {
        $this->expectException(NegativeValueException::class);
        $this->expectExceptionMessage('$logicalLinesOfCode must not be negative');

        /** @phpstan-ignore argument.type */
        new LinesOfCode(0, 0, 0, -1);
    }

    #[TestDox('Lines of Code = Comment Lines of Code + Non-Comment Lines of Code')]
    public function testNumbersHaveToMakeSense(): void
    {
        $this->expectException(IllogicalValuesException::class);
        $this->expectExceptionMessage('$linesOfCode !== $commentLinesOfCode + $nonCommentLinesOfCode');

        new LinesOfCode(1, 2, 2, 0);
    }

    public function testTwoInstancesCanBeAdded(): void
    {
        $a = new LinesOfCode(2, 1, 1, 1);
        $b = new LinesOfCode(4, 2, 2, 2);

        $sum = $a->plus($b);

        $this->assertSame(6, $sum->linesOfCode());
        $this->assertSame(3, $sum->commentLinesOfCode());
        $this->assertSame(3, $sum->nonCommentLinesOfCode());
        $this->assertSame(3, $sum->logicalLinesOfCode());
    }

    private function linesOfCode(): LinesOfCode
    {
        return new LinesOfCode(1, 1, 0, 0);
    }
}