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 |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** @var Router */ |
14
|
|
|
protected $router; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
$this->router = new \Aimeos\Slim\Router; |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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) { |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.