mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-27 07:44:33 +02:00
Improved Breadcrumbs tests
This commit is contained in:
@@ -14,14 +14,20 @@ class Breadcrumbs extends ViewFunction
|
||||
/** @var Container The application container */
|
||||
protected $container;
|
||||
|
||||
/** @var string The directory separator */
|
||||
protected $directorySeparator;
|
||||
|
||||
/**
|
||||
* Create a new Breadcrumbs object.
|
||||
*
|
||||
* @param \DI\Container $container
|
||||
*/
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
public function __construct(
|
||||
Container $container,
|
||||
string $directorySeparator = DIRECTORY_SEPARATOR
|
||||
) {
|
||||
$this->container = $container;
|
||||
$this->directorySeparator = $directorySeparator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,15 +39,15 @@ class Breadcrumbs extends ViewFunction
|
||||
*/
|
||||
public function __invoke(string $path)
|
||||
{
|
||||
$breadcrumbs = Str::explode($path, DIRECTORY_SEPARATOR)->diff(
|
||||
explode(DIRECTORY_SEPARATOR, $this->container->get('base_path'))
|
||||
$breadcrumbs = Str::explode($path, $this->directorySeparator)->diff(
|
||||
explode($this->directorySeparator, $this->container->get('base_path'))
|
||||
)->filter();
|
||||
|
||||
return $breadcrumbs->filter(function (string $crumb) {
|
||||
return $crumb !== '.';
|
||||
})->reduce(function (Collection $carry, string $crumb) {
|
||||
return $carry->put($crumb, ltrim(
|
||||
$carry->last() . DIRECTORY_SEPARATOR . urlencode($crumb), DIRECTORY_SEPARATOR
|
||||
$carry->last() . $this->directorySeparator . urlencode($crumb), $this->directorySeparator
|
||||
));
|
||||
}, new Collection)->map(function (string $path): string {
|
||||
return sprintf('?dir=%s', $path);
|
||||
|
@@ -58,4 +58,15 @@ class BreadcrumbsTest extends TestCase
|
||||
'baz' => '?dir=foo/bar/baz',
|
||||
]), $breadcrumbs('foo/bar/baz'));
|
||||
}
|
||||
|
||||
public function test_it_can_parse_breadcrumbs_from_the_path_with_back_slashes(): void
|
||||
{
|
||||
$breadcrumbs = new Breadcrumbs($this->container, '\\');
|
||||
|
||||
$this->assertEquals(Collection::make([
|
||||
'foo' => '?dir=foo',
|
||||
'bar' => '?dir=foo\bar',
|
||||
'baz' => '?dir=foo\bar\baz',
|
||||
]), $breadcrumbs('foo\bar\baz'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user