diff options
Diffstat (limited to 'tests/Unit/RouterTest.php')
| -rw-r--r-- | tests/Unit/RouterTest.php | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php index 5d08441..12e27a4 100644 --- a/tests/Unit/RouterTest.php +++ b/tests/Unit/RouterTest.php @@ -2,14 +2,32 @@ declare(strict_types=1); +use Lightscale\Router\BasicStrategy; +use Lightscale\Router\Contracts\Strategy; +use Lightscale\Router\Enums\HttpMethod; use Lightscale\Router\Enums\PathSegmentType; use Lightscale\Router\PathSegment; +use Lightscale\Router\Route; +use Lightscale\Router\RouteCall; use Lightscale\Router\Router; +use Nyholm\Psr7\Factory\Psr17Factory; it('initializes') ->expect(fn () => new Router()) ->toBeInstanceOf(Router::class); +it('initializes with basic strategy') + ->expect((new Router)->getStrategy()) + ->toBeInstanceOf(BasicStrategy::class); + +it('can set strategy', function() { + $router = new Router; + $s = new BasicStrategy; + expect($router->getStrategy())->not->toBe($s); + $router->setStrategy($s); + expect($router->getStrategy())->toBe($s); +}); + it('has root') ->expect(fn () => (new Router())->root()) ->toBeInstanceOf(PathSegment::class) @@ -88,3 +106,41 @@ it('return null when segment not found', function () { expect($router->findSegment('/testing/testing'))->toBeNull(); }); + +it('calls strategy notFound with dispatch', function () { + $router = new Router(); + $factory = new Psr17Factory; + $request = $factory->createServerRequest(HttpMethod::Get->value, '/testing/testing'); + $response = $factory->createResponse(); + + $strat = Mockery::mock(Strategy::class); + $strat->shouldReceive('notFound') + ->with($request) + ->andReturn($response); + + $router->setStrategy($strat); + $result = $router->dispatch($request); + expect($result)->toBe($response); +}); + +it('calls strategy runRoute with dispatch on match', function() { + $router = new Router(); + + $router->root()->child('testing')->addRoute(new Route( + HttpMethod::Get, + fn () => null, + )); + + $factory = new Psr17Factory; + $request = $factory->createServerRequest(HttpMethod::Get->value, '/testing'); + $response = $factory->createResponse(); + + $strat = Mockery::mock(Strategy::class); + $strat->shouldReceive('runRoute') + ->with(Mockery::type(RouteCall::class)) + ->andReturn($response); + + $router->setStrategy($strat); + $result = $router->dispatch($request); + expect($result)->toBe($response); +}); |
