Skip to content

Commit

Permalink
update some for http routes
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 9, 2020
1 parent 0366bd9 commit 449bf02
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 42 deletions.
57 changes: 40 additions & 17 deletions src/http-server/src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,45 @@ public function match(string $path): array
* helper methods
******************************************************************************/

/**
* Build an URL by route path vars
*
* @param array $pathVars Path vars. eg: ['name' => 'inhere']
*
* @return string
*/
public function createUrl(array $pathVars = []): string
{
$pairs = [];
foreach ($pathVars as $name => $val) {
$name = '{' . $name . '}';
// save
$pairs[$name] = $val;
}

if ($pairs) {
return strtr($this->path, $pairs);
}

return $this->path;
}

/**
* Build uri path string.
*
* @param array $pathVars Path vars. eg: ['{name}' => 'inhere']
*
* @return string
*/
public function toUri(array $pathVars = []): string
{
if ($pathVars) {
return strtr($this->path, $pathVars);
}

return $this->path;
}

/**
* @param array $params
*
Expand All @@ -359,7 +398,7 @@ public function copyWithParams(array $params): self
/**
* push middleware(s) to the route
*
* @param array ...$middleware
* @param array|mixed ...$middleware
*
* @return Route
*/
Expand Down Expand Up @@ -396,22 +435,6 @@ public function setChains(array $chains): void
$this->chains = $chains;
}

/**
* build uri string.
*
* @param array $pathVars
*
* @return string
*/
public function toUri(array $pathVars = []): string
{
if ($pathVars) {
return strtr($this->path, $pathVars);
}

return $this->path;
}

