summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/RouteMatch.php5
-rw-r--r--tests/Unit/RouteMatchTest.php26
-rw-r--r--tests/Unit/RouterTest.php65
3 files changed, 70 insertions, 26 deletions
diff --git a/tests/Unit/RouteMatch.php b/tests/Unit/RouteMatch.php
new file mode 100644
index 0000000..083ebbd
--- /dev/null
+++ b/tests/Unit/RouteMatch.php
@@ -0,0 +1,5 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\Router\Test\Unit;
diff --git a/tests/Unit/RouteMatchTest.php b/tests/Unit/RouteMatchTest.php
new file mode 100644
index 0000000..e3b8bce
--- /dev/null
+++ b/tests/Unit/RouteMatchTest.php
@@ -0,0 +1,26 @@
+<?php
+
+declare(strict_types=1);
+
+use Lightscale\Router\Enums\HttpMethod;
+use Lightscale\Router\PathSegment;
+use Lightscale\Router\PathSegmentMatch;
+use Lightscale\Router\Route;
+use Lightscale\Router\RouteMatch;
+
+it('initializes with data', function () {
+ $v = new RouteMatch(
+ $segMatch = new PathSegmentMatch(
+ new PathSegment('test'),
+ []
+ ),
+ $route = new Route(
+ HttpMethod::Get,
+ fn () => null,
+ ),
+ );
+
+ expect($v)
+ ->segmentMatch->toBe($segMatch)
+ ->route->toBe($route);
+});
diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php
index 2f8960a..dc78f56 100644
--- a/tests/Unit/RouterTest.php
+++ b/tests/Unit/RouterTest.php
@@ -9,6 +9,7 @@ use Lightscale\Router\Enums\PathSegmentType;
use Lightscale\Router\PathSegment;
use Lightscale\Router\Route;
use Lightscale\Router\RouteCall;
+use Lightscale\Router\RouteMatch;
use Lightscale\Router\Router;
use Nyholm\Psr7\Factory\Psr17Factory;
@@ -107,6 +108,44 @@ it('return null when segment not found', function () {
expect($router->findSegment('/testing/testing'))->toBeNull();
});
+it('returns null when finding route')
+ ->expect(fn () => (new Router())->findRoute(HttpMethod::Get, '/'))
+ ->toBeNull();
+
+it('finds a route', function () {
+ $router = new Router();
+ $seg = $router->root()->child('testing');
+ $route = new Route(HttpMethod::Get, fn () => null);
+ $seg->addRoute($route);
+
+ $result = $router->findRoute(HttpMethod::Get, '/testing');
+ expect($result)
+ ->toBeInstanceOf(RouteMatch::class)
+ ->segmentMatch->segment->toBe($seg)
+ ->route->toBe($route);
+});
+
+it('finds any method route', function () {
+ $router = new Router();
+ $router->root()->child('testing')->addRoute(new Route(
+ HttpMethod::Any,
+ fn () => null
+ ));
+
+ $result = $router->findRoute(HttpMethod::Get, '/testing');
+ expect($result)->toBeInstanceOf(RouteMatch::class);
+});
+
+it('finds route method case insenitively', function () {
+ $router = new Router();
+ $router->root()->child('testing')->addRoute(new Route(
+ HttpMethod::Get,
+ fn () => null
+ ));
+ $result = $router->findRoute('gET', '/testing');
+ expect($result)->toBeInstanceOf(RouteMatch::class);
+});
+
it('calls strategy notFound with dispatch', function () {
$router = new Router();
$factory = new Psr17Factory();
@@ -144,29 +183,3 @@ it('calls strategy runRoute with dispatch on match', function () {
$result = $router->dispatch($request);
expect($result)->toBe($response);
});
-
-it('matches an any method route', function() {
- $router = new Router();
- $factory = new Psr17Factory();
- $response = $factory->createResponse();
- $router->root()->child('testing')->addRoute(new Route(
- HttpMethod::Any,
- fn() => $response
- ));
-
- $result = $router->dispatch($factory->createServerRequest(HttpMethod::Get->value, '/testing'));
- expect($result)->toBe($response);
-});
-
-it('matches route method case insenitively', function() {
- $router = new Router();
- $factory = new Psr17Factory();
- $response = $factory->createResponse();
- $router->root()->child('testing')->addRoute(new Route(
- HttpMethod::Get,
- fn() => $response
- ));
-
- $result = $router->dispatch($factory->createServerRequest('GeT', '/testing'));
- expect($result)->toBe($response);
-});