Completed
Push — master ( 7bdd8d...a18ca2 )
by Oleg
04:39
created

CompareUrlTest::testPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Malezha\Menu\Tests;
3
4
use Illuminate\Routing\UrlGenerator;
5
use Malezha\Menu\Support\ComparativeUrl;
6
7
class CompareUrlTest extends TestCase
8
{
9
    protected function makeComparativeUrl($currentUrl)
10
    {
11
        $mock = $this->createMock(UrlGenerator::class);
12
        $mock->method('current')->willReturn(url($currentUrl));
13
        $mock->method('to')->willReturnCallback('url');
14
15
        return new ComparativeUrl($mock, config('menu.skippedPaths'));
16
    }
17
    
18
    public function testSkipped()
19
    {
20
        $compare = $this->makeComparativeUrl(url('/'));
21
        foreach (config('menu.skippedPaths') as $value) {
22
            $this->assertFalse($compare->isEquals($value));
23
        }
24
    }
25
26
    public function testEquals()
27
    {
28
        $stub = [
29
            'http://username:password@hostname/path?arg=value#anchor',
30
            '//www.example.com/path?googleguy=googley',
31
            'http://usr:[email protected]:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment',
32
            url('/'),
33
            '/',
34
        ];
35
        
36
        foreach ($stub as $value) {
37
            $compare = $this->makeComparativeUrl($value);
38
            $this->assertTrue($compare->isEquals($value));
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 36 can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Support\ComparativeUrl::isEquals() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39
        }
40
    }
41
42
    public function testPaths()
43
    {
44
        $index = $this->makeComparativeUrl(url('/'));
45
        $this->assertFalse($index->isEquals(url('profile')));
0 ignored issues
show
Bug introduced by
It seems like url('profile') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Malezha\Menu\Support\ComparativeUrl::isEquals() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46
47
        $profile = $this->makeComparativeUrl(url('profile'));
48
        $this->assertFalse($profile->isEquals(url('/')));
49
    }
50
}