/**
* get basic info data
*
Expand Down
8 changes: 6 additions & 2 deletions src/http-server/src/Router/RouteRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Http\Server\Router;

use Swoft\Stdlib\Helper\Str;
use function rtrim;

/**
* Class RoutesRegister - collect all routes info from annotations
*
* @since 2.0
*/
class RouteRegister
final class RouteRegister
{
/**
* @var array
Expand Down Expand Up @@ -84,7 +86,9 @@ public static function registerRoutes(Router $router): void
$path = $routePath[0] === '/' ? $routePath : $prefix . '/' . $routePath;
$handler = $class . '@' . $route['action'];

$router->map($route['method'], $path, $handler, $route['params'], ['name' => $route['name']]);
$router->map($route['method'], $path, $handler, $route['params'], [
'name' => $route['name'],
]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ protected function cacheMatchedParamRoute(string $path, string $method, Route $r

/**
* @param string $name Route name
* @param array $pathVars
* @param array $pathVars Path vars. eg: ['{name}' => 'inhere']
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/ContentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @since 2.0
*/
class ContentTypeTest extends TestCase
class ContentTypeTest extends HttpServerTestCase
{
public function testUserCt()
{
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @since 2.0
*/
class HeaderTest extends TestCase
class HeaderTest extends HttpServerTestCase
{
/**
* @throws SwoftException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
*
* @since 2.0
*/
abstract class TestCase extends \PHPUnit\Framework\TestCase
abstract class HttpServerTestCase extends \PHPUnit\Framework\TestCase
{
/**
* @var MockHttpServer
*/
protected $mockServer;

public function setUp()
public function setUp(): void
{
$this->mockServer = BeanFactory::getBean(MockHttpServer::class);
}
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/MdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @since 2.0
*/
class MdTest extends TestCase
class MdTest extends HttpServerTestCase
{
public function testMethod()
{
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @since 2.0
*/
class RequestTest extends TestCase
class RequestTest extends HttpServerTestCase
{
public function testPost(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @package SwoftTest\Http\Server\Unit
*/
class ResponseTest extends TestCase
class ResponseTest extends HttpServerTestCase
{
/**
* @throws SwoftException
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/RestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @since 2.0
*/
class RestTest extends TestCase
class RestTest extends HttpServerTestCase
{
/**
* @throws \Swoft\Exception\SwoftException
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @since 2.0
*
*/
class RouteTest extends TestCase
class RouteTest extends HttpServerTestCase
{
/**
* @throws SwoftException
Expand Down
16 changes: 10 additions & 6 deletions src/http-server/test/unit/Router/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SwoftTest\Http\Server\Router;

use LogicException;
use PHPUnit\Framework\TestCase;
use Swoft\Http\Server\Router\Route;

Expand All @@ -22,25 +23,28 @@ class RouteTest extends TestCase
public function testCreateFromArray(): void
{
$route = Route::createFromArray([
'path' => '/kfhxlkeugug/{name}',
'path' => '/hi/{name}',
'method' => 'GET',
'handler' => 'handler_func',
'bindVars' => [],
'params' => [],
'pathVars' => ['name',],
'pathRegex' => '#^/kfhxlkeugug/([^/]+)$#',
'pathStart' => '/kfhxlkeugug/',
'pathRegex' => '#^/hi/([^/]+)$#',
'pathStart' => '/hi/',
'chains' => [],
'options' => [],
]);
$route->addOption('n1', 'v1');

$this->assertEquals('GET', $route->getMethod());
$this->assertEquals(['name'], $route->getPathVars());
$this->assertEquals('/kfhxlkeugug/', $route->getPathStart());
$this->assertEquals('#^/kfhxlkeugug/([^/]+)$#', $route->getPathRegex());
$this->assertEquals('/hi/', $route->getPathStart());
$this->assertEquals('#^/hi/([^/]+)$#', $route->getPathRegex());
$this->assertArrayHasKey('name', $route->toArray());
$this->assertArrayHasKey('n1', $route->getOptions());

$this->assertSame('/hi/inhere', $route->toUri(['{name}' => 'inhere']));
$this->assertSame('/hi/inhere', $route->createUrl(['name' => 'inhere']));
}

public function testParseParam(): void
Expand Down Expand Up @@ -117,7 +121,7 @@ public function testParseParam(): void
$this->assertEquals('#^/blog-(\w+)$#', $route->getPathRegex());

$route = Route::create('GET', '/some/[to/]path', 'my_handler');
$this->expectException(\LogicException::class);
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Optional segments can only occur at the end of a route');
$route->parseParam();
}
Expand Down
16 changes: 10 additions & 6 deletions src/http-server/test/unit/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use PHPUnit\Framework\TestCase;
use Swoft\Http\Server\Router\Route;
use Swoft\Http\Server\Router\Router;
use Throwable;
use function sprintf;

/**
* Class RouterTest
Expand Down Expand Up @@ -62,14 +64,14 @@ public function testAddRoutes(): void

$string = (string)$r;
foreach (Router::METHODS_ARRAY as $method) {
$s = \sprintf('%-7s %-25s --> %s', $method, "/$method", "handle_$method");
$s = sprintf('%-7s %-25s --> %s', $method, "/$method", "handle_$method");
$this->assertContains($s, $string);
}

$r->add('ANY', '/any', 'handler_any');
$string = $r->toString();
foreach (Router::METHODS_ARRAY as $method) {
$s = \sprintf('%-7s %-25s --> %s', $method, '/any', 'handler_any');
$s = sprintf('%-7s %-25s --> %s', $method, '/any', 'handler_any');
$this->assertContains($s, $string);
}

Expand All @@ -78,7 +80,7 @@ public function testAddRoutes(): void

try {
$r->add('invalid', '/path', '/handler');
} catch (\Throwable $e) {
} catch (Throwable $e) {
$this->assertContains('The method [INVALID] is not supported', $e->getMessage());
}
}
Expand All @@ -97,7 +99,7 @@ public function testAddRoute(): void
$r3 = $router->add('get', '/path3', 'handler3');
$r3->namedTo('r3', $router);

$r4 = $router->add('get', '/path4', 'handler4', [], ['name' => 'r4']);
$r4 = $router->add('get', '/path4/{var}', 'handler4', [], ['name' => 'r4']);
$r5 = Route::create('get', '/path5', 'handler5', [], ['name' => 'r5'])
->attachTo($router);

Expand All @@ -107,6 +109,8 @@ public function testAddRoute(): void
$this->assertEquals($r4, $router->getRoute('r4'));
$this->assertEquals($r5, $router->getRoute('r5'));

$this->assertSame('/path4/val', $router->createUri('r4', ['{var}' => 'val']));

$ret = $router->getRoute('r3');
$this->assertEquals($r3, $ret);
$this->assertEquals([
Expand Down Expand Up @@ -221,10 +225,10 @@ public function testParamRoute(): void
// invalid
[$status, ,] = $router->match('/hi/dont-match');
$this->assertSame(Router::NOT_FOUND, $status);

// '/user/upload/moment/orj/{name}'
$router->get('/user/upload/moment/orj/{name}', 'handler4');

[$status, $path, $route] = $router->match('/user/upload/moment/orj/da');

$this->assertSame(Router::FOUND, $status);
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/test/unit/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @since 2.0
*/
class ValidatorTest extends TestCase
class ValidatorTest extends HttpServerTestCase
{
/**
* @throws SwoftException
Expand Down

0 comments on commit 449bf02

Please sign in to comment.