diff --git a/app/ViewFunctions/Breadcrumbs.php b/app/ViewFunctions/Breadcrumbs.php index 415053d..3989dd1 100644 --- a/app/ViewFunctions/Breadcrumbs.php +++ b/app/ViewFunctions/Breadcrumbs.php @@ -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, '/'); + }); } } diff --git a/tests/ViewFunctions/BreadcrumbsTest.php b/tests/ViewFunctions/BreadcrumbsTest.php index 5fb9359..00a8a5a 100644 --- a/tests/ViewFunctions/BreadcrumbsTest.php +++ b/tests/ViewFunctions/BreadcrumbsTest.php @@ -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')); } }