Issues (19)

tests/Check/ScrutinizerCheckTest.php (3 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\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()
24
    {
25
        parent::setUp();
26
27
        $this->client = $this->getMockBuilder(Client::class)
0 ignored issues
show
Documentation Bug introduced by Robbie Averill
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 Robbie Averill
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
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