RouterTest::testGetRouterResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the DS Framework.
4
 *
5
 * (c) Dan Smith <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Tests\Ds\Router;
11
12
use Ds\Router\Interfaces\AdaptorInterface;
13
use Ds\Router\Interfaces\RouteCollectionInterface;
14
use Ds\Router\Interfaces\RouterInterface;
15
use Ds\Router\RouteCollection;
16
use Ds\Router\Router;
17
18
/**
19
 * Class RouterTest
20
 * @package Tests\Ds\Router
21
 */
22
class RouterTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    /**
26
     * @var \PHPUnit_Framework_MockObject_MockObject|AdaptorInterface
27
     */
28
    public $adaptor;
29
30
    /**
31
     * @var \PHPUnit_Framework_MockObject_MockObject|RouteCollectionInterface
32
     */
33
    public $collection;
34
35
    /**
36
     * @var RouterInterface
37
     */
38
    public $router;
39
40
41
    /**
42
     *
43
     */
44
    public function setUp()
45
    {
46
        $this->adaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock();
47
        $this->collection = $this->getMockBuilder(RouteCollectionInterface::class)->getMock();
48
49
        $this->router = new Router(
50
            $this->adaptor,
51
            $this->collection
52
        );
53
    }
54
55
    /**
56
     *
57
     */
58 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...
59
    {
60
        $method = 'GET';
61
        $path = '/mypath';
62
        $actual = 'my-handler';
63
64
        $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...
65
            ->method('match')
66
            ->with($this->collection, $method, $path)
67
            ->willReturn($actual);
68
69
        $expected = $this->router->getRouterResponseFromPath($method, $path);
70
        $this->assertEquals($expected, $actual);
71
    }
72
73
    /**
74
     *
75
     */
76 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...
77
    {
78
        $method = 'GET';
79
        $path = '/path';
80
        $actual = 'my-handler';
81
82
        $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...
83
            ->method('match')
84
            ->with($this->collection, $method, $path)
85
            ->willReturn($actual);
86
87
        $expected = $this->router->getRouterResponseFromPath($method,$path);
88
        $this->assertEquals($expected, $actual);
89
    }
90
91
92
    /**
93
     *
94
     */
95
    public function testWithAdaptor()
96
    {
97
        $newAdaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock();
98
        $newRouter = $this->router->withAdaptor($newAdaptor);
99
        $this->assertNotSame($this->adaptor, $newRouter->getAdaptor());
100
    }
101
102
    /**
103
     *
104
     */
105
    public function testGetAdaptor()
106
    {
107
        $expected = $this->adaptor;
108
        $actual = $this->router->getAdaptor();
109
        $this->assertEquals($expected, $actual);
110
    }
111
112
    /**
113
     *
114
     */
115
    public function testWithCollection()
116
    {
117
        $routes = $this->getMockBuilder(RouteCollectionInterface::class)->getMock();
118
        $this->router = $this->router->withCollection($routes);
119
120
        $this->assertSame(
121
            $routes,
122
            Helpers\Reflection::getProperty(Router::class, 'collection', $this->router)
123
        );
124
    }
125
126
    /**
127
     *
128
     */
129
    public function testGetCollection()
130
    {
131
        $expected = $this->collection;
132
        $actual = $this->router->getCollection();
133
        $this->assertEquals($expected, $actual);
134
    }
135
136
    /**
137
     *
138
     */
139
    public function testGetCollectionCached()
140
    {
141
        $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...
142
            ->method('isCached')
143
            ->willReturn(true);
144
145
        $this->adaptor->expects($this->once())
146
            ->method('getCachedRoutes')
147
            ->willReturn($this->collection);
148
149
        $collection = $this->router->getCollection();
150
        $this->assertSame($this->collection, $collection);
151
    }
152
153
154
    /**
155
     *
156
     */
157
    public function testMergeCollection()
158
    {
159
        $collection = new RouteCollection();
160
161
        $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...
162
            ->method('mergeCollection')
163
            ->with($this->equalTo($collection))
164
            ->willReturn($collection);
165
166
        $newCollection = $this->router->mergeCollection($collection);
167
        $this->assertNotSame($this->collection, $newCollection);
168
    }
169
170
    /**
171
     *
172
     */
173
    public function testIsCached()
174
    {
175
        $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...
176
            ->method('isCached')
177
            ->willReturn(true);
178
        $this->router->isCached();
179
    }
180
}
181