ScrutinizerCheckTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 10
1
<?php
2
3
namespace SilverStripe\ModuleRatings\Tests\Check;
4
5
use GuzzleHttp\Client;
6
use PHPUnit\Framework\TestCase;
7
use SilverStripe\ModuleRatings\Check\CIPassingCheck;
8
use SilverStripe\ModuleRatings\Check\ScrutinizerCheck;
9
use SilverStripe\ModuleRatings\CheckSuite;
10
11
class ScrutinizerCheckTest extends TestCase
12
{
13
    /**
14
     * @var Client
15
     */
16
    protected $client;
17
18
    /**
19
     * @var CIPassingCheck
20
     */
21
    protected $check;
22
23
    protected function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $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

27
        $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...
28
            ->setMethods(['get', 'getBody'])
29
            ->getMock();
30
31
        // Mock get to return client, we can then mock the last call easily
32
        $this->client->method('get')->willReturn($this->client);
33
34
        $this->check = new ScrutinizerCheck($this->client);
0 ignored issues
show
Documentation Bug introduced by
It seems like new SilverStripe\ModuleR...zerCheck($this->client) of type SilverStripe\ModuleRatings\Check\ScrutinizerCheck is incompatible with the declared type SilverStripe\ModuleRatings\Check\CIPassingCheck 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...
35
36
        $suite = (new CheckSuite())->setRepositorySlug('foo/bar');
37
        $this->check->setSuite($suite);
38
    }
39
40
    public function testScrutinizerFetchFailure()
41
    {
42
        $this->client->method('getBody')->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn() 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

42
        $this->client->method('getBody')->/** @scrutinizer ignore-call */ 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...
43
        $this->check->run();
44
        $this->assertFalse($this->check->getSuccessful());
45
    }
46
47
    public function testScrutinizerNotFoundFailure()
48
    {
49
        $this->client->method('getBody')->willReturn('{
50
            "foo": "bar"
51
        }');
52
        $this->check->run();
53
        $this->assertFalse($this->check->getSuccessful());
54
    }
55
56
    /**
57
     * Check a successful API result is measured against the theshold
58
     *
59
     * @param float $quality
60
     * @param boolean $expected
61
     * @dataProvider runProvider
62
     */
63
    public function testRun($quality, $expected)
64
    {
65
        $this->client->method('getBody')->willReturn('{
66
            "applications": {
67
                "master": {
68
                    "index": {
69
                        "_embedded": {
70
                            "project": {
71
                                "metric_values": {
72
                                    "scrutinizer.quality": ' . $quality . '
73
                                }
74
                            }
75
                        }
76
                    }
77
                }
78
            }
79
        }');
80
        $this->check->run();
81
        $this->assertSame($expected, $this->check->getSuccessful());
82
    }
83
84
    /**
85
     * @return array[]
86
     */
87
    public function runProvider()
88
    {
89
        return [
90
            'lower_than_threshold' => [4.0, false],
91
            'higher_than_threshold' => [8.5, true],
92
            'equal_to_threshold' => [ScrutinizerCheck::THRESHOLD, true],
93
        ];
94
    }
95
96
    public function testScrutinizerUnsuccessful()
97
    {
98
        $this->client->method('getBody')->willReturn('{
99
            {"failed": true},
100
            {"failed": false},
101
            {"failed": true}
102
        }');
103
        $this->check->run();
104
        $this->assertFalse($this->check->getSuccessful());
105
    }
106
107
    public function testScrutinizerDefaultReturn()
108
    {
109
        $this->client->method('getBody')->willReturn('{
110
            "unrelated": "information"
111
        }');
112
        $this->check->run();
113
        $this->assertFalse($this->check->getSuccessful());
114
    }
115
}
116