Skip to content

Commit

Permalink
Merge pull request #1207 from hydephp/general-code-polishing
Browse files Browse the repository at this point in the history
Finalize RouteKey class
  • Loading branch information
caendesilva committed Mar 4, 2023
2 parents 3492142 + 9344961 commit 99913ca
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/framework/src/Facades/Route.php
Expand Up @@ -8,7 +8,7 @@
use Hyde\Foundation\Facades\Routes;
use Hyde\Foundation\Kernel\RouteCollection;
use Hyde\Framework\Exceptions\RouteNotFoundException;
use function str_replace;
use Hyde\Support\Models\RouteKey;

/**
* Provides an easy way to access the Hyde pseudo-router.
Expand All @@ -22,7 +22,7 @@ class Route
*/
public static function get(string $routeKey): ?\Hyde\Support\Models\Route
{
return Routes::get(str_replace('.', '/', $routeKey));
return Routes::get(RouteKey::normalize($routeKey));
}

/**
Expand Down
11 changes: 9 additions & 2 deletions packages/framework/src/Support/Models/RouteKey.php
Expand Up @@ -5,10 +5,12 @@
namespace Hyde\Support\Models;

use Stringable;
use function str_replace;
use function unslash;

/**
* Route keys are the core of Hyde's routing system.
* Route keys provide the core bindings of the HydePHP routing system as they are what canonically identifies a page.
* This class both provides a data object for normalized type-hintable values, and general related helper methods.
*
* In short, the route key is the URL path relative to the site webroot, without the file extension.
*
Expand All @@ -29,7 +31,7 @@ public static function make(string $key): self

public function __construct(string $key)
{
$this->key = $key;
$this->key = self::normalize($key);
}

public function __toString(): string
Expand All @@ -42,6 +44,11 @@ public function get(): string
return $this->key;
}

public static function normalize(string $string): string
{
return str_replace('.', '/', $string);
}

/** @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */
public static function fromPage(string $pageClass, string $identifier): self
{
Expand Down
61 changes: 57 additions & 4 deletions packages/framework/tests/Unit/RouteKeyTest.php
Expand Up @@ -4,10 +4,12 @@

namespace Hyde\Framework\Testing\Unit;

use Hyde\Pages\HtmlPage;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\InMemoryPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Pages\DocumentationPage;
use Hyde\Support\Models\RouteKey;
use Hyde\Testing\UnitTestCase;

Expand All @@ -21,23 +23,50 @@ public function testMake()
$this->assertEquals(RouteKey::make('foo'), new RouteKey('foo'));
}

public function test__construct()
public function testConstruct()
{
$this->assertInstanceOf(RouteKey::class, new RouteKey('test'));
}

public function test__toString()
public function testToString()
{
$this->assertSame('foo', (string) new RouteKey('foo'));
$this->assertSame('foo', (new RouteKey('foo'))->__toString());
}

public function testGet()
{
$this->assertSame('foo', (new RouteKey('foo'))->get());
}

public function testCast()
{
$this->assertSame('foo', (string) new RouteKey('foo'));
}

public function testNormalize()
{
$this->assertSame('foo', RouteKey::normalize('foo'));
$this->assertSame('foo/bar', RouteKey::normalize('foo/bar'));
$this->assertSame('foo/bar', RouteKey::normalize('foo.bar'));
}

public function testConstructorValuesAreNormalized()
{
$this->assertEquals(new RouteKey('foo'), new RouteKey('foo'));
$this->assertEquals(new RouteKey('foo/bar'), new RouteKey('foo/bar'));
$this->assertEquals(new RouteKey('foo/bar'), new RouteKey('foo.bar'));
}

public function testStaticConstructorValuesAreNormalized()
{
$this->assertEquals(RouteKey::make('foo'), RouteKey::make('foo'));
$this->assertEquals(RouteKey::make('foo/bar'), RouteKey::make('foo/bar'));
$this->assertEquals(RouteKey::make('foo/bar'), RouteKey::make('foo.bar'));
}

public function testFromPage()
{
$this->assertEquals(new RouteKey('foo'), RouteKey::fromPage(HtmlPage::class, 'foo'));
$this->assertEquals(new RouteKey('foo'), RouteKey::fromPage(BladePage::class, 'foo'));
$this->assertEquals(new RouteKey('foo'), RouteKey::fromPage(MarkdownPage::class, 'foo'));
$this->assertEquals(new RouteKey('posts/foo'), RouteKey::fromPage(MarkdownPost::class, 'foo'));
Expand All @@ -46,9 +75,33 @@ public function testFromPage()

public function testFromPageWithNestedIdentifier()
{
$this->assertEquals(new RouteKey('foo/bar'), RouteKey::fromPage(HtmlPage::class, 'foo/bar'));
$this->assertEquals(new RouteKey('foo/bar'), RouteKey::fromPage(BladePage::class, 'foo/bar'));
$this->assertEquals(new RouteKey('foo/bar'), RouteKey::fromPage(MarkdownPage::class, 'foo/bar'));
$this->assertEquals(new RouteKey('posts/foo/bar'), RouteKey::fromPage(MarkdownPost::class, 'foo/bar'));
$this->assertEquals(new RouteKey('docs/foo/bar'), RouteKey::fromPage(DocumentationPage::class, 'foo/bar'));
}

public function testFromPageWithInMemoryPage()
{
$this->assertEquals(new RouteKey('foo'), RouteKey::fromPage(InMemoryPage::class, 'foo'));
$this->assertEquals(new RouteKey('foo/bar'), RouteKey::fromPage(InMemoryPage::class, 'foo/bar'));
}

public function testFromPageWithCustomOutputDirectory()
{
MarkdownPage::setOutputDirectory('foo');
$this->assertEquals(new RouteKey('foo/bar'), RouteKey::fromPage(MarkdownPage::class, 'bar'));
}

public function testFromPageWithCustomNestedOutputDirectory()
{
MarkdownPage::setOutputDirectory('foo/bar');
$this->assertEquals(new RouteKey('foo/bar/baz'), RouteKey::fromPage(MarkdownPage::class, 'baz'));
}

public static function tearDownAfterClass(): void
{
MarkdownPage::setOutputDirectory('');
}
}

0 comments on commit 99913ca

Please sign in to comment.