Skip to content

Commit a10f18d

Browse files
committedJul 16, 2024·
Keep a trace of each routes return code if available
1 parent 5d39afe commit a10f18d

10 files changed

+129
-19
lines changed
 

‎src/Command/CheckCommand.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Symfony\Component\Console\Input\InputOption;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Symfony\Component\Console\Style\SymfonyStyle;
13+
use Tiime\TestedRoutesCheckerBundle\IgnoredRoutesStorage;
1314
use Tiime\TestedRoutesCheckerBundle\RoutesChecker;
14-
use Tiime\TestedRoutesCheckerBundle\RouteStorage\FileRouteStorage;
1515

1616
#[AsCommand(
1717
name: 'tiime:tested-routes-checker:check',
@@ -44,10 +44,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4444

4545
/** @var string $routesToIgnoreFile */
4646
$routesToIgnoreFile = $input->getOption('routes-to-ignore');
47-
$fileRouteStorage = new FileRouteStorage($routesToIgnoreFile);
47+
$ignoredRoutesStorage = new IgnoredRoutesStorage($routesToIgnoreFile);
4848

4949
try {
50-
$routesToIgnore = $fileRouteStorage->getRoutes();
50+
$routesToIgnore = $ignoredRoutesStorage->getRoutes();
5151
} catch (\InvalidArgumentException $e) {
5252
$io->warning('Unable to load the given file containing routes to ignore.');
5353
}
@@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8484
$this->showTestedIgnoredRoutesSection($io, $testedIgnoredRoutes);
8585

8686
if ($input->getOption('generate-baseline')) {
87-
$fileRouteStorage
87+
$ignoredRoutesStorage
8888
->saveRoute(
8989
implode(\PHP_EOL, $untestedRoutes)
9090
);

‎src/EventListener/KernelRequestListener.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function __invoke(RequestEvent $event): void
2020
return;
2121
}
2222

23-
$this->routeStorage->saveRoute($routeName);
23+
if (null === $response = $event->getResponse()) {
24+
return;
25+
}
26+
27+
$this->routeStorage->saveRoute($routeName, $response->getStatusCode());
2428
}
2529
}

‎src/IgnoredRoutesStorage.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tiime\TestedRoutesCheckerBundle;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class IgnoredRoutesStorage
11+
{
12+
public function __construct(
13+
private readonly string $file,
14+
) {
15+
}
16+
17+
public function saveRoute(string $route): void
18+
{
19+
if (!file_exists($this->file)) {
20+
touch($this->file);
21+
}
22+
23+
file_put_contents($this->file, "$route\n", \FILE_APPEND);
24+
}
25+
26+
/**
27+
* @return string[]
28+
*/
29+
public function getRoutes(): array
30+
{
31+
if (!file_exists($this->file)) {
32+
throw new \InvalidArgumentException("File \"{$this->file}\"does not exists, unable to load ignored routes!");
33+
}
34+
35+
if (false === $routes = @file($this->file, \FILE_IGNORE_NEW_LINES)) {
36+
throw new \RuntimeException('Unable to load ignored routes from given file.');
37+
}
38+
39+
return array_unique($routes);
40+
}
41+
}

‎src/RouteStorage/FileRouteStorage.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public function __construct(
1515
}
1616

1717
#[\Override]
18-
public function saveRoute(string $route): void
18+
public function saveRoute(string $route, int $statusCode): void
1919
{
2020
if (!file_exists($this->file)) {
2121
touch($this->file);
2222
}
2323

24-
file_put_contents($this->file, "$route\n", \FILE_APPEND);
24+
file_put_contents($this->file, "$route|$statusCode\n", \FILE_APPEND);
2525
}
2626

2727
#[\Override]
@@ -35,6 +35,23 @@ public function getRoutes(): array
3535
throw new \RuntimeException('Unable to load routes from given file.');
3636
}
3737

38-
return array_unique($routes);
38+
$filteredRoutes = [];
39+
40+
foreach ($routes as $route) {
41+
$parts = explode('|', $route);
42+
43+
$name = $parts[0];
44+
// In order to avoid BC break, we consider a route without a status code as a 200 OK.
45+
// To be removed in 2.0
46+
$statusCode = (int) ($parts[1] ?? 200);
47+
48+
if (!\array_key_exists($name, $filteredRoutes)) {
49+
$filteredRoutes[$name] = [$statusCode];
50+
} else {
51+
$filteredRoutes[$name][] = $statusCode;
52+
}
53+
}
54+
55+
return $filteredRoutes;
3956
}
4057
}

