Beispiel #1
0
                $route->link(['id' => ['123', '456', '78']]);
            };
            expect($closure)->toThrow(new RouterException("Expected `'id'` to match `'[0-9]{3}'`, but received `'78'`."));
        });
    });
    describe("->dispatch()", function () {
        it("passes route as argument of the handler function", function () {
            $r = new Router();
            $r->get('foo/{var1}[/{var2}]', ['host' => '{subdomain}.{domain}.bar'], function ($route, $response) {
                return array_merge([$response], array_values($route->params));
            });
            $response = new stdClass();
            $route = $r->route('foo/25', 'GET', 'foo.biz.bar');
            $actual = $route->dispatch($response);
            expect($actual)->toBe([$response, 'foo', 'biz', '25', null]);
            $route = $r->route('foo/25/bar', 'GET', 'foo.biz.bar');
            $actual = $route->dispatch($response);
            expect($actual)->toBe([$response, 'foo', 'biz', '25', 'bar']);
        });
        it("throws an exception on non valid routes", function () {
            $closure = function () {
                $r = new Router();
                $r->get('foo', function () {
                });
                $route = $r->route('bar');
                $route->dispatch();
            };
            expect($closure)->toThrow(new RouterException("No route found for `*:*:GET:/bar`.", 404));
        });
    });
});
Beispiel #2
0
namespace Lead\Router\Spec\Suite;

use Lead\Router\RouterException;
use Lead\Router\Router;
use Lead\Router\Route;
use Lead\Net\Http\Cgi\Request;
describe("Router", function () {
    beforeEach(function () {
        $this->router = new Router();
        $this->export = function ($request) {
            return array_intersect_key($request, array_fill_keys(['path', 'method', 'host', 'scheme'], true));
        };
    });
    describe("->__construct()", function () {
        it("formats the basePath", function () {
            $router = new Router(['basePath' => '/']);
            expect($router->basePath())->toBe('');
        });
    });
    describe("->basePath()", function () {
        it("sets an empty basePath", function () {
            expect($this->router->basePath('/'))->toBe($this->router);
            expect($this->router->basePath())->toBe('');
            expect($this->router->basePath(''))->toBe($this->router);
            expect($this->router->basePath())->toBe('');
        });
        it("adds an leading slash for non empty basePath", function () {
            expect($this->router->basePath('app'))->toBe($this->router);
            expect($this->router->basePath())->toBe('/app');
            expect($this->router->basePath('/base'))->toBe($this->router);
            expect($this->router->basePath())->toBe('/base');