Test Failed
Push — master ( 3e96d2...d4b731 )
by Dan
06:30
created

FileLoaderTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 147
Duplicated Lines 48.3 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 71
loc 147
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A testLoadFileNoCacheNonInvalidFile() 8 8 1
A testLoadFileNoCacheNoFile() 8 8 1
A testLoadFileNoCacheWithVars() 14 14 1
A testLoadFileWithCache() 12 12 1
A testLoadFilesNoCacheNoFile() 14 14 1
A testLoadFilesNoCache() 15 15 1
A testLoadFilesCache() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Tests\Ds\Router\Loaders;
4
5
use Ds\Router\Exceptions\RouterException;
6
use Ds\Router\Interfaces\RouterInterface;
7
use Ds\Router\Loaders\FileLoader;
8
use Ds\Router\RouteCollection;
9
10
/**
11
 * Class FileLoaderTest
12
 * @package Tests\Ds\Router\Loaders
13
 */
14
class FileLoaderTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var \PHPUnit_Framework_MockObject_MockObject|RouterInterface
18
     */
19
    public $router;
20
    /**
21
     * @var FileLoader
22
     */
23
    public $loader;
24
25
    /**
26
     * @var array
27
     */
28
    public $vars;
29
30
    /**
31
     * @var string
32
     */
33
    public $file;
34
35
    /**
36
     * PHP Route File Loader setUp
37
     */
38
    public function setUp()
39
    {
40
        $options = [
41
            'vars' => [
42
                'variable' => 'my-var'
43
            ]
44
        ];
45
46
        $this->file = __DIR__ . '/Files/FileLoaderRoutes.php';
47
        $this->router = $this->getMockBuilder(RouterInterface::class)->getMock();
48
        $this->loader = new FileLoader($this->router, $options);
49
    }
50
51
    /**
52
     *
53
     */
54 View Code Duplication
    public function testLoadFileNoCacheNonInvalidFile()
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...
55
    {
56
        $this->setExpectedException(RouterException::class);
57
        $this->router->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\RouterInterface.

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...
58
            ->method('isCached')
59
            ->willReturn(false);
60
        $this->loader->loadFile(__DIR__ . '/Files/Malformed.php');
61
    }
62
63
    /**
64
     *
65
     */
66 View Code Duplication
    public function testLoadFileNoCacheNoFile()
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...
67
    {
68
        $this->setExpectedException(RouterException::class);
69
        $this->router->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\RouterInterface.

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...
70
            ->method('isCached')
71
            ->willReturn(false);
72
        $this->loader->loadFile(__DIR__ . '/RouteFileNone.php');
73
    }
74
75
    /**
76
     *
77
     */
78 View Code Duplication
    public function testLoadFileNoCacheWithVars()
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...
79
    {
80
        $this->router->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\RouterInterface.

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...
81
            ->method('isCached')
82
            ->willReturn(false);
83
84
        $routes = $this->loader->loadFile($this->file);
85
        $route = $routes->current();
86
        $handler = $route->getHandler();
87
88
        $expected = 'my-var';
89
        $actual = $handler();
90
        $this->assertEquals($expected, $actual);
91
    }
92
93
    /**
94
     *
95
     */
96 View Code Duplication
    public function testLoadFileWithCache()
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...
97
    {
98
        $this->router->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\RouterInterface.

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...
99
            ->method('isCached')
100
            ->willReturn(true);
101
        $this->router->expects($this->once())
102
            ->method('getCollection')
103
            ->willReturn(new RouteCollection());
104
105
        $collection = $this->loader->loadFile($this->file);
106
        $this->assertInstanceOf(RouteCollection::class, $collection);
107
    }
108
109
    /**
110
     *
111
     */
112 View Code Duplication
    public function testLoadFilesNoCacheNoFile()
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...
113
    {
114
        $this->setExpectedException(RouterException::class);
115
116
        $this->router->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

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...
117
            ->method('isCached')
118
            ->willReturn(false);
119
120
        $this->router->expects($this->any())
121
            ->method('mergeCollection')
122
            ->willReturn($this->router);
123
124
        $this->loader->loadFiles([$this->file, __DIR__ . '/RouteFileNone.php']);
125
    }
126
127
    /**
128
     *
129
     */
130 View Code Duplication
    public function testLoadFilesNoCache()
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...
131
    {
132
        $this->router->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

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...
133
            ->method('isCached')
134
            ->willReturn(false);
135
136
        $this->router->expects($this->any())
137
            ->method('mergeCollection')
138
            ->willReturn($this->router);
139
140
        $this->loader->loadFiles([
141
            $this->file,
142
            __DIR__ . '/Files/FileLoaderRoutesAlt.php'
143
        ]);
144
    }
145
146
    /**
147
     *
148
     */
149
    public function testLoadFilesCache()
150
    {
151
        $this->router->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Ds\Router\Interfaces\RouterInterface.

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...
152
            ->method('isCached')
153
            ->willReturn(true);
154
        $router = $this->loader->loadFiles([
155
            $this->file,
156
            __DIR__ . '/Files/FileLoaderRoutesAlt.php'
157
        ]);
158
        $this->assertSame($this->router, $router);
159
    }
160
}
161