Completed
Push — master ( 144537...5a31b6 )
by Alex
02:50
created

Psr7UriExtensionTest::testGenerateRelativePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 6
1
<?php
2
3
namespace Asmaster\TwigExtension\tests;
4
5
use PHPUnit_Framework_TestCase;
6
use Psr\Http\Message\UriInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Twig_ExtensionInterface;
9
use Asmaster\TwigExtension\Psr7UriExtension;
10
11
class Psr7UriExtensionTest extends PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var ServerRequestInterface|\PHPUnit_Framework_MockObject_MockObject
15
     */
16
    protected $request;
17
18
    public function setUp()
19
    {
20
        $this->request = $this->getMockBuilder(ServerRequestInterface::class)
21
            ->disableOriginalConstructor()
22
            ->getMock();
23
    }
24
25
    public function testExtension()
26
    {
27
        $extension = new Psr7UriExtension($this->request);
28
29
        $this->assertInstanceOf(Twig_ExtensionInterface::class, $extension);
30
        $this->assertSame('psr7_uri', $extension->getName());
31
    }
32
33
    /**
34
     * @dataProvider getFunctions()
35
     */
36
    public function testExtensionFunctions($function)
37
    {
38
        $extension = new Psr7UriExtension($this->request);
39
40
        $functions = array_map(
41
            function ($function) {
42
                return $function->getName();
43
            },
44
            $extension->getFunctions()
45
        );
46
47
        $this->assertContains($function, $functions);
48
    }
49
50
    public function getFunctions()
51
    {
52
        return [[
53
            'absolute_url',
54
            'relative_path'
55
       ]];
56
    }
57
58
    /**
59
     * @dataProvider getGenerateAbsoluteUrlData()
60
     */
61
    public function testGenerateAbsoluteUrl($path, $expected, $scheme, $host, $port, $basePath)
62
    {
63
        $request = $this->request
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Http\Message\ServerRequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
            ->expects($this->any())
65
            ->method('getUri')
66
            ->willReturn($this->createUri($scheme, $host, $port, $basePath));
67
68
        $extension = new Psr7UriExtension($this->request);
69
        $absoluteUrl = $extension->generateAbsoluteUrl($path);
70
71
        $this->assertEquals($expected, $absoluteUrl);
72
    }
73
74
    public function getGenerateAbsoluteUrlData()
75
    {
76
        return [
77
            ['/foo.png', '/foo.png', 'http', '', null, '/foo.png'],
78
            ['/foo.png', 'http://localhost/foo.png', 'http', 'localhost', null, '/'],
79
            ['/foo.png', 'http://localhost/foo.png', 'http', 'localhost', null, '/foo.png'],
80
            ['foo.png', 'http://localhost/foo/foo.png', 'http', 'localhost', null, '/foo'],
81
            ['foo.png', 'http://localhost/bar/foo.png', 'http', 'localhost', null, '/bar'],
82
            ['foo.png', 'http://localhost/foo/bar/foo.png', 'http', 'localhost', null, '/foo/bar'],
83
            ['/foo.png', 'http://localhost:8080/foo.png', 'http', 'localhost', 8080, '/'],
84
            ['/foo.png', 'https://localhost/foo.png', 'https', 'localhost', null, '/'],
85
            ['/', 'http://localhost/', 'http', 'localhost', null, '/'],
86
            ['//', '//', 'http', 'localhost', null, '/']
87
        ];
88
    }
89
90
    /**
91
     * @dataProvider getGenerateRelativePathData()
92
     */
93
    public function testGenerateRelativePath($path, $expected, $scheme, $host, $port, $basePath)
94
    {
95
        $request = $this->request
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Http\Message\ServerRequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
96
            ->expects($this->any())
97
            ->method('getUri')
98
            ->willReturn($this->createUri($scheme, $host, $port, $basePath));
99
100
        $extension = new Psr7UriExtension($this->request);
101
        $relativePath = $extension->generateRelativePath($path);
102
103
        $this->assertEquals($expected, $relativePath);
104
    }
105
106
    public function getGenerateRelativePathData()
107
    {
108
        return [
109
            ['/a/b/c/d', '', 'http', 'localhost', null, '/a/b/c/d'],
110
            ['/a/b/c/d/', '', 'http', 'localhost', null, '/a/b/c/d/'],
111
            ['/a/b/c/', './', 'http', 'localhost', null, '/a/b/c/d'],
112
            ['/a/b/c/', '../', 'http', 'localhost', null, '/a/b/c/d/'],
113
            ['/a/b/', '../', 'http', 'localhost', null, '/a/b/c/d'],
114
            ['/a/b/', '../../', 'http', 'localhost', null, '/a/b/c/d/'],
115
            ['/', '../../../', 'http', 'localhost', null, '/a/b/c/d'],
116
            ['/', '../../../../', 'http', 'localhost', null, '/a/b/c/d/'],
117
            ['/a/b/foo.png', '../foo.png', 'http', 'localhost', null, '/a/b/c/d'],
118
            ['/a/b/c/foo.png', 'foo.png', 'http', 'localhost', null, '/a/b/c/d'],
119
            ['/a/b/c/other', 'other', 'http', 'localhost', null, '/a/b/c/d'],
120
            ['/a/b/z/foo.png', '../z/foo.png', 'http', 'localhost', null, '/a/b/c/d'],
121
            ['/a/b/c/this:that', './this:that', 'http', 'localhost', null, '/a/b/c/d'],
122
            ['/a/b/c/foo/this:that', 'foo/this:that', 'http', 'localhost', null, '/a/b/c/d'],
123
            ['/', '', 'http', 'localhost', null, '/'],
124
            ['//', '//', 'http', 'localhost', null, '/']
125
        ];
126
    }
127
128
    /**
129
     * @param string $scheme
130
     * @param string $host
131
     * @param string $port
132
     * @param string $basePath
133
     *
134
     * @return UriInterface $uri New request URI to use.
135
     */
136
    protected function createUri($scheme, $host, $port, $basePath)
137
    {
138
        $uri = $this->getMockBuilder(UriInterface::class)
139
            ->disableOriginalConstructor()
140
            ->getMock();
141
142
        $uri->expects($this->any())->method('getScheme')->willReturn($scheme);
143
        $uri->expects($this->any())->method('getHost')->willReturn($host);
144
        $uri->expects($this->any())->method('getPort')->willReturn($port);
145
        $uri->expects($this->any())->method('getPath')->willReturn($basePath);
146
147
        return $uri;
148
    }
149
}
150