Skip to content

Commit

Permalink
Refactoring collision list & improve the test
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Aug 22, 2024
1 parent 407c272 commit 7b501f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static function routeCollisionList()
{
return collect(\Illuminate\Support\Facades\Route::getRoutes()->get())
->map(fn(\Illuminate\Routing\Route $route) => $route->uri)
->reject(fn($value) => !preg_match('/^[a-zA-Z\-]+$/', $value))
->unique()->sort()
->pipe(fn($value) => self::collisionCandidateFilter($value))
->sort()
->toArray();
}

Expand All @@ -86,12 +86,19 @@ public static function publicPathCollisionList()
}

return collect($publicPathList)
// remove ., ..,
->reject(fn($value) => in_array($value, ['.', '..']))
// remove file with extension
->filter(fn($value) => !preg_match('/\.[a-z]+$/', $value))
// remove array value which is in config('urlhub.reserved_keyword')
->reject(fn($value) => in_array($value, config('urlhub.reserved_keyword')))
->pipe(fn($value) => self::collisionCandidateFilter($value))
->toArray();
}

/**
* @param \Illuminate\Support\Collection $value
* @return \Illuminate\Support\Collection
*/
public static function collisionCandidateFilter($value)
{
return collect($value)
->filter(fn($value) => preg_match('/^([0-9a-zA-Z\-])+$/', $value))
->reject(fn($value) => in_array($value, config('urlhub.reserved_keyword')))
->unique();
}
}
31 changes: 31 additions & 0 deletions tests/Unit/Helpers/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,35 @@ public function test_n_abb(): void
$this->assertSame('6.79K', \Illuminate\Support\Number::abbreviate(6789, maxPrecision: 2));
$this->assertSame('6.79K', n_abb(6789));
}

public function testCollisionCandidateFilter(): void
{
$actual = array_merge(
[
'css',
'reset-password',

'.',
'..',
'.htaccess',
'favicon.ico',

'+{url}',
'/',
'_debugbar',
'_debugbar/assets/javascript',
'admin/about',
'admin/user/{user}/changepassword',
'admin/links/u/{user}',
],
config('urlhub.reserved_keyword'),
);

$expected = ['css', 'reset-password'];

$this->assertEquals(
$expected,
Helper::collisionCandidateFilter($actual)->toArray(),
);
}
}

0 comments on commit 7b501f1

Please sign in to comment.