Skip to content

Commit 5197723

Browse files
committed
Add LazyBreadcrumb functionality and initial test cases
Introduce the LazyBreadcrumb class with basic methods to manage breadcrumbs. Add the `breadcrumbs` helper function and implement initial test cases to verify breadcrumb creation and addition. Update configuration to include a default breadcrumb entry.
1 parent 254c8ab commit 5197723

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

config/lazy-breadcrumb.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22

33
// config for Step2Dev/LazyBreadcrumb
44
return [
5+
'default' => [
6+
[
7+
'url' => '/',//route('home'),
8+
'label' => env('APP_NAME'),
9+
],
10+
],
511
];

src/LazyBreadcrumb.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,31 @@
22

33
namespace Step2Dev\LazyBreadcrumb;
44

5-
class LazyBreadcrumb {}
5+
class LazyBreadcrumb
6+
{
7+
/**
8+
* @var array|array[]
9+
*/
10+
private array $breadcrumbs;
11+
12+
public function __construct()
13+
{
14+
$this->breadcrumbs = (array) config('lazy-breadcrumb.default', []);
15+
}
16+
17+
public function add(string|null $url, string|null $label): static
18+
{
19+
if (! $url && ! $label) {
20+
return $this;
21+
}
22+
23+
$this->breadcrumbs[] = array_filter(compact('url', 'label'));
24+
25+
return $this;
26+
}
27+
28+
public function getBreadcrumbs(): array
29+
{
30+
return $this->breadcrumbs;
31+
}
32+
}

src/helpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Step2Dev\LazyBreadcrumb\LazyBreadcrumb;
4+
5+
if (! function_exists('breadcrumbs')) {
6+
/**
7+
* Generate breadcrumbs.
8+
*
9+
* @return LazyBreadcrumb
10+
*/
11+
function breadcrumbs(): LazyBreadcrumb
12+
{
13+
return new LazyBreadcrumb();
14+
}
15+
}

tests/LazyBreadcrumbTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Step2Dev\LazyBreadcrumb\Tests;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use PHPUnit\Framework\TestCase;
7+
use Step2Dev\LazyBreadcrumb\LazyBreadcrumb;
8+
9+
class LazyBreadcrumbTest extends TestCase
10+
{
11+
public function testInitialBreadcrumbs()
12+
{
13+
$breadcrumbs = new LazyBreadcrumb();
14+
$this->assertEquals(Config::get('lazy_breadcrumb.default'), $breadcrumbs->getBreadcrumbs());
15+
}
16+
17+
public function testAddBreadcrumb()
18+
{
19+
$breadcrumbs = new LazyBreadcrumb();
20+
$breadcrumbs->add('/new-url', 'New Label');
21+
22+
$this->assertEquals([
23+
Config::get('lazy_breadcrumb.default'),
24+
[
25+
'url' => '/new-url',
26+
'label' => 'New Label',
27+
],
28+
], $breadcrumbs->getBreadcrumbs());
29+
}
30+
}

0 commit comments

Comments
 (0)