-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Packaged\Routing\Routes; | ||
|
||
use Packaged\Context\Context; | ||
use Packaged\Routing\Handler\Handler; | ||
use Packaged\Routing\Route; | ||
|
||
class ExactPathRoute extends Route | ||
{ | ||
protected array $_paths = []; | ||
|
||
/** | ||
* @param string $exactPath | ||
* @param Handler|string|callable $handler | ||
* | ||
* @return $this | ||
*/ | ||
public function addPath(string $exactPath, Handler|string|callable $handler): static | ||
{ | ||
$this->_paths[$exactPath] = $handler; | ||
return $this; | ||
} | ||
|
||
public function match(Context $context): bool | ||
{ | ||
$path = $context->request()->path(); | ||
if(isset($this->_paths[$path])) | ||
{ | ||
$this->setHandler($this->_paths[$path]); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace Packaged\Tests\Routing\Routes; | ||
|
||
use Packaged\Context\Context; | ||
use Packaged\Http\Request; | ||
use Packaged\Routing\Routes\ExactPathRoute; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ExactPathRouteTest extends TestCase | ||
{ | ||
public function testUnmatchedPath() | ||
{ | ||
$ctx = new Context(Request::create('http://www.google.com/a/b/c/?d=e&f=g')); | ||
|
||
$route = new ExactPathRoute(); | ||
$route->addPath('/test', 'test'); | ||
static::assertFalse($route->match($ctx)); | ||
} | ||
|
||
public function matchPathProvider() | ||
{ | ||
return [ | ||
['/test', 'testHandle'], | ||
['/test/', 'testHandle'], | ||
['/test?query=string', 'testHandle'], | ||
['/test/?query=string', 'testHandle'], | ||
['/test/?query=string&another=param', 'testHandle'], | ||
['/secondary', 'handleTwo'], | ||
['/random', 'randomHandle'], | ||
]; | ||
} | ||
|
||
/** | ||
* @param $url | ||
* @param $handler | ||
* | ||
* @dataProvider matchPathProvider | ||
* @return void | ||
*/ | ||
public function testMatchedPath($url, $handler) | ||
{ | ||
$ctx = new Context(Request::create($url)); | ||
|
||
$route = new ExactPathRoute(); | ||
$route->addPath('/test', 'testHandle'); | ||
$route->addPath('/test/', 'testHandle'); | ||
$route->addPath('/secondary', 'handleTwo'); | ||
$route->addPath('/random', 'randomHandle'); | ||
static::assertTrue($route->match($ctx)); | ||
static::assertEquals($handler, $route->getHandler()); | ||
} | ||
} |