Test Failed
Push — master ( 0ee32e...41c938 )
by Dan
07:10
created

RouterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Tests\Router;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Ds\Router\Interfaces\AdaptorInterface;
7
use Ds\Router\Interfaces\RouteCollectionInterface;
8
use Ds\Router\Interfaces\RouterInterface;
9
use Ds\Router\RouteCollection;
10
use Ds\Router\Router;
11
12
/**
13
 * Class RouterTest
14
 * @package Tests\Router
15
 */
16
class RouterTest extends \PHPUnit_Framework_TestCase
17
{
18
19
    /**
20
     * @var \PHPUnit_Framework_MockObject_MockObject|AdaptorInterface
21
     */
22
    public $adaptor;
23
24
    /**
25
     * @var \PHPUnit_Framework_MockObject_MockObject|RouteCollectionInterface
26
     */
27
    public $collection;
28
29
    /**
30
     * @var RouterInterface
31
     */
32
    public $router;
33
34
35
    /**
36
     *
37
     */
38
    public function setUp()
39
    {
40
        $this->adaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock();
41
        $this->collection = $this->getMockBuilder(RouteCollectionInterface::class)->getMock();
42
43
        $this->router = new Router(
44
            $this->adaptor,
45
            $this->collection
46
        );
47
    }
48
49
    /**
50
     *
51
     */
52 View Code Duplication
    public function testGetRouterResponseFromPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $method = 'GET';
55
        $path = '/mypath';
56
        $actual = 'my-handler';
57
58
        $this->adaptor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\AdaptorInterface.

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...
59
            ->method('match')
60
            ->with($this->collection, $method, $path)
61
            ->willReturn($actual);
62
63
        $expected = $this->router->getRouterResponseFromPath($method, $path);
64
        $this->assertEquals($expected, $actual);
65
    }
66
67
    /**
68
     *
69
     */
70 View Code Duplication
    public function testGetRouterResponse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $request =  $this->getMockBuilder(ServerRequestInterface::class)->getMock();
73
74
        $method = 'GET';
75
        $path = '/path';
76
        $actual = 'my-handler';
77
78
        $this->adaptor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\AdaptorInterface.

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...
79
            ->method('match')
80
            ->with($this->collection, $method, $path)
81
            ->willReturn($actual);
82
83
        $expected = $this->router->getRouterResponse($request);
84
        $this->assertEquals($expected, $actual);
85
    }
86
87
88
    /**
89
     *
90
     */
91
    public function testWithAdaptor()
92
    {
93
        $newAdaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock();
94
        $newRouter = $this->router->withAdaptor($newAdaptor);
95
        $this->assertNotSame($this->adaptor, $newRouter->getAdaptor());
96
    }
97
98
    /**
99
     *
100
     */
101
    public function testGetAdaptor()
102
    {
103
        $expected = $this->adaptor;
104
        $actual = $this->router->getAdaptor();
105
        $this->assertEquals($expected, $actual);
106
    }
107
108
    /**
109
     *
110
     */
111
    public function testWithCollection()
112
    {
113
        $routes = $this->getMockBuilder(RouteCollectionInterface::class)->getMock();
114
        $this->router = $this->router->withCollection($routes);
115
116
        $this->assertSame(
117
            $routes,
118
            Helpers\Reflection::getProperty(Router::class, 'collection', $this->router)
119
        );
120
    }
121
122
    /**
123
     *
124
     */
125
    public function testGetCollection()
126
    {
127
        $expected = $this->collection;
128
        $actual = $this->router->getCollection();
129
        $this->assertEquals($expected, $actual);
130
    }
131
132
    /**
133
     *
134
     */
135
    public function testGetCollectionCached()
136
    {
137
        $this->adaptor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\AdaptorInterface.

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...
138
            ->method('isCached')
139
            ->willReturn(true);
140
141
        $this->adaptor->expects($this->once())
142
            ->method('getCachedRoutes')
143
            ->willReturn($this->collection);
144
145
        $collection = $this->router->getCollection();
146
        $this->assertSame($this->collection, $collection);
147
    }
148
149
150
    /**
151
     *
152
     */
153
    public function testMergeCollection()
154
    {
155
        $collection = new RouteCollection();
156
157
        $this->collection->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouteCollectionInterface.

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...
158
            ->method('mergeCollection')
159
            ->with($this->equalTo($collection))
160
            ->willReturn($collection);
161
162
        $newCollection = $this->router->mergeCollection($collection);
163
        $this->assertNotSame($this->collection, $newCollection);
164
    }
165
166
    /**
167
     *
168
     */
169
    public function testIsCached()
170
    {
171
        $this->adaptor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\AdaptorInterface.

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...
172
            ->method('isCached')
173
            ->willReturn(true);
174
        $this->router->isCached();
175
    }
176
}
177