Skip to content

Commit

Permalink
Clean-up RouteCollectorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Apr 22, 2024
1 parent 3efa22b commit 29d9dd7
Showing 1 changed file with 58 additions and 50 deletions.
108 changes: 58 additions & 50 deletions test/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ final class RouteCollectorTest extends TestCase
#[PHPUnit\Test]
public function shortcutsCanBeUsedToRegisterRoutes(): void
{
$dataGenerator = self::dummyDataGenerator();
$r = new RouteCollector(new Std(), $dataGenerator);

$r->any('/any', 'any');
$r->delete('/delete', 'delete');
$r->get('/get', 'get');
$r->head('/head', 'head');
$r->patch('/patch', 'patch');
$r->post('/post', 'post');
$r->put('/put', 'put');
$r->options('/options', 'options');
$r = self::routeCollector();

$r
->any('/any', 'any')
->delete('/delete', 'delete')
->get('/get', 'get')
->head('/head', 'head')
->patch('/patch', 'patch')
->post('/post', 'post')
->put('/put', 'put')
->options('/options', 'options');

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

self::assertObjectHasProperty('routes', $dataGenerator);
self::assertSame($expected, $dataGenerator->routes);
self::assertSame($expected, $r->processedRoutes()[0]);

Check failure on line 46 in test/RouteCollectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis (locked, 8.1, ubuntu-latest)

Call to static method PHPUnit\Framework\Assert::assertSame() with array{array{'*', '/any', 'any', array{_route: '/any'}}, array{'DELETE', '/delete', 'delete', array{_route: '/delete'}}, array{'GET', '/get', 'get', array{_route: '/get'}}, array{'HEAD', '/head', 'head', array{_route: '/head'}}, array{'PATCH', '/patch', 'patch', array{_route: '/patch'}}, array{'POST', '/post', 'post', array{_route: '/post'}}, array{'PUT', '/put', 'put', array{_route: '/put'}}, array{'OPTIONS', '/options', 'options', array{_route: '/options'}}} and array<string, array<string, array{mixed, array<string, bool|float|int|string>}>> will always evaluate to false.
}

#[PHPUnit\Test]
public function routesCanBeGrouped(): void
{
$dataGenerator = self::dummyDataGenerator();
$r = new RouteCollector(new Std(), $dataGenerator);

$r->delete('/delete', 'delete');
$r->get('/get', 'get');
$r->head('/head', 'head');
$r->patch('/patch', 'patch');
$r->post('/post', 'post');
$r->put('/put', 'put');
$r->options('/options', 'options');

$r->addGroup('/group-one', static function (ConfigureRoutes $r): void {
$r->delete('/delete', 'delete');
$r->get('/get', 'get');
$r->head('/head', 'head');
$r->patch('/patch', 'patch');
$r->post('/post', 'post');
$r->put('/put', 'put');
$r->options('/options', 'options');

$r->addGroup('/group-two', static function (ConfigureRoutes $r): void {
$r->delete('/delete', 'delete');
$r->get('/get', 'get');
$r->head('/head', 'head');
$r->patch('/patch', 'patch');
$r->post('/post', 'post');
$r->put('/put', 'put');
$r->options('/options', 'options');
$r = self::routeCollector();

$r
->delete('/delete', 'delete')
->get('/get', 'get')
->head('/head', 'head')
->patch('/patch', 'patch')
->post('/post', 'post')
->put('/put', 'put')
->options('/options', 'options');

$r->addGroup('/group-one', static function (ConfigureRoutes $r1): void {
$r1
->delete('/delete', 'delete')
->get('/get', 'get')
->head('/head', 'head')
->patch('/patch', 'patch')
->post('/post', 'post')
->put('/put', 'put')
->options('/options', 'options');

$r1->addGroup('/group-two', static function (ConfigureRoutes $r2): void {
$r2
->delete('/delete', 'delete')
->get('/get', 'get')
->head('/head', 'head')
->patch('/patch', 'patch')
->post('/post', 'post')
->put('/put', 'put')
->options('/options', 'options');
});
});

$r->addGroup('/admin', static function (ConfigureRoutes $r): void {
$r->get('-some-info', 'admin-some-info');
});

$r->addGroup('/admin-', static function (ConfigureRoutes $r): void {
$r->get('more-info', 'admin-more-info');
});
Expand Down Expand Up @@ -114,16 +116,14 @@ public function routesCanBeGrouped(): void
['GET', '/admin-more-info', 'admin-more-info', ['_route' => '/admin-more-info']],
];

self::assertObjectHasProperty('routes', $dataGenerator);
self::assertSame($expected, $dataGenerator->routes);
self::assertSame($expected, $r->processedRoutes()[0]);

Check failure on line 119 in test/RouteCollectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis (locked, 8.1, ubuntu-latest)

Call to static method PHPUnit\Framework\Assert::assertSame() with array{array{'DELETE', '/delete', 'delete', array{_route: '/delete'}}, array{'GET', '/get', 'get', array{_route: '/get'}}, array{'HEAD', '/head', 'head', array{_route: '/head'}}, array{'PATCH', '/patch', 'patch', array{_route: '/patch'}}, array{'POST', '/post', 'post', array{_route: '/post'}}, array{'PUT', '/put', 'put', array{_route: '/put'}}, array{'OPTIONS', '/options', 'options', array{_route: '/options'}}, array{'DELETE', '/group-one/delete', 'delete', array{_route: '/group-one/delete'}}, ...} and array<string, array<string, array{mixed, array<string, bool|float|int|string>}>> will always evaluate to false.
}

#[PHPUnit\Test]
public function namedRoutesShouldBeRegistered(): void
{
$dataGenerator = self::dummyDataGenerator();
$r = self::routeCollector();

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

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

$this->expectException(BadRouteException::class);

$r->get('/', 'index-handler', ['_name' => '']);
}

#[PHPUnit\Test]
public function cannotDefineRouteWithInvalidTypeAsName(): void
{
$r = new RouteCollector(new Std(), self::dummyDataGenerator());
$r = self::routeCollector();

$this->expectException(BadRouteException::class);

$r->get('/', 'index-handler', ['_name' => false]);
}

#[PHPUnit\Test]
public function cannotDefineDuplicatedRouteName(): void
{
$r = new RouteCollector(new Std(), self::dummyDataGenerator());
$r = self::routeCollector();

$this->expectException(BadRouteException::class);

$r->get('/', 'index-handler', ['_name' => 'index']);
$r->get('/users/me', 'fetch-user-handler', ['_name' => 'index']);
}

private static function routeCollector(): ConfigureRoutes
{
return new RouteCollector(new Std(), self::dummyDataGenerator());
}

private static function dummyDataGenerator(): DataGenerator
{
return new class implements DataGenerator
{
/** @var list<array{string, string, mixed, array<string, bool|float|int|string>}> */
public array $routes = [];
private array $routes = [];

/** @inheritDoc */
public function getData(): array
{
return [[], []];
return [$this->routes, []];

Check failure on line 179 in test/RouteCollectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis (locked, 8.1, ubuntu-latest)

Method class@anonymous/test/RouteCollectorTest.php:171::getData() should return array{array<string, array<string, array{mixed, array<string, bool|float|int|string>}>>, array<string, array<int, array{regex: string, suffix?: string, routeMap: array<int|string, array{mixed, array<string, string>, array<string, bool|float|int|string>}>}>>} but returns array{array<int, array{string, string, mixed, array<string, bool|float|int|string>}>, array{}}.
}

/** @inheritDoc */
Expand Down

0 comments on commit 29d9dd7

Please sign in to comment.