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

RouteCollectionTest::testFindHandlerNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

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 9
nc 1
nop 0
1
<?php
2
3
namespace Tests\Ds\Router;
4
5
use Ds\Router\Exceptions\RouteException;
6
use Ds\Router\Route;
7
use Ds\Router\RouteCollection;
8
9
/**
10
 * Route Collection Tests.
11
 * @package Tests\Rs\Router
12
 */
13
class RouteCollectionTest extends \PHPUnit_Framework_TestCase
14
{
15
16
    /**
17
     * @var RouteCollection
18
     */
19
    public $collection;
20
21
    /**
22
     * Route Collection Set up.
23
     */
24
    public function setUp()
25
    {
26
        $this->collection = new RouteCollection();
27
    }
28
29
    /**
30
     *
31
     */
32
    public function testGroup()
33
    {
34
        $handler = 'my-handler';
35
        $routes = $this->collection;
36
        $routes->group('/path', function () use ($routes, $handler) {
37
            $routes->addRoute(['GET', 'POST'], '/foo', $handler, ['global']);
38
            $routes->group('/sub-dir', function () use ($routes, $handler) {
39
                $routes->addRoute(['GET'], '/sub-page', $handler, ['sub-dir']);
40
            });
41
        });
42
        foreach ($routes as $route) {
43
            $expected[] = $route;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$expected was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expected = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
44
        }
45
        $actual = Helpers\Reflection::getProperty(RouteCollection::class, 'collection', $routes);
46
        $this->assertEquals($expected, $actual);
0 ignored issues
show
Bug introduced by
The variable $expected does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
47
    }
48
49
    /**
50
     *
51
     */
52
    public function testAddRoute()
53
    {
54
        $handler = 'my-handler';
55
        $collection = new RouteCollection();
56
        $collection->addRoute(['GET', 'POST'], '/foo', $handler, ['global']);
57
        $actual = Helpers\Reflection::getProperty(RouteCollection::class, 'collection', $collection);
58
        $expected = [
59
            0 => new Route('GET', '/foo', $handler, ['global']),
60
            1 => new Route('POST', '/foo', $handler, ['global'])
61
        ];
62
        $this->assertEquals($expected, $actual);
63
    }
64
65
    /**
66
     * Test that RouterException is thrown on duplicate route.
67
     */
68
    public function testIsUnique()
69
    {
70
        $this->setExpectedException(RouteException::class);
71
        $handler = 'my-handler';
72
        $this->collection->addRoute('GET', '/foo', $handler, ['global']);
73
        $this->collection->addRoute('GET', '/foo', $handler, ['global']);
74
    }
75
76
    /**
77
     *
78
     */
79
    public function testCount()
80
    {
81
        $handler = 'my-handler';
82
        $collection = new RouteCollection();
83
        $collection->addRoute(['GET', 'POST'], '/foo', $handler, ['global']);
84
        $collection->addRoute(['GET', 'POST'], '/bar', $handler, ['global']);
85
        $expected = 4;
86
        $this->assertEquals($expected, $collection->count());
87
    }
88
89
    /**
90
     *
91
     */
92 View Code Duplication
    public function testKey()
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...
93
    {
94
        $handler = 'my-handler';
95
        $collection = new RouteCollection();
96
        $collection->addRoute(['GET'], '/foo', $handler, ['global']);
97
        $collection->addRoute(['GET'], '/bar', $handler, ['global']);
98
        $expected = 0;
99
        $this->assertEquals($expected, $collection->key());
100
    }
101
102
    /**
103
     *
104
     */
105 View Code Duplication
    public function testNext()
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...
106
    {
107
        $handler = 'my-handler';
108
        $collection = new RouteCollection();
109
        $collection->addRoute(['GET'], '/foo', $handler, ['global']);
110
        $collection->addRoute(['GET'], '/bar', $handler, ['global']);
111
        $expected = 1;
112
        $collection->next();
113
        $this->assertEquals($expected, $collection->key());
114
    }
115
116
    /**
117
     *
118
     */
119 View Code Duplication
    public function testCurrent()
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...
120
    {
121
        $handler = 'my-handler';
122
        $collection = new RouteCollection();
123
        $collection->addRoute(['GET'], '/foo', $handler, ['global']);
124
        $collection->addRoute(['GET'], '/bar', $handler, ['global']);
125
        $expected = new Route('GET', '/foo', $handler, ['global']);
126
        $this->assertEquals($expected, $collection->current());
127
    }
128
129
    /**
130
     *
131
     */
132 View Code Duplication
    public function testValidTrue()
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...
133
    {
134
        $handler = 'my-handler';
135
        $collection = new RouteCollection();
136
        $collection->addRoute(['GET'], '/foo', $handler, ['global']);
137
        $expected = true;
138
        $this->assertEquals($expected, $collection->valid());
139
    }
140
141
    /**
142
     *
143
     */
144 View Code Duplication
    public function testValidFalse()
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...
145
    {
146
        $expected = false;
147
        $handler = 'my-handler';
148
        $collection = new RouteCollection();
149
        $collection->addRoute(['GET'], '/foo', $handler, ['global']);
150
        $collection->next();
151
        $this->assertEquals($expected, $collection->valid());
152
    }
153
154
155
    /**
156
     *
157
     */
158
    public function testRewind()
159
    {
160
        $handler = 'my-handler';
161
        $expected = new Route('GET', '/bar', $handler, ['global']);
162
        $collection = new RouteCollection();
163
        $collection->addRoute(['GET'], '/foo', $handler, ['global']);
164
        $collection->addRoute(['GET'], '/bar', $handler, ['global']);
165
        $collection->next();
166
        $this->assertEquals($expected, $collection->current());
167
        $expectedKey = 0;
168
        $collection->rewind();
169
        $this->assertEquals($expectedKey, $collection->key());
170
    }
171
172
    /**
173
     *
174
     */
175
    public function testMergeCollection()
176
    {
177
        $handler = 'handler::string';
178
        $names = ['routes','names'];
179
        $collection = new RouteCollection();
180
        $collection->addRoute('GET', '/path', $handler, $names);
181
        $collection->addRoute('GET', '/another', $handler, $names);
182
        $secondCollection = new RouteCollection();
183
        $secondCollection->addRoute('GET', '/path-2', $handler, $names);
184
        $secondCollection->addRoute('GET', '/another-2', $handler, $names);
185
        $combined = $collection->mergeCollection($secondCollection, false);
186
        $expectedRouteCount = 4;
187
        $this->assertEquals($expectedRouteCount, $combined->count());
188
    }
189
190
    /**
191
     *
192
     */
193
    public function testMergeCollectionDuplicate()
194
    {
195
        $this->setExpectedException(RouteException::class);
196
        $handler = 'handler::string';
197
        $names = ['routes','names'];
198
        $collection = new RouteCollection();
199
        $collection->addRoute('GET', '/path', $handler, $names);
200
        $collection->addRoute('GET', '/another', $handler, $names);
201
        $secondCollection = new RouteCollection();
202
        $secondCollection->addRoute('GET', '/path', $handler, $names);
203
        $secondCollection->addRoute('GET', '/another-2', $handler, $names);
204
        $collection->mergeCollection($secondCollection, true);
205
    }
206
207
    /**
208
     *
209
     */
210
    public function testGetRoutes()
211
    {
212
        $method = 'GET';
213
        $pattern = '/path';
214
        $handler = 'myhandler';
215
        $names = ['names'];
216
        $expected = [new Route($method, $pattern, $handler, $names)];
217
        $this->collection->addRoute($method, $pattern, $handler, $names);
218
        $this->assertEquals($expected, $this->collection->getRoutes());
219
    }
220
221
    public function testFormatRoutePatternForRoute()
222
    {
223
        $rawPattern = '/foo/';
224
        $expectedPattern = '/' . \ltrim($rawPattern, '/');
225
        $this->assertEquals($expectedPattern, $this->collection->formatRoutePattern($rawPattern));
226
    }
227
228
    /**
229
     * Test that forward slashes are replaces with backslash
230
     */
231
    public function testFormatRoutePatternForGroup()
232
    {
233
        $groups = ['group'];
234
        $collection = $this->collection;
235
        $collection = Helpers\Reflection::setProperty(RouteCollection::class, '_isGrouped', $collection, true);
236
        $collection = Helpers\Reflection::setProperty(RouteCollection::class, 'group', $collection, $groups);
237
        $rawPattern = '/mypattern';
238
        $expectedPattern = '/' . \ltrim($rawPattern, '/');
239
        $expected = \implode('', $groups) . $expectedPattern;
240
        $this->assertEquals($expected, $collection->formatRoutePattern($rawPattern));
241
    }
242
243
    /**
244
     * Test that route names are merge with grouped routes.
245
     */
246
    public function testMergeGroupNames()
247
    {
248
        $expected = ['route-name','group-name'];
249
        $handler = 'myhandler';
250
        $routes = $this->collection;
251
252
        $routes->group('/path', function () use ($routes, $handler) {
253
            $routes->addRoute(['GET', 'POST'], '/foo', $handler, ['route-name']);
254
        }, ['group-name']);
255
256
        $route = $routes->current();
257
        $this->assertEquals($expected, $route->getNames());
258
    }
259
260
    /**
261
     * Test that the namespace is matched to the handler and returned.
262
     */
263 View Code Duplication
    public function testFindHandlerNamespace(){
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...
264
265
        $routeCollection = new RouteCollection();
266
        $routeCollection->addNamespace('\Rs\Router');
267
268
        $handler = 'RouteCollection::addRoute';
269
        $expected = '\\Rs\\Router\\RouteCollection::addRoute';
270
271
        $reflectionMethod = new \ReflectionMethod(RouteCollection::class, '_findHandlerNamespace');
272
        $reflectionMethod->setAccessible(true);
273
        $actual = $reflectionMethod->invoke($routeCollection, $handler);
274
275
        $this->assertEquals($expected,$actual);
276
    }
277
278
    /**
279
     * Test that backlash is added to start of namespace on response.
280
     */
281 View Code Duplication
    public function testFindHandlerNamespaceNoBacklash(){
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...
282
283
        $routeCollection = new RouteCollection();
284
        $routeCollection->addNamespace('Rs\Router');
285
286
        $handler = 'RouteCollection::addRoute';
287
        $expected = '\\Rs\\Router\\RouteCollection::addRoute';
288
289
        $reflectionMethod = new \ReflectionMethod(RouteCollection::class, '_findHandlerNamespace');
290
        $reflectionMethod->setAccessible(true);
291
        $actual = $reflectionMethod->invoke($routeCollection, $handler);
292
293
        $this->assertEquals($expected,$actual);
294
    }
295
296
}
297