Updated breadcrumbs view function to handle running from a sub-directory

This commit is contained in:
Chris Kankiewicz
2020-01-26 00:16:05 -07:00
parent d5924445a8
commit 2f87c29e5d
2 changed files with 22 additions and 8 deletions

View File

@@ -24,10 +24,10 @@ class Breadcrumbs extends ViewFunction
return $breadcrumbs->filter(function (string $crumb) {
return $crumb !== '.';
})->reduce(function (array $carry, string $crumb) {
$carry[$crumb] = end($carry) . "/{$crumb}";
return $carry;
}, []);
})->reduce(function (Collection $carry, string $crumb) {
return $carry->put($crumb, $carry->last() . '/' . $crumb);
}, new Collection)->map(function (string $path) {
return '/' . ltrim(dirname($_SERVER['SCRIPT_NAME']) . $path, '/');
});
}
}

View File

@@ -4,6 +4,7 @@ namespace Tests\ViewFunctions;
use App\ViewFunctions\Breadcrumbs;
use Tests\TestCase;
use Tightenco\Collect\Support\Collection;
class BreadcrumbsTest extends TestCase
{
@@ -11,17 +12,30 @@ class BreadcrumbsTest extends TestCase
{
$breadcrumbs = new Breadcrumbs($this->container, $this->config);
$this->assertEquals([
$this->assertEquals(Collection::make([
'foo' => '/foo',
'bar' => '/foo/bar',
'baz' => '/foo/bar/baz',
], $breadcrumbs('foo/bar/baz'));
]), $breadcrumbs('foo/bar/baz'));
}
public function test_it_can_parse_breadcrumbs_for_dot_path(): void
{
$breadcrumbs = new Breadcrumbs($this->container, $this->config);
$this->assertEquals([], $breadcrumbs('.'));
$this->assertEquals(new Collection, $breadcrumbs('.'));
}
public function test_it_can_parse_breadcrumbs_from_a_subdirectory(): void
{
$_SERVER['SCRIPT_NAME'] = '/some/dir/index.php';
$breadcrumbs = new Breadcrumbs($this->container, $this->config);
$this->assertEquals(Collection::make([
'foo' => '/some/dir/foo',
'bar' => '/some/dir/foo/bar',
'baz' => '/some/dir/foo/bar/baz',
]), $breadcrumbs('foo/bar/baz'));
}
}