generated from spatie/package-skeleton-laravel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from RobertBoes/feat/customizable-searlization
- Loading branch information
Showing
6 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace RobertBoes\InertiaBreadcrumbs; | ||
|
||
class InertiaBreadcrumbs | ||
{ | ||
/** | ||
* The callback that is responsible serializing breadcrumbs to the frontend | ||
* | ||
* @var callable|null | ||
*/ | ||
public static $serializeUsingCallback; | ||
|
||
public static function serializeUsing(callable $callback) | ||
{ | ||
static::$serializeUsingCallback = $callback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace RobertBoes\InertiaBreadcrumbs\Tests; | ||
|
||
use Diglactic\Breadcrumbs\Breadcrumbs; | ||
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail; | ||
use Inertia\Inertia; | ||
use Inertia\Testing\AssertableInertia as Assert; | ||
use RobertBoes\InertiaBreadcrumbs\Breadcrumb; | ||
use RobertBoes\InertiaBreadcrumbs\InertiaBreadcrumbs; | ||
use RobertBoes\InertiaBreadcrumbs\Middleware; | ||
|
||
class SerializationTest extends TestCase | ||
{ | ||
/** | ||
* @param \Illuminate\Routing\Router $router | ||
*/ | ||
public function defineRoutes($router): void | ||
{ | ||
$router->get('/home', function () { | ||
return Inertia::render('Home', []); | ||
})->name('home')->middleware([Middleware::class]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_serializes_breadcrumbs(): void | ||
{ | ||
Breadcrumbs::for('home', function (BreadcrumbTrail $trail) { | ||
$trail->push('Home', route('home')); | ||
}); | ||
|
||
$this->getJson('/home') | ||
->assertOk() | ||
->assertInertia( | ||
fn (Assert $page) => $page | ||
->component('Home') | ||
->has( | ||
'breadcrumbs', | ||
1, | ||
fn (Assert $page) => $page | ||
->where('title', 'Home') | ||
->where('url', route('home')) | ||
->where('current', true) | ||
->missing('data') | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_can_use_a_custom_serializer(): void | ||
{ | ||
Breadcrumbs::for('home', function (BreadcrumbTrail $trail) { | ||
$trail->push('Home', route('home')); | ||
}); | ||
|
||
InertiaBreadcrumbs::serializeUsing(fn (Breadcrumb $breadcrumb) => [ | ||
'name' => $breadcrumb->title(), | ||
'href' => $breadcrumb->url(), | ||
'active' => $breadcrumb->current(), | ||
'data' => $breadcrumb->data(), | ||
]); | ||
|
||
$this->getJson('/home') | ||
->assertOk() | ||
->assertInertia( | ||
fn (Assert $page) => $page | ||
->component('Home') | ||
->has( | ||
'breadcrumbs', | ||
1, | ||
fn (Assert $page) => $page | ||
->where('name', 'Home') | ||
->where('href', route('home')) | ||
->where('active', true) | ||
->where('data', []) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters