Completed
Push — master ( 27667a...b36312 )
by Dave
15s queued 12s
created

RandomResultsPickerTest::testPick5Results()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

112
        $this->randomNumberGenerator->/** @scrutinizer ignore-call */ 
113
                                      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 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

112
        $this->randomNumberGenerator->/** @scrutinizer ignore-call */ 
113
                                      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...
113
    }
114
}
115