Issues (25)

tests/Check/AbstractCodeCoverageCheckTest.php (4 issues)

1
<?php
2
3
namespace SilverStripe\ModuleRatings\Tests\Check;
4
5
use GuzzleHttp\Client;
6
use PHPUnit\Framework\TestCase;
7
use SilverStripe\ModuleRatings\Check\AbstractCodeCoverageCheck;
8
use SilverStripe\ModuleRatings\CheckSuite;
9
10
class AbstractCodeCoverageCheckTest extends TestCase
11
{
12
    /**
13
     * @var CheckSuite
14
     */
15
    protected $checkSuite;
16
17
    /**
18
     * @var Client
19
     */
20
    protected $client;
21
22
    /**
23
     * @var AbstractCodeCoverageCheck
24
     */
25
    protected $check;
26
27
    protected function setUp(): void
28
    {
29
        parent::setUp();
30
31
        $this->checkSuite = (new CheckSuite())->setRepositorySlug('foo/bar');
32
33
        $this->client = $this->getMockBuilder(Client::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

33
        $this->client = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(Client::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Documentation Bug introduced by
It seems like $this->getMockBuilder(Gu... 'getBody'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type GuzzleHttp\Client of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
            ->setMethods(['get', 'getBody'])
35
            ->getMock();
36
37
        $this->client->method('get')->will($this->returnSelf());
38
39
        $this->check = $this->getMockBuilder(AbstractCodeCoverageCheck::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Si...tMockForAbstractClass() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type SilverStripe\ModuleRatin...stractCodeCoverageCheck of property $check.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
            ->setConstructorArgs([$this->client])
41
            ->getMockForAbstractClass();
42
43
        $this->check->setSuite($this->checkSuite);
44
    }
45
46
    public function testGetCoverageReturnsZeroWhenSlugIsNotSet()
47
    {
48
        $this->checkSuite->setRepositorySlug(null);
49
        $this->assertSame(0, $this->check->getCoverage());
50
    }
51
52
    public function testGetCodecovCoverageFetchFailure()
53
    {
54
        $this->client->expects($this->once())->method('getBody')->willReturn(false);
0 ignored issues
show
The method method() does not exist on GuzzleHttp\Promise\PromiseInterface. ( Ignorable by Annotation )

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

54
        $this->client->expects($this->once())->/** @scrutinizer ignore-call */ method('getBody')->willReturn(false);

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...
55
        $this->assertFalse($this->check->getCodecovCoverage());
56
    }
57
58
    public function testGetCodecovCoverageNotFoundFailure()
59
    {
60
        $this->client->expects($this->once())->method('getBody')->willReturn('{
61
            "meta": {
62
                "status": 404
63
            }
64
        }');
65
        $this->assertFalse($this->check->getCodecovCoverage());
66
    }
67
68
    public function testGetCodecovCoverageReturnsResult()
69
    {
70
        $this->client->expects($this->once())->method('getBody')->willReturn('{
71
            "meta": {
72
                "status": 200
73
            },
74
            "commit": {
75
                "totals": {
76
                    "c": 85
77
                }
78
            }
79
        }');
80
        $this->assertSame(85, $this->check->getCodecovCoverage());
81
    }
82
83
    public function testGetCodecovCoverageDefaultReturn()
84
    {
85
        $this->client->expects($this->once())->method('getBody')->willReturn('{
86
            "meta": {
87
                "status": 200
88
            },
89
            "commit": {
90
                "totals": {
91
                    "wrong_key": 85
92
                }
93
            }
94
        }');
95
        $this->assertSame(0, $this->check->getCodecovCoverage());
96
    }
97
98
    public function testGetScrutinizerCoverageFetchFailure()
99
    {
100
        $this->client->expects($this->once())->method('getBody')->willReturn(false);
101
        $this->assertFalse($this->check->getScrutinizerCoverage());
102
    }
103
104
    public function testGetScrutinizerCoverageNotFoundFailure()
105
    {
106
        $this->client->expects($this->once())->method('getBody')->willReturn('{
107
            "applications": []
108
        }');
109
        $this->assertFalse($this->check->getScrutinizerCoverage());
110
    }
111
112
    public function testGetScrutinizerCoverageReturnsResult()
113
    {
114
        $this->client->expects($this->once())->method('getBody')->willReturn('{
115
            "applications": {
116
                "master": {
117
                    "index": {
118
                        "_embedded": {
119
                            "project": {
120
                                "metric_values": {
121
                                    "scrutinizer.test_coverage": 0.5
122
                                }
123
                            }
124
                        }
125
                    }
126
                }
127
            }
128
        }');
129
        $this->assertEquals(50, $this->check->getScrutinizerCoverage());
130
    }
131
132
    /**
133
     * This test represents a default Scrutinizer repository API response which will have a code quality
134
     * rating but not a code coverage rating
135
     */
136
    public function testGetScrutinizerCoverageDefaultReturn()
137
    {
138
        $this->client->expects($this->once())->method('getBody')->willReturn('{
139
            "applications": {
140
                "master": {
141
                    "index": {
142
                        "_embedded": {
143
                            "project": {
144
                                "metric_values": {
145
                                    "scrutinizer.quality": 7.423076923076923
146
                                }
147
                            }
148
                        }
149
                    }
150
                }
151
            }
152
        }');
153
        $this->assertSame(0, $this->check->getScrutinizerCoverage());
154
    }
155
}
156