Completed
Push — master ( 069441...8a02a6 )
by Aimeos
07:19
created

RouterTest::testPathForWithBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 16
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
/**
3
 * Slim Framework (http://slimframework.com)
4
 *
5
 * @link      https://github.com/slimphp/Slim
6
 * @copyright Copyright (c) 2011-2016 Josh Lockhart
7
 * @license   https://github.com/slimphp/Slim/blob/master/LICENSE.md (MIT License)
8
 */
9
10
11
class RouterTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /** @var Router */
14
    protected $router;
15
16
    public function setUp()
17
    {
18
        $this->router = new \Aimeos\Slim\Router;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Aimeos\Slim\Router() of type object<Aimeos\Slim\Router> is incompatible with the declared type object<Router> of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
19
    }
20
21
    public function testRelativePathFor()
22
    {
23
        $this->router->setBasePath('/base/path');
24
25
        $methods = ['GET'];
26
        $pattern = '/hello/{first:\w+}/{last}';
27
        $callable = function ($request, $response, $args) {
28
            echo sprintf('Hello %s %s', $args['first'], $args['last']);
29
        };
30
        $route = $this->router->map($methods, $pattern, $callable);
31
        $route->setName('foo');
32
33
        $this->assertEquals(
34
            '/hello/josh/lockhart',
35
            $this->router->relativePathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
36
        );
37
    }
38
39 View Code Duplication
    public function testPathForWithNoBasePath()
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...
40
    {
41
        $this->router->setBasePath('');
42
43
        $methods = ['GET'];
44
        $pattern = '/hello/{first:\w+}/{last}';
45
        $callable = function ($request, $response, $args) {
46
            echo sprintf('Hello %s %s', $args['first'], $args['last']);
47
        };
48
        $route = $this->router->map($methods, $pattern, $callable);
49
        $route->setName('foo');
50
51
        $this->assertEquals(
52
            '/hello/josh/lockhart',
53
            $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
54
        );
55
    }
56
57 View Code Duplication
    public function testPathForWithBasePath()
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...
58
    {
59
        $methods = ['GET'];
60
        $pattern = '/hello/{first:\w+}/{last}';
61
        $callable = function ($request, $response, $args) {
62
            echo sprintf('Hello %s %s', $args['first'], $args['last']);
63
        };
64
        $this->router->setBasePath('/base/path');
65
        $route = $this->router->map($methods, $pattern, $callable);
66
        $route->setName('foo');
67
68
        $this->assertEquals(
69
            '/base/path/hello/josh/lockhart',
70
            $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
71
        );
72
    }
73
74
    public function testPathForWithOptionalParameters()
75
    {
76
        $methods = ['GET'];
77
        $pattern = '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]';
78
        $callable = function ($request, $response, $args) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
            return $response;
80
        };
81
        $route = $this->router->map($methods, $pattern, $callable);
82
        $route->setName('foo');
83
84
        $this->assertEquals(
85
            '/archive/2015',
86
            $this->router->pathFor('foo', ['year' => '2015'])
87
        );
88
        $this->assertEquals(
89
            '/archive/2015/07',
90
            $this->router->pathFor('foo', ['year' => '2015', 'month' => '07'])
91
        );
92
        $this->assertEquals(
93
            '/archive/2015/07/d/19',
94
            $this->router->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19'])
95
        );
96
    }
97
98 View Code Duplication
    public function testPathForWithSurplusRouteParameters()
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...
99
    {
100
        $methods = ['GET'];
101
        $pattern = '/hello/{name}';
102
        $callable = function ($request, $response, $args) {
103
            echo sprintf('Hello %s', $args['name']);
104
        };
105
        $route = $this->router->map($methods, $pattern, $callable);
106
        $route->setName('foo');
107
108
        $this->assertEquals(
109
            '/hello/josh?a=b',
110
            $this->router->pathFor('foo', ['name' => 'josh', 'a' => 'b'])
111
        );
112
    }
113
114
    /**
115
     * @expectedException \InvalidArgumentException
116
     */
117 View Code Duplication
    public function testPathForWithMissingSegmentData()
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...
118
    {
119
        $methods = ['GET'];
120
        $pattern = '/hello/{first}/{last}';
121
        $callable = function ($request, $response, $args) {
122
            echo sprintf('Hello %s %s', $args['first'], $args['last']);
123
        };
124
        $route = $this->router->map($methods, $pattern, $callable);
125
        $route->setName('foo');
126
127
        $this->router->pathFor('foo', ['last' => 'lockhart']);
128
    }
129
130
    /**
131
     * @expectedException \RuntimeException
132
     */
133 View Code Duplication
    public function testPathForRouteNotExists()
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...
134
    {
135
        $methods = ['GET'];
136
        $pattern = '/hello/{first}/{last}';
137
        $callable = function ($request, $response, $args) {
138
            echo sprintf('Hello %s %s', $args['first'], $args['last']);
139
        };
140
        $route = $this->router->map($methods, $pattern, $callable);
141
        $route->setName('foo');
142
143
        $this->router->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']);
144
    }
145
}
146