-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRoutingCollectorTest.php
150 lines (135 loc) · 5.89 KB
/
RoutingCollectorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* This file is part of Aplus Framework Routing Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Routing\Debug;
use Framework\HTTP\Request;
use Framework\HTTP\Response;
use Framework\Routing\Debug\RoutingCollector;
use Framework\Routing\RouteCollection;
use Framework\Routing\Router;
use PHPUnit\Framework\TestCase;
final class RoutingCollectorTest extends TestCase
{
protected RoutingCollector $collector;
protected function setUp() : void
{
$this->collector = new RoutingCollector();
}
protected function setServerVars() : void
{
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/contact';
$_SERVER['HTTP_HOST'] = 'domain.tld';
}
protected function makeRouter() : Router
{
$this->setServerVars();
$router = new Router(new Response(new Request()));
$router->setDebugCollector($this->collector);
$router->serve('http://domain.tld', static function (RouteCollection $routes) : void {
$routes->get('/', static fn () => 'Home page');
$routes->get('/contact', static fn () => 'Contact page', 'contact');
$routes->post('/contact', static fn () => 'Thanks!');
$routes->notFound(static fn () => 'Page not found');
})->serve('http://api.domain.tld', static function (RouteCollection $routes) : void {
$routes->get('/users', static fn () => [[1], [2]], 'users');
}, 'api');
return $router;
}
public function testRouterNotSet() : void
{
$contents = $this->collector->getContents();
self::assertStringContainsString('A Router instance has not been set', $contents);
}
public function testDefaultRouteNotFound() : void
{
$router = $this->makeRouter();
$router->setDefaultRouteNotFound('App\Errors::notFound');
$contents = $this->collector->getContents();
self::assertStringContainsString('App\Errors::notFound', $contents);
}
public function testNoRouteCollection() : void
{
$this->setServerVars();
$router = new Router(new Response(new Request()));
$router->setDebugCollector($this->collector);
$contents = $this->collector->getContents();
self::assertStringContainsString('No route collection has been set', $contents);
}
public function testCollections() : void
{
$this->makeRouter();
$activities = $this->collector->getActivities();
self::assertSame('Serve route collection 1', $activities[0]['description']);
self::assertSame('Serve route collection 2', $activities[1]['description']);
$contents = $this->collector->getContents();
self::assertStringContainsString('Route Collection 1', $contents);
self::assertStringContainsString('Route Collection 2', $contents);
}
public function testNoMatchedRoute() : void
{
$this->makeRouter();
$contents = $this->collector->getContents();
self::assertStringContainsString('No matching route', $contents);
self::assertStringNotContainsString('Time to Match', $contents);
}
public function testMatchedRoute() : void
{
$this->makeRouter()->match();
$activities = $this->collector->getActivities();
self::assertSame('Match route', $activities[2]['description']);
$contents = $this->collector->getContents();
self::assertStringNotContainsString('No matching route', $contents);
self::assertStringContainsString('Time to Match', $contents);
}
public function testRun() : void
{
$this->makeRouter()->match()->run();
$activities = $this->collector->getActivities();
self::assertSame('Run matched route', $activities[3]['description']);
$contents = $this->collector->getContents();
self::assertStringNotContainsString('No matching route', $contents);
self::assertStringContainsString('Time to Match', $contents);
}
public function testRunWithRouteActions() : void
{
$this->setServerVars();
$_SERVER['REQUEST_URI'] = '/foo';
$router = new Router(new Response(new Request()));
$router->setDebugCollector($this->collector);
$router->serve('http://domain.tld', static function (RouteCollection $routes) : void {
$routes->get('/foo', 'Tests\Routing\Support\WithRouteActions::index', 'actions');
});
$router->match()->run();
$activities = $this->collector->getActivities();
self::assertSame('Serve route collection 1', $activities[0]['description']);
self::assertSame('Match route', $activities[1]['description']);
self::assertSame('Run matched route', $activities[2]['description']);
$contents = $this->collector->getContents();
self::assertStringNotContainsString('No matching route', $contents);
self::assertStringContainsString('Time to Match', $contents);
self::assertSame('actions', $router->getMatchedRoute()->getName());
}
public function testNotRoutesInCollection() : void
{
$this->makeRouter()->serve('http://admin.domain.tld', static function (RouteCollection $routes) : void {
});
$contents = $this->collector->getContents();
self::assertStringContainsString('No route has been set in this collection', $contents);
}
public function testOnlyRouteNotFoundInCollection() : void
{
$this->makeRouter()->serve('http://admin.domain.tld', static function (RouteCollection $routes) : void {
$routes->notFound('foo');
})->match();
$contents = $this->collector->getContents();
self::assertStringContainsString('Only Route Not Found has been set in this collection', $contents);
}
}