‎src/RouteStorage/RouteStorageInterface.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
*/
1010
interface RouteStorageInterface
1111
{
12-
public function saveRoute(string $route): void;
12+
public function saveRoute(string $route, int $statusCode): void;
1313

1414
/**
15-
* @return string[]
15+
* Return an array with the route name as key and all known return codes
16+
* (including duplicates).
17+
*
18+
* @return array<string, int[]>
1619
*/
1720
public function getRoutes(): array;
1821
}

‎src/RoutesChecker.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getUntestedRoutes(array $routesToIgnore = []): array
2424
{
2525
$routesToIgnore = array_merge($this->getDefaultRoutesToIgnore(), $routesToIgnore);
2626

27-
$testedRoutes = $this->routeStorage->getRoutes();
27+
$testedRoutes = array_keys($this->routeStorage->getRoutes());
2828

2929
$routes = array_keys($this->router->getRouteCollection()->all());
3030
$untestedRoutes = array_diff($routes, $testedRoutes);
@@ -55,7 +55,7 @@ public function getUntestedRoutes(array $routesToIgnore = []): array
5555
*/
5656
public function getTestedIgnoredRoutes(array $routesToIgnore = []): array
5757
{
58-
$testedRoutes = $this->routeStorage->getRoutes();
58+
$testedRoutes = array_keys($this->routeStorage->getRoutes());
5959

6060
return array_values(array_intersect($testedRoutes, $routesToIgnore));
6161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
route1
2+
route2
3+
route3
4+
route1
5+
route2
6+
route3
7+
route2

‎tests/IgnoredRoutesStorageTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tiime\TestedRoutesCheckerBundle\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Tiime\TestedRoutesCheckerBundle\IgnoredRoutesStorage;
9+
10+
final class IgnoredRoutesStorageTest extends TestCase
11+
{
12+
public function testStorage(): void
13+
{
14+
$storage = new IgnoredRoutesStorage(__DIR__.'/../var/cache/test_ignored_routes');
15+
16+
$storage->saveRoute('route1');
17+
$storage->saveRoute('route2');
18+
$storage->saveRoute('route3');
19+
$storage->saveRoute('route2');
20+
21+
$this->assertSame(['route1', 'route2', 'route3'], $storage->getRoutes());
22+
}
23+
}

‎tests/RouteStorage/FileRouteStorageTest.php

+21-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,28 @@ final class FileRouteStorageTest extends TestCase
1111
{
1212
public function testStorage(): void
1313
{
14-
$storage = new FileRouteStorage(__DIR__.'/../../var/cache/test_cache_file');
14+
$storage = new FileRouteStorage(__DIR__.'/../../var/cache/test_cache_file_'.bin2hex(random_bytes(5)));
1515

16-
$storage->saveRoute('route1');
17-
$storage->saveRoute('route2');
18-
$storage->saveRoute('route3');
19-
$storage->saveRoute('route2');
16+
$storage->saveRoute('route1', 200);
17+
$storage->saveRoute('route2', 500);
18+
$storage->saveRoute('route3', 403);
19+
$storage->saveRoute('route2', 401);
2020

21-
$this->assertSame(['route1', 'route2', 'route3'], $storage->getRoutes());
21+
$this->assertSame([
22+
'route1' => [200],
23+
'route2' => [500, 401],
24+
'route3' => [403],
25+
], $storage->getRoutes());
26+
}
27+
28+
public function testWithStorageWithoutStatusCode(): void
29+
{
30+
$storage = new FileRouteStorage(__DIR__.'/../Fixtures/file_containing_one_route_per_row_with_duplicates');
31+
32+
$this->assertSame([
33+
'route1' => [200, 200],
34+
'route2' => [200, 200, 200],
35+
'route3' => [200, 200],
36+
], $storage->getRoutes());
2237
}
2338
}

‎tests/RoutesCheckerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testGetTestedIgnoredRoutes(): void
2828
{
2929
$this->routeStorage->expects($this->once())
3030
->method('getRoutes')
31-
->willReturn(['route1', 'route2']);
31+
->willReturn(['route1' => [200], 'route2' => [404]]);
3232

3333
$testedIgnoredRoutes = $this->routesChecker->getTestedIgnoredRoutes(['route2', 'route3']);
3434

0 commit comments

Comments
 (0)
Please sign in to comment.