GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch master (3aeb29)
by Rob
03:21
created

SafeUrlsTest::testRemoveUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 16
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RattfieldNz\SafeUrls;
6
7
use RattfieldNz\SafeUrls\Tests\TestCase;
8
9
class SafeUrlsTest extends TestCase
10
{
11
    /** @var SafeUrls */
12
    private $safeUrls;
13
14
    private $urlsToTest;
15
16
    protected function setUp():void
17
    {
18
        parent::setUp();
19
20
        $this->safeUrls = new SafeUrls();
21
        $this->urlsToTest = [
22
            'https://testsafebrowsing.appspot.com/s/phishing.html',
23
            'https://testsafebrowsing.appspot.com/s/malware.html',
24
            'http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/',
25
            'http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/',
26
            'http://malware.testing.google.test/testing/malware/',
27
        ];
28
    }
29
30
    public function testAddUrls()
31
    {
32
        $this->safeUrls->add($this->urlsToTest);
33
34
        $expected = $this->urlsToTest;
35
        $actual = $this->safeUrls->getCurrentUrls(true);
36
37
        $this->assertEquals($expected, $actual);
38
    }
39
40
    public function testRemoveUrls()
41
    {
42
        $this->safeUrls->add($this->urlsToTest);
43
        $urlToRemove = 'https://testsafebrowsing.appspot.com/s/phishing.html';
44
45
        $expected = [
46
            $this->urlsToTest[1],
47
            $this->urlsToTest[2],
48
            $this->urlsToTest[3],
49
            $this->urlsToTest[4],
50
        ];
51
        $this->safeUrls->remove([$urlToRemove]);
52
53
        $actual = $this->safeUrls->getCurrentUrls(true);
54
55
        $this->assertEquals($expected, $actual);
56
    }
57
58
    public function testRemoveAllUrls()
59
    {
60
        $this->safeUrls->add($this->urlsToTest);
61
62
        $expected = [];
63
        $this->safeUrls->remove($this->urlsToTest);
64
        $actual = $this->safeUrls->getCurrentUrls(true);
65
66
        $this->assertEquals($expected, $actual);
67
    }
68
69
    public function testExecute()
70
    {
71
72
        // Only testing with returned HTTP status, as Google Safebrowsing API not working as
73
        // documented. See tests\Libraries\Curl\CurlTest testMalwareSocialEngineeringAnyPlatformUrl().
74
75
        $this->safeUrls->add($this->urlsToTest);
76
77
        $expected = '{ "status": 200, "response": { "matches": [ { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/phishing.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/malware.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" } ] } }';
78
79
        $expected = json_decode($expected, true)['status'];
80
        $actual = json_decode((string) $this->safeUrls->execute()->getResults(), true)['status'];
81
82
        $this->assertEquals($expected, $actual);
83
    }
84
85
    public function testGetResults()
86
    {
87
        $this->testExecute();
88
    }
89
90
    public function testCheck()
91
    {
92
        $expected = '{ "status": 200, "response": { "matches": [ { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/phishing.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/malware.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" } ] } }';
93
        $expected = json_decode($expected, true)['status'];
94
95
        $actual = json_decode($this->safeUrls::check($this->urlsToTest), true)['status'];
96
97
        $this->assertEquals($expected, $actual);
98
    }
99
100
    /**
101
     * @expectedException \ErrorException
102
     */
103
    public function testCheckThrowErrorException()
104
    {
105
        $this->safeUrls->add($this->urlsToTest);
106
107
        $safeUrlsMock = $this->getMockBuilder(SafeUrls::class)
108
            ->setMethods(['callStatic'])
109
            ->getMock();
110
111
        /* @scrutinizer ignore-call */
112
        $safeUrlsMock->method('callStatic')
113
            ->with(SafeUrls::class, 'check', $this->urlsToTest)
114
            ->willThrowException(new \ErrorException('Expected Exception was thrown'));
115
116
        /* @scrutinizer ignore-call */
117
        $safeUrlsMock->checkCallStatic($this->urlsToTest);
118
    }
119
120
    public function testIsDangerous()
121
    {
122
        $this->safeUrls->add($this->urlsToTest);
123
        $this->safeUrls->execute();
124
125
        $dangerousUrl = 'http://malware.testing.google.test/testing/malware/';
126
127
        // See tests\Libraries\Curl\CurlTest testMalwareSocialEngineeringAnyPlatformUrl().
128
        $expected = true;
129
        $actual = !$this->safeUrls->isDangerous($dangerousUrl);
130
131
        $this->assertEquals($expected, $actual);
132
    }
133
}
134