Skip to content

Commit 2702089

Browse files
committed
Improve IgnoredRoutesStorage API
1 parent 3304559 commit 2702089

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/IgnoredRoutesStorage.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public function __construct(
1414
) {
1515
}
1616

17+
public function reset(): void
18+
{
19+
file_put_contents($this->file, '');
20+
}
21+
1722
public function saveRoute(string $route): void
1823
{
1924
if (!file_exists($this->file)) {
@@ -23,6 +28,18 @@ public function saveRoute(string $route): void
2328
file_put_contents($this->file, "$route\n", \FILE_APPEND);
2429
}
2530

31+
/**
32+
* @param string[] $routes
33+
*/
34+
public function saveRoutes(array $routes): void
35+
{
36+
if (!file_exists($this->file)) {
37+
touch($this->file);
38+
}
39+
40+
file_put_contents($this->file, implode(\PHP_EOL, $routes), \FILE_APPEND);
41+
}
42+
2643
/**
2744
* @return string[]
2845
*/
@@ -36,6 +53,6 @@ public function getRoutes(): array
3653
throw new \RuntimeException('Unable to load ignored routes from given file.');
3754
}
3855

39-
return array_unique($routes);
56+
return array_values(array_unique($routes));
4057
}
4158
}

tests/IgnoredRoutesStorageTest.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@ public function testStorage(): void
1313
{
1414
$storage = new IgnoredRoutesStorage(__DIR__.'/../var/cache/test_ignored_routes');
1515

16+
$storage->reset();
17+
18+
$this->assertSame([], $storage->getRoutes());
19+
20+
$storage->saveRoutes(['route5', 'route6', 'route7']);
21+
22+
$this->assertSame(['route5', 'route6', 'route7'], $storage->getRoutes());
23+
24+
$storage->reset();
25+
26+
$this->assertSame([], $storage->getRoutes());
27+
1628
$storage->saveRoute('route1');
1729
$storage->saveRoute('route2');
1830
$storage->saveRoute('route3');
1931
$storage->saveRoute('route2');
32+
$storage->saveRoutes(['route2', 'route4', 'route5']);
2033

21-
$this->assertSame(['route1', 'route2', 'route3'], $storage->getRoutes());
34+
$this->assertSame(['route1', 'route2', 'route3', 'route4', 'route5'], $storage->getRoutes());
2235
}
2336
}

0 commit comments

Comments
 (0)