Skip to content

Commit

Permalink
Inline the navigation destination class
Browse files Browse the repository at this point in the history
Turns out we really didn't need this. It does add one test for the new side effect of constructor now being less magic (desirable as it's the scope of the create method)
  • Loading branch information
caendesilva committed Mar 24, 2024
1 parent 1b8cd1b commit d3b2044
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 49 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class NavigationItem implements NavigationElement, Stringable
{
protected NavigationDestination $destination;
protected string|Route $destination;
protected string $label;
protected int $priority;

Expand All @@ -40,7 +40,7 @@ class NavigationItem implements NavigationElement, Stringable
*/
public function __construct(Route|string $destination, string $label, int $priority = NavigationMenu::DEFAULT, ?string $group = null)
{
$this->destination = new NavigationDestination($destination);
$this->destination = $destination;

$this->label = $label;
$this->priority = $priority;
Expand Down Expand Up @@ -87,15 +87,15 @@ public function __toString(): string
*/
public function getPage(): ?HydePage
{
return $this->destination->getRoute()?->getPage();
return $this->destination instanceof Route ? $this->destination->getPage() : null;
}

/**
* Resolve the destination link of the navigation item.
*/
public function getLink(): string
{
return $this->destination->getLink();
return (string) $this->destination;
}

/**
Expand Down
15 changes: 11 additions & 4 deletions packages/framework/tests/Unit/NavigationItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ public function testPassingRouteInstanceToConstructorUsesRouteInstance()
$this->assertEquals($route, (new NavigationItem($route, 'Home'))->getPage()->getRoute());
}

public function testPassingRouteKeyToConstructorUsesRouteInstance()
public function testPassingRouteKeyToConstructorUsesDestinationAsLink()
{
$route = Routes::get('index');
$item = new NavigationItem('index', 'Home');
$this->assertNotNull($item->getPage());
$this->assertSame($route->getPage(), $item->getPage());
$this->assertNull($item->getPage());
$this->assertSame('index', $item->getLink());
}

public function testPassingUrlToConstructorSetsRouteToNull()
Expand Down Expand Up @@ -209,6 +208,14 @@ public function testCreateWithCustomPriority()
$this->assertSame(100, NavigationItem::create(Routes::get('index'), 'foo', 100)->getPriority());
}

public function testPassingRouteKeyToStaticConstructorUsesRouteInstance()
{
$route = Routes::get('index');
$item = NavigationItem::create('index', 'Home');
$this->assertNotNull($item->getPage());
$this->assertSame($route->getPage(), $item->getPage());
}

public function testRouteBasedNavigationItemDestinationsAreResolvedRelatively()
{
Render::swap(Mockery::mock(RenderData::class, [
Expand Down

0 comments on commit d3b2044

Please sign in to comment.