RouterTest::testPathForRouteNotExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * Slim Framework (http://slimframework.com)
4
 *
5
 * @link      https://github.com/slimphp/Slim
6
 * @copyright Copyright (c) 2011-2017 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() : void
17
	{
18
		if( !class_exists( \Slim\Router::class, false ) ) {
19
			$this->markTestSkipped( '\Slim\Router is not available' );
20
		}
21
22
		$this->router = new \Aimeos\Slim\Router;
0 ignored issues
show
Documentation Bug introduced by
It seems like new Aimeos\Slim\Router() of type Aimeos\Slim\Router is incompatible with the declared type 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...
23
	}
24
25
	public function testRelativePathFor()
26
	{
27
		$this->router->setBasePath('/base/path');
28
29
		$methods = ['GET'];
30
		$pattern = '/hello/{first:\w+}/{last}';
31
		$callable = function ( $request, $response, $args ) {
32
			echo sprintf('Hello %s %s', $args['first'], $args['last']);
33
		};
34
		$route = $this->router->map($methods, $pattern, $callable);
35
		$route->setName('foo');
36
37
		$this->assertEquals(
38
			'/hello/josh/lockhart',
39
			$this->router->relativePathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
40
		);
41
	}
42
43
	public function testPathForWithNoBasePath()
44
	{
45
		$this->router->setBasePath('');
46
47
		$methods = ['GET'];
48
		$pattern = '/hello/{first:\w+}/{last}';
49
		$callable = function ( $request, $response, $args ) {
50
			echo sprintf('Hello %s %s', $args['first'], $args['last']);
51
		};
52
		$route = $this->router->map($methods, $pattern, $callable);
53
		$route->setName('foo');
54
55
		$this->assertEquals(
56
			'/hello/josh/lockhart',
57
			$this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
58
		);
59
	}
60
61
	public function testPathForWithBasePath()
62
	{
63
		$methods = ['GET'];
64
		$pattern = '/hello/{first:\w+}/{last}';
65
		$callable = function ( $request, $response, $args ) {
66
			echo sprintf('Hello %s %s', $args['first'], $args['last']);
67
		};
68
		$this->router->setBasePath('/base/path');
69
		$route = $this->router->map($methods, $pattern, $callable);
70
		$route->setName('foo');
71
72
		$this->assertEquals(
73
			'/base/path/hello/josh/lockhart',
74
			$this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart'])
75
		);
76
	}
77
78
	public function testPathForWithOptionalParameters()
79
	{
80
		$methods = ['GET'];
81
		$pattern = '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]';
82
		$callable = function ( $request, $response, $args ) {
83
			return $response;
84
		};
85
		$route = $this->router->map($methods, $pattern, $callable);
86
		$route->setName('foo');
87
88
		$this->assertEquals(
89
			'/archive/2015',
90
			$this->router->pathFor('foo', ['year' => '2015'])
91
		);
92
		$this->assertEquals(
93
			'/archive/2015/07',
94
			$this->router->pathFor('foo', ['year' => '2015', 'month' => '07'])
95
		);
96
		$this->assertEquals(
97
			'/archive/2015/07/d/19',
98
			$this->router->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19'])
99
		);
100
	}
101
102
	public function testPathForWithSurplusRouteParameters()
103
	{
104
		$methods = ['GET'];
105
		$pattern = '/hello/{name}';
106
		$callable = function ( $request, $response, $args ) {
107
			echo sprintf('Hello %s', $args['name']);
108
		};
109
		$route = $this->router->map($methods, $pattern, $callable);
110
		$route->setName('foo');
111
112
		$this->assertEquals(
113
			'/hello/josh?a=b',
114
			$this->router->pathFor('foo', ['name' => 'josh', 'a' => 'b'])
115
		);
116
	}
117
118
	/**
119
	 * @expectedException \InvalidArgumentException
120
	 */
121
	public function testPathForWithMissingSegmentData()
122
	{
123
		$methods = ['GET'];
124
		$pattern = '/hello/{first}/{last}';
125
		$callable = function ( $request, $response, $args ) {
126
			echo sprintf('Hello %s %s', $args['first'], $args['last']);
127
		};
128
		$route = $this->router->map($methods, $pattern, $callable);
129
		$route->setName('foo');
130
131
		$this->router->pathFor('foo', ['last' => 'lockhart']);
132
	}
133
134
	/**
135
	 * @expectedException \RuntimeException
136
	 */
137
	public function testPathForRouteNotExists()
138
	{
139
		$methods = ['GET'];
140
		$pattern = '/hello/{first}/{last}';
141
		$callable = function ( $request, $response, $args ) {
142
			echo sprintf('Hello %s %s', $args['first'], $args['last']);
143
		};
144
		$route = $this->router->map($methods, $pattern, $callable);
145
		$route->setName('foo');
146
147
		$this->router->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']);
148
	}
149
}
150