Passed
Push — master ( aac959...f67e8c )
by Anatoly
01:26
created

RouteTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 184
rs 10
c 0
b 0
f 0
wmc 2

19 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddPattern() 0 6 1
A testAddMethod() 0 6 1
A testGetPath() 0 5 1
A testGetAction() 0 7 1
A testGetId() 0 5 1
A testAddMiddleware() 0 8 1
A testAddLowercasedMethod() 0 7 1
A testAddSeveralMiddleware() 0 11 1
A testAddSeveralPrefixex() 0 8 1
A testGetMethods() 0 5 1
A testGetPatterns() 0 5 1
A testAddPrefix() 0 6 1
A testSetAttributes() 0 9 1
A testGetMiddlewareStack() 0 5 1
A testGetAttributes() 0 5 1
A testAddSeveralPatterns() 0 11 1
A testConstructor() 0 6 1
A testProcess() 0 13 1
A testAddSeveralMethods() 0 8 1
1
<?php
2
3
namespace Sunrise\Http\Router\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\Http\Server\MiddlewareInterface;
7
use Sunrise\Http\Router\RequestHandler;
8
use Sunrise\Http\Router\Route;
9
use Sunrise\Http\Router\RouteInterface;
10
use Sunrise\Http\ServerRequest\ServerRequestFactory;
11
12
class RouteTest extends TestCase
13
{
14
	use HelpersInjectTest;
15
16
	public function testConstructor()
17
	{
18
		$route = new Route('home', '/', $this->getRouteActionFoo());
19
20
		$this->assertInstanceOf(RouteInterface::class, $route);
21
		$this->assertInstanceOf(MiddlewareInterface::class, $route);
22
	}
23
24
	public function testGetId()
25
	{
26
		$route = new Route('home', '/', $this->getRouteActionFoo());
27
28
		$this->assertEquals('home', $route->getId());
29
	}
30
31
	public function testGetPath()
32
	{
33
		$route = new Route('home', '/', $this->getRouteActionFoo());
34
35
		$this->assertEquals('/', $route->getPath());
36
	}
37
38
	public function testGetAction()
39
	{
40
		$action = $this->getRouteActionFoo();
41
42
		$route = new Route('home', '/', $action);
43
44
		$this->assertEquals($action, $route->getAction());
45
	}
46
47
	public function testGetMethods()
48
	{
49
		$route = new Route('home', '/', $this->getRouteActionFoo());
50
51
		$this->assertEquals([], $route->getMethods());
52
	}
53
54
	public function testGetPatterns()
55
	{
56
		$route = new Route('home', '/', $this->getRouteActionFoo());
57
58
		$this->assertEquals([], $route->getPatterns());
59
	}
60
61
	public function testGetMiddlewareStack()
62
	{
63
		$route = new Route('home', '/', $this->getRouteActionFoo());
64
65
		$this->assertEquals([], $route->getMiddlewareStack());
66
	}
67
68
	public function testGetAttributes()
69
	{
70
		$route = new Route('home', '/', $this->getRouteActionFoo());
71
72
		$this->assertEquals([], $route->getAttributes());
73
	}
74
75
	public function testAddPrefix()
76
	{
77
		$route = new Route('home', '/', $this->getRouteActionFoo());
78
79
		$this->assertInstanceOf(RouteInterface::class, $route->prefix('/foo'));
80
		$this->assertEquals('/foo/', $route->getPath());
81
	}
82
83
	public function testAddSeveralPrefixex()
84
	{
85
		$route = new Route('home', '/', $this->getRouteActionFoo());
86
87
		$route->prefix('/foo');
88
		$route->prefix('/bar');
89
90
		$this->assertEquals('/bar/foo/', $route->getPath());
91
	}
92
93
	public function testAddMethod()
94
	{
95
		$route = new Route('home', '/', $this->getRouteActionFoo());
96
97
		$this->assertInstanceOf(RouteInterface::class, $route->method('GET'));
98
		$this->assertEquals(['GET'], $route->getMethods());
99
	}
100
101
	public function testAddSeveralMethods()
102
	{
103
		$route = new Route('home', '/', $this->getRouteActionFoo());
104
105
		$route->method('GET');
106
		$route->method('POST');
107
108
		$this->assertEquals(['GET', 'POST'], $route->getMethods());
109
	}
110
111
	public function testAddLowercasedMethod()
112
	{
113
		$route = new Route('home', '/', $this->getRouteActionFoo());
114
115
		$route->method('get');
116
117
		$this->assertEquals(['GET'], $route->getMethods());
118
	}
119
120
	public function testAddPattern()
121
	{
122
		$route = new Route('home', '/', $this->getRouteActionFoo());
123
124
		$this->assertInstanceOf(RouteInterface::class, $route->pattern('id', '\d+'));
125
		$this->assertEquals(['id' => '\d+'], $route->getPatterns());
126
	}
127
128
	public function testAddSeveralPatterns()
129
	{
130
		$route = new Route('home', '/', $this->getRouteActionFoo());
131
132
		$route->pattern('id', '\d+');
133
		$route->pattern('word', '\w+');
134
135
		$this->assertEquals([
136
			'id' => '\d+',
137
			'word' => '\w+',
138
		], $route->getPatterns());
139
	}
140
141
	public function testAddMiddleware()
142
	{
143
		$foo = $this->getMiddlewareFoo();
144
145
		$route = new Route('home', '/', $this->getRouteActionFoo());
146
147
		$this->assertInstanceOf(RouteInterface::class, $route->middleware($foo));
148
		$this->assertEquals([$foo], $route->getMiddlewareStack());
149
	}
150
151
	public function testAddSeveralMiddleware()
152
	{
153
		$foo = $this->getMiddlewareFoo();
154
		$bar = $this->getMiddlewareBar();
155
156
		$route = new Route('home', '/', $this->getRouteActionFoo());
157
158
		$route->middleware($foo);
159
		$route->middleware($bar);
160
161
		$this->assertEquals([$foo, $bar], $route->getMiddlewareStack());
162
	}
163
164
	public function testSetAttributes()
165
	{
166
		$route = new Route('home', '/', $this->getRouteActionFoo());
167
168
		$clone = $route->withAttributes(['id' => '1']);
169
		$this->assertInstanceOf(RouteInterface::class, $clone);
170
		$this->assertEquals(['id' => '1'], $clone->getAttributes());
171
172
		$this->assertEquals([], $route->getAttributes());
173
	}
174
175
	public function testProcess()
176
	{
177
		$route = new Route('home', '/', $this->getRouteActionFoo());
178
179
		$handler = new RequestHandler();
180
		$handler->add($route);
181
182
		$request = (new ServerRequestFactory)
183
		->createServerRequest('GET', '/');
184
185
		$response = $handler->handle($request);
186
187
		$this->assertEquals(['foo'], $response->getHeader('x-route'));
188
	}
189
}
190