Skip to content

Commit 29d9dd7

Browse files
committed
Clean-up RouteCollectorTest
1 parent 3efa22b commit 29d9dd7

1 file changed

Lines changed: 58 additions & 50 deletions

File tree

test/RouteCollectorTest.php

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ final class RouteCollectorTest extends TestCase
2020
#[PHPUnit\Test]
2121
public function shortcutsCanBeUsedToRegisterRoutes(): void
2222
{
23-
$dataGenerator = self::dummyDataGenerator();
24-
$r = new RouteCollector(new Std(), $dataGenerator);
25-
26-
$r->any('/any', 'any');
27-
$r->delete('/delete', 'delete');
28-
$r->get('/get', 'get');
29-
$r->head('/head', 'head');
30-
$r->patch('/patch', 'patch');
31-
$r->post('/post', 'post');
32-
$r->put('/put', 'put');
33-
$r->options('/options', 'options');
23+
$r = self::routeCollector();
24+
25+
$r
26+
->any('/any', 'any')
27+
->delete('/delete', 'delete')
28+
->get('/get', 'get')
29+
->head('/head', 'head')
30+
->patch('/patch', 'patch')
31+
->post('/post', 'post')
32+
->put('/put', 'put')
33+
->options('/options', 'options');
3434

3535
$expected = [
3636
['*', '/any', 'any', ['_route' => '/any']],
@@ -43,47 +43,49 @@ public function shortcutsCanBeUsedToRegisterRoutes(): void
4343
['OPTIONS', '/options', 'options', ['_route' => '/options']],
4444
];
4545

46-
self::assertObjectHasProperty('routes', $dataGenerator);
47-
self::assertSame($expected, $dataGenerator->routes);
46+
self::assertSame($expected, $r->processedRoutes()[0]);
4847
}
4948

5049
#[PHPUnit\Test]
5150
public function routesCanBeGrouped(): void
5251
{
53-
$dataGenerator = self::dummyDataGenerator();
54-
$r = new RouteCollector(new Std(), $dataGenerator);
55-
56-
$r->delete('/delete', 'delete');
57-
$r->get('/get', 'get');
58-
$r->head('/head', 'head');
59-
$r->patch('/patch', 'patch');
60-
$r->post('/post', 'post');
61-
$r->put('/put', 'put');
62-
$r->options('/options', 'options');
63-
64-
$r->addGroup('/group-one', static function (ConfigureRoutes $r): void {
65-
$r->delete('/delete', 'delete');
66-
$r->get('/get', 'get');
67-
$r->head('/head', 'head');
68-
$r->patch('/patch', 'patch');
69-
$r->post('/post', 'post');
70-
$r->put('/put', 'put');
71-
$r->options('/options', 'options');
72-
73-
$r->addGroup('/group-two', static function (ConfigureRoutes $r): void {
74-
$r->delete('/delete', 'delete');
75-
$r->get('/get', 'get');
76-
$r->head('/head', 'head');
77-
$r->patch('/patch', 'patch');
78-
$r->post('/post', 'post');
79-
$r->put('/put', 'put');
80-
$r->options('/options', 'options');
52+
$r = self::routeCollector();
53+
54+
$r
55+
->delete('/delete', 'delete')
56+
->get('/get', 'get')
57+
->head('/head', 'head')
58+
->patch('/patch', 'patch')
59+
->post('/post', 'post')
60+
->put('/put', 'put')
61+
->options('/options', 'options');
62+
63+
$r->addGroup('/group-one', static function (ConfigureRoutes $r1): void {
64+
$r1
65+
->delete('/delete', 'delete')
66+
->get('/get', 'get')
67+
->head('/head', 'head')
68+
->patch('/patch', 'patch')
69+
->post('/post', 'post')
70+
->put('/put', 'put')
71+
->options('/options', 'options');
72+
73+
$r1->addGroup('/group-two', static function (ConfigureRoutes $r2): void {
74+
$r2
75+
->delete('/delete', 'delete')
76+
->get('/get', 'get')
77+
->head('/head', 'head')
78+
->patch('/patch', 'patch')
79+
->post('/post', 'post')
80+
->put('/put', 'put')
81+
->options('/options', 'options');
8182
});
8283
});
8384

8485
$r->addGroup('/admin', static function (ConfigureRoutes $r): void {
8586
$r->get('-some-info', 'admin-some-info');
8687
});
88+
8789
$r->addGroup('/admin-', static function (ConfigureRoutes $r): void {
8890
$r->get('more-info', 'admin-more-info');
8991
});
@@ -114,16 +116,14 @@ public function routesCanBeGrouped(): void
114116
['GET', '/admin-more-info', 'admin-more-info', ['_route' => '/admin-more-info']],
115117
];
116118

117-
self::assertObjectHasProperty('routes', $dataGenerator);
118-
self::assertSame($expected, $dataGenerator->routes);
119+
self::assertSame($expected, $r->processedRoutes()[0]);
119120
}
120121

121122
#[PHPUnit\Test]
122123
public function namedRoutesShouldBeRegistered(): void
123124
{
124-
$dataGenerator = self::dummyDataGenerator();
125+
$r = self::routeCollector();
125126

126-
$r = new RouteCollector(new Std(), $dataGenerator);
127127
$r->get('/', 'index-handler', ['_name' => 'index']);
128128
$r->get('/users/me', 'fetch-user-handler', ['_name' => 'users.fetch']);
129129

@@ -133,42 +133,50 @@ public function namedRoutesShouldBeRegistered(): void
133133
#[PHPUnit\Test]
134134
public function cannotDefineRouteWithEmptyName(): void
135135
{
136-
$r = new RouteCollector(new Std(), self::dummyDataGenerator());
136+
$r = self::routeCollector();
137137

138138
$this->expectException(BadRouteException::class);
139+
139140
$r->get('/', 'index-handler', ['_name' => '']);
140141
}
141142

142143
#[PHPUnit\Test]
143144
public function cannotDefineRouteWithInvalidTypeAsName(): void
144145
{
145-
$r = new RouteCollector(new Std(), self::dummyDataGenerator());
146+
$r = self::routeCollector();
146147

147148
$this->expectException(BadRouteException::class);
149+
148150
$r->get('/', 'index-handler', ['_name' => false]);
149151
}
150152

151153
#[PHPUnit\Test]
152154
public function cannotDefineDuplicatedRouteName(): void
153155
{
154-
$r = new RouteCollector(new Std(), self::dummyDataGenerator());
156+
$r = self::routeCollector();
155157

156158
$this->expectException(BadRouteException::class);
159+
157160
$r->get('/', 'index-handler', ['_name' => 'index']);
158161
$r->get('/users/me', 'fetch-user-handler', ['_name' => 'index']);
159162
}
160163

164+
private static function routeCollector(): ConfigureRoutes
165+
{
166+
return new RouteCollector(new Std(), self::dummyDataGenerator());
167+
}
168+
161169
private static function dummyDataGenerator(): DataGenerator
162170
{
163171
return new class implements DataGenerator
164172
{
165173
/** @var list<array{string, string, mixed, array<string, bool|float|int|string>}> */
166-
public array $routes = [];
174+
private array $routes = [];
167175

168176
/** @inheritDoc */
169177
public function getData(): array
170178
{
171-
return [[], []];
179+
return [$this->routes, []];
172180
}
173181

174182
/** @inheritDoc */

0 commit comments

Comments
 (0)