Skip to content

Commit

Permalink
Merge pull request #6 from PhpGt/5-dynamic-get
Browse files Browse the repository at this point in the history
feature: dynamic path without a get parameter returns deepest dynamic match
  • Loading branch information
g105b authored Nov 3, 2021
2 parents fc0eaf1 + 83053cf commit 7b5080b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example/01-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// Request 1: A page request, as if it is sent from a web browser.
$pageRequest = new Request(
"GET",
new Uri("/shop/furniture/chair"),
new Uri("/shop/phone/oneplus"),
new RequestHeaders([
// An example accept header from Firefox when requesting a normal link:
"Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions example/project/simple-site/page/explorer/topic/@id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function go(\Gt\Routing\Path\DynamicPath $path) {
echo "The topic is: " . $path->get("id"), PHP_EOL;
}
7 changes: 6 additions & 1 deletion src/Path/DynamicPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(
$this->assemblyList = $assemblyList;
}

public function get(string $key):?string {
public function get(string $key = null):?string {
$requestPathParts = explode("/", $this->requestPath);

foreach($this->assemblyList as $assembly) {
Expand All @@ -25,6 +25,11 @@ public function get(string $key):?string {
if($f[0] !== "@") {
continue;
}

if(is_null($key)) {
return $requestPathParts[count($filePathParts) - 1] ?? null;
}

if("@$key" !== $f) {
continue;
}
Expand Down
61 changes: 61 additions & 0 deletions test/phpunit/Path/DynamicPathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace Gt\Routing\Test\Path;

use Gt\Routing\Assembly;
use Gt\Routing\Path\DynamicPath;
use PHPUnit\Framework\TestCase;

class DynamicPathTest extends TestCase {
public function testGet_noAssembly():void {
$sut = new DynamicPath("/");
self::assertNull($sut->get("something"));
}

public function testGet_withAssembly_noMatch():void {
$assembly = self::createMock(Assembly::class);
$assembly->method("current")
->willReturnOnConsecutiveCalls(
"page/@dynamic/something.html",
"page/@dynamic/something-else.html"
);
$assembly->method("valid")
->willReturn(
true,
true,
false
);
$sut = new DynamicPath("/", $assembly);
self::assertNull($sut->get("dynamic"));
}

public function testGet_byKey():void {
$assembly = self::createMock(Assembly::class);
$assembly->method("current")
->willReturnOnConsecutiveCalls(
"page/shop/_common.php",
"page/shop/@category/@itemName.php",
"page/shop/_common.php",
"page/shop/@category/@itemName.php",
);
$assembly->method("valid")
->willReturn(true);
$sut = new DynamicPath("/shop/OnePlus/6T", $assembly);
self::assertEquals("OnePlus", $sut->get("category"));
self::assertEquals("6T", $sut->get("itemName"));
}

public function testGet_noKey_shouldReturnDeepest():void {
$assembly = self::createMock(Assembly::class);
$assembly->method("current")
->willReturnOnConsecutiveCalls(
"page/shop/_common.php",
"page/shop/@category/@itemName.php",
"page/shop/_common.php",
"page/shop/@category/@itemName.php",
);
$assembly->method("valid")
->willReturn(true);
$sut = new DynamicPath("/shop/OnePlus/6T", $assembly);
self::assertEquals("6T", $sut->get());
}
}

0 comments on commit 7b5080b

Please sign in to comment.