RandomResultsPickerTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 95
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addIssue() 0 17 1
A setUp() 0 6 1
A testPickWhenFewerThan5Results() 0 20 1
A testPick5Results() 0 26 1
A setRandomNumbers() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Core\RandomResultsPicker;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\LineNumber;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Location;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName;
11
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Severity;
12
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Type;
13
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\RandomResultsPicker\RandomResultsPicker;
14
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResult;
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsBuilder;
16
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\RandomNumberGenerator;
17
use PHPUnit\Framework\MockObject\Stub;
18
use PHPUnit\Framework\TestCase;
19
20
final class RandomResultsPickerTest extends TestCase
21
{
22
    /**
23
     * @var ProjectRoot
24
     */
25
    private $projectRoot;
26
    /**
27
     * @var RandomResultsPicker
28
     */
29
    private $randomResultsPicker;
30
    /**
31
     * @var RandomNumberGenerator&Stub
32
     */
33
    private $randomNumberGenerator;
34
35
    protected function setUp(): void
36
    {
37
        $this->projectRoot = ProjectRoot::fromCurrentWorkingDirectory('/');
38
39
        $this->randomNumberGenerator = $this->createStub(RandomNumberGenerator::class);
40
        $this->randomResultsPicker = new RandomResultsPicker($this->randomNumberGenerator);
41
    }
42
43
    public function testPick5Results(): void
44
    {
45
        $issuesBuilder = new AnalysisResultsBuilder();
46
        $issue1 = $this->addIssue($issuesBuilder, 'a');
47
        $issue2 = $this->addIssue($issuesBuilder, 'b');
48
        $this->addIssue($issuesBuilder, 'c');
49
        $issue4 = $this->addIssue($issuesBuilder, 'd');
50
        $this->addIssue($issuesBuilder, 'e');
51
        $issue6 = $this->addIssue($issuesBuilder, 'f');
52
        $issue7 = $this->addIssue($issuesBuilder, 'g');
53
54
        $this->setRandomNumbers(5, 1, 0, 3, 1);
55
56
        $pickedIssues = $this->randomResultsPicker->getRandomResultsToFix($issuesBuilder->build());
57
58
        $expected = [
59
            $issue1,
60
            $issue2,
61
            $issue4,
62
            $issue6,
63
            $issue7,
64
        ];
65
66
        $this->assertEquals($expected, $pickedIssues->getAnalysisResults());
67
        $this->assertSame(5, $pickedIssues->getCount());
68
        $this->assertFalse($pickedIssues->hasNoIssues());
69
    }
70
71
    public function testPickWhenFewerThan5Results(): void
72
    {
73
        $issuesBuilder = new AnalysisResultsBuilder();
74
        $issue1 = $this->addIssue($issuesBuilder, 'a');
75
        $issue2 = $this->addIssue($issuesBuilder, 'b');
76
        $issue3 = $this->addIssue($issuesBuilder, 'c');
77
78
        $this->setRandomNumbers(2, 0, 0);
79
80
        $pickedIssues = $this->randomResultsPicker->getRandomResultsToFix($issuesBuilder->build());
81
82
        $expected = [
83
            $issue1,
84
            $issue2,
85
            $issue3,
86
        ];
87
88
        $this->assertEquals($expected, $pickedIssues->getAnalysisResults());
89
        $this->assertSame(3, $pickedIssues->getCount());
90
        $this->assertFalse($pickedIssues->hasNoIssues());
91
    }
92
93
    private function addIssue(AnalysisResultsBuilder $issuesBuilder, string $string): AnalysisResult
94
    {
95
        $analysisResult = new AnalysisResult(
96
            Location::fromRelativeFileName(
97
                new RelativeFileName($string),
98
                $this->projectRoot,
99
                new LineNumber(10),
100
            ),
101
            new Type("Type-{$string}"),
102
            $string,
103
            [],
104
            Severity::error(),
105
        );
106
107
        $issuesBuilder->addAnalysisResult($analysisResult);
108
109
        return $analysisResult;
110
    }
111
112
    private function setRandomNumbers(int ...$numbers): void
113
    {
114
        $this->randomNumberGenerator->method('getRandomNumber')->willReturnOnConsecutiveCalls(...$numbers);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\Stub. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        $this->randomNumberGenerator->/** @scrutinizer ignore-call */ 
115
                                      method('getRandomNumber')->willReturnOnConsecutiveCalls(...$numbers);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method method() does not exist on DaveLiddament\StaticAnal...s\RandomNumberGenerator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        $this->randomNumberGenerator->/** @scrutinizer ignore-call */ 
115
                                      method('getRandomNumber')->willReturnOnConsecutiveCalls(...$numbers);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
    }
116
}
117