Compare commits

...

39 Commits

Author SHA1 Message Date
Simon 0634ba79dc Fixed getUrl() not working for home urls. 2023-12-11 22:45:14 +01:00
Simon 3534233a76 Fixed included triggering on other request-types than post 2023-12-09 18:39:33 +01:00
Simon 0f55480156 Fixed rewrite-route not being executed in rare instances 2023-12-09 05:36:30 +01:00
Simon 99ed44eb1e Fixed setHost nullable value 2023-11-29 01:20:00 +01:00
Simon 565a926bd3 Remove port from hostname 2023-11-27 08:06:35 +01:00
Simon 64483652ff Strip any potential port number from hostname 2023-11-27 07:58:49 +01:00
Simon d17ee96221 Added better handling of domains on urls. 2023-11-27 06:53:33 +01:00
Simon ed1ed43484 - Fixed domains not being prepending properly to urls.
- Won't prepend subdomain to urls if subdomain is equal to current host.
- Url: implemented parse function.
- Router: getUrl now parses url to properly parse the subdomain.
2023-11-27 05:52:10 +01:00
Simon a275366a90 Fixed inputhandler overwriting post-values 2023-11-21 22:25:06 +01:00
Simon Sessingø 4d1caddce4 Merge pull request #683 from ms-afk/bugfix-rewrite-route-executed-twice
Fixed rare double execution of rewrite routes in exception handler
2023-11-21 16:11:56 +01:00
Simon Sessingø 0970bd00c6 Merge pull request #682 from ms-afk/fix-readme-php-di
Fixed the php-di integration example in the README
2023-11-21 16:11:06 +01:00
Simon Sessingø 49b132da93 Merge pull request #672 from ATC-4K/patch-1
Added @return never to Response.php
2023-11-21 16:10:16 +01:00
Simon 08d78c8f71 Added support for input stream when not json encoded 2023-11-21 16:08:22 +01:00
ms-afk 5986dc9a08 fixed rare double execution of rewrite routes in exception handler
If a rewrite route is present, Router's method handleException will, currently, be adding that route to the processedRoutes array without removing the hasPendingRewrite flag. This leads to the associated callback being executed twice if the callback itself returns NULL. This happens because the handleRouteRewrite method, finding that hasPendingRewrite is still set to true, adds the rewriteRoute to the processedRoutes for a second time, before finally setting that flag to false.
2023-10-03 12:26:41 +02:00
Marco Scagnol cdf165d0f4 Fixed the php-di integration example
The previous version of the example in the README used exceptions that were not imported (NotFoundHttpException) to wrap existing exceptions. I've adapted the example to be similar to the current version of Pecee\SimpleRouter\ClassLoader\ClassLoader. That includes returning strings from loadClassMethod and loadClosure and not wrapping every exception, thrown by the called functions, into NotFoundHttpException exceptions, as well as using the exception ClassNotFoundHttpException when a class cannot be found, instead of the generic NotFoundHttpException. I also changed the way the ClassLoader checks if the container can resolve the class by using the container's method "has" instead of the php function class_exists.
2023-09-30 16:22:52 +02:00
ATC-4K adfe70f191 Added @return never to Response.php
In PHP8.1 instead of :void :never would be returned.
As this project is PHP7.4 compatible, we add it as a PHPDOC, so IDEs using this project will no longer complain and automatic checks will successfully detect dead code after calling a redirect()
2023-07-10 21:16:32 +02:00
Simon Sessingø cd891d5334 Merge pull request #671 from skipperbent/v5-release
V5 release
2023-05-06 21:14:29 +02:00
Simon Sessingø 7feb464af1 Merge pull request #670 from skipperbent/v5-development
Version 5.3.0.5
2023-05-06 21:11:12 +02:00
sessingo d3b1577095 RouteResource: simplified the findUrl method (issue: #666) 2023-05-06 21:07:58 +02:00
Simon Sessingø 12b6e3c1ab Merge pull request #669 from skipperbent/v5-release
V5 release
2023-05-06 17:39:41 +02:00
Simon Sessingø f085134ae3 Merge pull request #668 from skipperbent/v5-development
Version 5.3.0.4
2023-05-06 17:37:00 +02:00
Simon Sessingø 00d1c534de Merge pull request #667 from skipperbent/fix-resource-parameters
Resource-type not respecting parameters when using getUrl (issue: #666)
2023-05-06 17:33:53 +02:00
Simon Sessingø 37f826f24c Merge pull request #664 from SunflowerFuchs/v5-development
Fixed Route::setUrl() behavior
2023-05-06 17:32:57 +02:00
sessingo f3c6015a59 Fixed Resource-type not respecting parameters when using getUrl (issue: #666) 2023-05-06 17:29:33 +02:00
Pascal Pirschel b2851e41f1 Fixed Route::setUrl() behavior
When there are no parameters in the url, correctly empty the routes parameter array
2023-05-05 13:48:26 +02:00
Simon Sessingø 8ffa1088ab Merge pull request #662 from skipperbent/v5-release
V5 release
2023-04-24 20:08:29 +02:00
Simon Sessingø 9b8843aa08 Merge pull request #661 from skipperbent/v5-development
Reverted exception handling to old behavior (issue: #660)
2023-04-24 20:08:17 +02:00
sessingo e105f266e3 Fixed typo 2023-04-24 20:05:33 +02:00
sessingo a49d7c13b6 Reverted exception handling to old behavior (issue: #660) 2023-04-24 20:03:15 +02:00
Simon Sessingø f565014dff Merge pull request #659 from skipperbent/v5-release
V5 release
2023-04-21 11:38:08 +02:00
Simon Sessingø 97b61fb8bf Merge pull request #656 from skipperbent/v5-release
V5 release
2023-04-08 20:01:07 +02:00
Simon Sessingø 0ff9258776 Merge pull request #653 from skipperbent/v5-release
V5 release
2023-04-07 15:41:17 +02:00
Simon Sessingø 5dc3e99d6e Merge pull request #648 from skipperbent/v5-release
V5 release
2023-04-02 03:24:30 +02:00
Simon Sessingø 578fa10fc9 Merge pull request #646 from skipperbent/v5-release
V5 release
2023-04-02 03:20:13 +02:00
Simon Sessingø 72ebada821 Merge pull request #643 from skipperbent/v5-release
V5 release
2023-03-30 16:27:25 +02:00
Simon Sessingø e5b5b0898f Merge pull request #640 from skipperbent/v5-release
V5 release
2023-03-25 03:21:27 +01:00
Simon Sessingø 515fbc173c Merge pull request #633 from skipperbent/v5-release
V5 release
2023-02-13 14:06:39 +01:00
Simon Sessingø 89b766ff2f Merge pull request #631 from skipperbent/v5-release
V5 release
2023-02-11 17:35:00 +01:00
Simon Sessingø 0cb7fc416d Merge pull request #629 from skipperbent/v5-release
V5 release
2023-02-09 03:35:29 +01:00
16 changed files with 188 additions and 114 deletions
+9 -21
View File
@@ -1742,6 +1742,7 @@ SimpleRouter::setCustomClassLoader(new MyCustomClassLoader());
php-di support was discontinued by version 4.3, however you can easily add it again by creating your own class-loader like the example below:
```php
use Pecee\SimpleRouter\ClassLoader\IClassLoader;
use Pecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;
class MyCustomClassLoader implements IClassLoader
@@ -1762,19 +1763,14 @@ class MyCustomClassLoader implements IClassLoader
*
* @param string $class
* @return object
* @throws NotFoundHttpException
* @throws ClassNotFoundHttpException
*/
public function loadClass(string $class)
{
if (class_exists($class) === false) {
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
if ($this->container->has($class) === false) {
throw new ClassNotFoundHttpException($class, null, sprintf('Class "%s" does not exist', $class), 404, null);
}
try {
return $this->container->get($class);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
return $this->container->get($class);
}
/**
@@ -1782,15 +1778,11 @@ class MyCustomClassLoader implements IClassLoader
* @param object $class
* @param string $method
* @param array $parameters
* @return object
* @return string
*/
public function loadClassMethod($class, string $method, array $parameters)
{
try {
return $this->container->call([$class, $method], $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
return (string)$this->container->call([$class, $method], $parameters);
}
/**
@@ -1798,15 +1790,11 @@ class MyCustomClassLoader implements IClassLoader
*
* @param Callable $closure
* @param array $parameters
* @return mixed
* @return string
*/
public function loadClosure(callable $closure, array $parameters)
{
try {
return $this->container->call($closure, $parameters);
} catch (\Exception $e) {
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
}
return (string)$this->container->call($closure, $parameters);
}
}
```
+14 -10
View File
@@ -82,6 +82,10 @@ class InputHandler
if ($post !== false) {
$this->originalPost += $post;
}
} else {
$post = [];
parse_str($contents, $post);
$this->originalPost += $post;
}
}
@@ -108,7 +112,7 @@ class InputHandler
foreach ($files as $key => $value) {
// Parse multi dept file array
if(isset($value['name']) === false && is_array($value) === true) {
if (isset($value['name']) === false && is_array($value) === true) {
$list[$key] = $this->parseFiles($value, $key);
continue;
}
@@ -161,12 +165,12 @@ class InputHandler
try {
$file = InputFile::createFromArray([
'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key],
'type' => $original['type'][$key],
'size' => $original['size'][$key],
'type' => $original['type'][$key],
'size' => $original['size'][$key],
]);
if (isset($output[$key]) === true) {
@@ -231,7 +235,7 @@ class InputHandler
{
$element = null;
if(count($methods) > 0) {
if (count($methods) > 0) {
$methods = is_array(...$methods) ? array_values(...$methods) : $methods;
}
@@ -303,9 +307,9 @@ class InputHandler
public function exists($index, ...$methods): bool
{
// Check array
if(is_array($index) === true) {
foreach($index as $key) {
if($this->value($key, null, ...$methods) === null) {
if (is_array($index) === true) {
foreach ($index as $key) {
if ($this->value($key, null, ...$methods) === null) {
return false;
}
}
@@ -98,7 +98,7 @@ class BaseCsrfVerifier implements IMiddleware
*/
public function handle(Request $request): void
{
if ($this->skip($request) === false && ($request->isPostBack() === true || $this->isIncluded($request) === true)) {
if ($this->skip($request) === false && ($request->isPostBack() === true || $request->isPostBack() === true && $this->isIncluded($request) === true)) {
$token = $request->getInputHandler()->value(
static::POST_KEY,
+13 -12
View File
@@ -130,9 +130,9 @@ class Request
// Check if special IIS header exist, otherwise use default.
$url = $this->getHeader('unencoded-url');
if($url !== null){
if ($url !== null) {
$this->setUrl(new Url($url));
}else{
} else {
$this->setUrl(new Url(urldecode((string)$this->getHeader('request-uri'))));
}
$this->setContentType((string)$this->getHeader('content-type'));
@@ -225,7 +225,7 @@ class Request
public function getIp(bool $safeMode = false): ?string
{
$headers = [];
if($safeMode === false) {
if ($safeMode === false) {
$headers = [
'http-cf-connecting-ip',
'http-client-ip',
@@ -303,9 +303,9 @@ class Request
*/
public function getFirstHeader(array $headers, $defaultValue = null)
{
foreach($headers as $header) {
foreach ($headers as $header) {
$header = $this->getHeader($header);
if($header !== null) {
if ($header !== null) {
return $header;
}
}
@@ -329,7 +329,7 @@ class Request
*/
protected function setContentType(string $contentType): self
{
if(strpos($contentType, ';') > 0) {
if (strpos($contentType, ';') > 0) {
$this->contentType = strtolower(substr($contentType, 0, strpos($contentType, ';')));
} else {
$this->contentType = strtolower($contentType);
@@ -371,7 +371,7 @@ class Request
/**
* Returns true when request-method is type that could contain data in the page body.
*
*
* @return bool
*/
public function isPostBack(): bool
@@ -395,11 +395,7 @@ class Request
{
$this->url = $url;
if ($this->url->getHost() === null) {
$this->url->setHost((string)$this->getHost());
}
if($this->isSecure() === true) {
if ($this->isSecure() === true) {
$this->url->setScheme('https');
}
}
@@ -409,6 +405,11 @@ class Request
*/
public function setHost(?string $host): void
{
// Strip any potential ports from hostname
if (strpos((string)$host, ':') !== false) {
$host = strstr($host, strrchr($host, ':'), true);
}
$this->host = $host;
}
+3 -1
View File
@@ -32,6 +32,8 @@ class Response
*
* @param string $url
* @param ?int $httpCode
*
* @return never
*/
public function redirect(string $url, ?int $httpCode = null): void
{
@@ -127,4 +129,4 @@ class Response
return $this;
}
}
}
+19 -5
View File
@@ -67,8 +67,12 @@ class Url implements JsonSerializable
public function __construct(?string $url)
{
$this->originalUrl = $url;
$this->parse($url, true);
}
if ($url !== null && $url !== '/') {
public function parse(?string $url, bool $setOriginalPath = false): self
{
if ($url !== null) {
$data = $this->parseUrl($url);
$this->scheme = $data['scheme'] ?? null;
@@ -79,7 +83,10 @@ class Url implements JsonSerializable
if (isset($data['path']) === true) {
$this->setPath($data['path']);
$this->originalPath = $data['path'];
if ($setOriginalPath === true) {
$this->originalPath = $data['path'];
}
}
$this->fragment = $data['fragment'] ?? null;
@@ -88,6 +95,8 @@ class Url implements JsonSerializable
$this->setQueryString($data['query']);
}
}
return $this;
}
/**
@@ -136,10 +145,15 @@ class Url implements JsonSerializable
/**
* Get url host
*
* @param bool $includeTrails Prepend // in front of hostname
* @return string|null
*/
public function getHost(): ?string
public function getHost(bool $includeTrails = false): ?string
{
if ((string)$this->host !== '' && $includeTrails === true) {
return '//' . $this->host;
}
return $this->host;
}
@@ -522,12 +536,12 @@ class Url implements JsonSerializable
*/
public function jsonSerialize(): string
{
return $this->getRelativeUrl();
return $this->getHost(true) . $this->getRelativeUrl();
}
public function __toString(): string
{
return $this->getRelativeUrl();
return $this->getHost(true) . $this->getRelativeUrl();
}
}
+14 -8
View File
@@ -6,6 +6,7 @@ use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Router;
use Pecee\SimpleRouter\SimpleRouter;
abstract class LoadableRoute extends Route implements ILoadableRoute
{
@@ -82,15 +83,18 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
{
$this->url = ($url === '/') ? '/' : '/' . trim($url, '/') . '/';
$parameters = [];
if (strpos($this->url, $this->paramModifiers[0]) !== false) {
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
if ((bool)preg_match_all('/' . $regex . '/u', $this->url, $matches) !== false) {
$this->parameters = array_fill_keys($matches[1], null);
$parameters = array_fill_keys($matches[1], null);
}
}
$this->parameters = $parameters;
return $this;
}
@@ -135,12 +139,6 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
{
$url = $this->getUrl();
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0) {
$url = '//' . $group->getDomains()[0] . $url;
}
/* Create the param string - {parameter} */
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
@@ -174,7 +172,15 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
}
}
return rtrim('/' . ltrim($url, '/'), '/') . '/';
$url = rtrim('/' . ltrim($url, '/'), '/') . '/';
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
$url = '//' . $group->getDomains()[0] . $url;
}
return $url;
}
/**
@@ -3,6 +3,7 @@
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
use Pecee\SimpleRouter\SimpleRouter;
class RouteController extends LoadableRoute implements IControllerRoute
{
@@ -77,13 +78,15 @@ class RouteController extends LoadableRoute implements IControllerRoute
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0) {
$url .= '//' . $group->getDomains()[0];
}
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower((string)$method) . implode('/', $parameters);
return '/' . trim($url, '/') . '/';
$url = '/' . trim($url, '/') . '/';
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
$url = '//' . $group->getDomains()[0] . $url;
}
return $url;
}
public function matchRoute(string $url, Request $request): bool
+1 -1
View File
@@ -220,7 +220,7 @@ class RouteGroup extends Route implements IGroupRoute
$this->setExceptionHandlers((array)$settings['exceptionHandler']);
}
if ($merge === false && isset($settings['domain']) === true) {
if (isset($settings['domain']) === true) {
$this->setDomains((array)$settings['domain']);
}
+36 -21
View File
@@ -3,26 +3,27 @@
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
use Pecee\SimpleRouter\SimpleRouter;
class RouteResource extends LoadableRoute implements IControllerRoute
{
protected array $urls = [
'index' => '',
'create' => 'create',
'store' => '',
'show' => '',
'edit' => 'edit',
'update' => '',
'index' => '',
'create' => 'create',
'store' => '',
'show' => '',
'edit' => 'edit',
'update' => '',
'destroy' => '',
];
protected array $methodNames = [
'index' => 'index',
'create' => 'create',
'store' => 'store',
'show' => 'show',
'edit' => 'edit',
'update' => 'update',
'index' => 'index',
'create' => 'create',
'store' => 'store',
'show' => 'show',
'edit' => 'edit',
'update' => 'update',
'destroy' => 'destroy',
];
@@ -69,11 +70,25 @@ class RouteResource extends LoadableRoute implements IControllerRoute
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{
$url = array_search($name, $this->names, true);
if ($url !== false) {
return rtrim($this->url . $this->urls[$url], '/') . '/';
$parametersUrl = '';
if ($parameters !== null && count($parameters) > 0) {
$parametersUrl = join('/', $parameters) . '/';
}
return $this->url;
if ($url !== false) {
return rtrim($this->url . $parametersUrl . $this->urls[$url], '/') . '/';
}
$url = $this->url . $parametersUrl;
$group = $this->getGroup();
if ($group !== null && count($group->getDomains()) !== 0 && SimpleRouter::request()->getHost() !== $group->getDomains()[0]) {
$url = '//' . $group->getDomains()[0] . $url;
}
return $url;
}
protected function call($method): bool
@@ -172,12 +187,12 @@ class RouteResource extends LoadableRoute implements IControllerRoute
$this->name = $name;
$this->names = [
'index' => $this->name . '.index',
'create' => $this->name . '.create',
'store' => $this->name . '.store',
'show' => $this->name . '.show',
'edit' => $this->name . '.edit',
'update' => $this->name . '.update',
'index' => $this->name . '.index',
'create' => $this->name . '.create',
'store' => $this->name . '.store',
'show' => $this->name . '.show',
'edit' => $this->name . '.edit',
'update' => $this->name . '.update',
'destroy' => $this->name . '.destroy',
];
+8 -27
View File
@@ -443,12 +443,8 @@ class Router
}
}
} catch (\Throwable $e) {
if ($e instanceof Exception) {
return $this->handleException($e);
}
return $this->handleException(new Exception($e->getMessage(), $e->getCode()));
} catch (Exception $e) {
return $this->handleException($e);
}
if ($methodNotAllowed === true) {
@@ -694,10 +690,7 @@ class Router
/* If nothing is defined and a route is loaded we use that */
if ($name === null && $loadedRoute !== null) {
return $this->request
->getUrlCopy()
->setPath($loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name))
->setParams($getParams);
return $this->request->getUrlCopy()->parse($loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name))->setParams($getParams);
}
if ($name !== null) {
@@ -705,10 +698,7 @@ class Router
$route = $this->findRoute($name);
if ($route !== null) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($route->getMethod(), $parameters, $name))
->setParams($getParams);
return $this->request->getUrlCopy()->parse($route->findUrl($route->getMethod(), $parameters, $name))->setParams($getParams);
}
}
@@ -723,18 +713,12 @@ class Router
/* Check if the route contains the name/alias */
if ($processedRoute->hasName($controller) === true) {
return $this->request
->getUrlCopy()
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
return $this->request->getUrlCopy()->parse($processedRoute->findUrl($method, $parameters, $name))->setParams($getParams);
}
/* Check if the route controller is equal to the name */
if ($processedRoute instanceof IControllerRoute && strtolower($processedRoute->getController()) === strtolower($controller)) {
return $this->request
->getUrlCopy()
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
return $this->request->getUrlCopy()->parse($processedRoute->findUrl($method, $parameters, $name))->setParams($getParams);
}
}
@@ -744,10 +728,7 @@ class Router
$url = trim(implode('/', array_merge((array)$name, (array)$parameters)), '/');
$url = (($url === '') ? '/' : '/' . $url . '/');
return $this->request
->getUrlCopy()
->setPath($url)
->setParams($getParams);
return $this->request->getUrlCopy()->parse($url)->setParams($getParams);
}
/**
@@ -977,4 +958,4 @@ class Router
return $this;
}
}
}
@@ -0,0 +1,11 @@
<?php
use Pecee\Http\Request;
class DummyLoadableRoute extends Pecee\SimpleRouter\Route\LoadableRoute {
public function matchRoute(string $url, Request $request): bool
{
return false;
}
}
@@ -0,0 +1,27 @@
<?php
require_once 'Dummy/Route/DummyLoadableRoute.php';
class LoadableRouteTest extends \PHPUnit\Framework\TestCase
{
public function testSetUrlUpdatesParameters()
{
$route = new DummyLoadableRoute();
$this->assertEmpty($route->getParameters());
$route->setUrl('/');
$this->assertEmpty($route->getParameters());
$expected = ['param' => null, 'optionalParam' => null];
$route->setUrl('/{param}/{optionalParam?}');
$this->assertEquals($expected, $route->getParameters());
$expected = ['otherParam' => null];
$route->setUrl('/{otherParam}');
$this->assertEquals($expected, $route->getParameters());
$expected = [];
$route->setUrl('/');
$this->assertEquals($expected, $route->getParameters());
}
}
@@ -63,7 +63,22 @@ class RouterResourceTest extends \PHPUnit\Framework\TestCase
$response = TestRouter::debugOutput('/resource/38', 'get');
$this->assertEquals('show 38', $response);
}
public function testResourceUrls()
{
TestRouter::resource('/resource', 'ResourceController')->name('resource');
TestRouter::debugNoReset('/resource');
$this->assertEquals('/resource/3/create/', TestRouter::router()->getUrl('resource.create', ['id' => 3]));
$this->assertEquals('/resource/5/edit/', TestRouter::router()->getUrl('resource.edit', ['id' => 5]));
$this->assertEquals('/resource/6/', TestRouter::router()->getUrl('resource.update', ['id' => 6]));
$this->assertEquals('/resource/9/', TestRouter::router()->getUrl('resource.destroy', ['id' => 9]));
$this->assertEquals('/resource/12/', TestRouter::router()->getUrl('resource.delete', ['id' => 12]));
$this->assertEquals('/resource/', TestRouter::router()->getUrl('resource'));
TestRouter::router()->reset();
}
}
+3 -1
View File
@@ -94,11 +94,13 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
public function testSimilarUrls()
{
TestRouter::reset();
// Match normal route on alias
TestRouter::get('/url11', 'DummyController@method1');
TestRouter::get('/url22', 'DummyController@method2');
TestRouter::get('/url33', 'DummyController@method2')->name('match');
TestRouter::debugNoReset('/url33', 'get');
$this->assertEquals(TestRouter::getUrl('match'), TestRouter::getUrl());
@@ -184,7 +186,7 @@ class RouterUrlTest extends \PHPUnit\Framework\TestCase
// Should match /?jackdaniels=true&cola=yeah
$this->assertEquals('/?jackdaniels=true&cola=yeah', TestRouter::getUrl('home', null, ['jackdaniels' => 'true', 'cola' => 'yeah']));
TestRouter::router()->reset();
TestRouter::reset();
}
+6 -1
View File
@@ -8,11 +8,16 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
static::request()->setHost('testhost.com');
}
public static function reset(): void
{
static::$router = null;
}
public static function debugNoReset(string $testUrl, string $testMethod = 'get'): void
{
$request = static::request();
$request->setUrl((new \Pecee\Http\Url($testUrl))->setHost('local.unitTest'));
$request->setUrl((new \Pecee\Http\Url($testUrl)));
$request->setMethod($testMethod);
static::start();