Skip to content

Commit 3cc2a84

Browse files
author
Анатолий Нехай
committed
tests was improved
1 parent 55bc9d6 commit 3cc2a84

File tree

3 files changed

+140
-16
lines changed

3 files changed

+140
-16
lines changed

tests/RouteTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Import classes
77
*/
88
use PHPUnit\Framework\TestCase;
9+
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler;
910
use Sunrise\Http\Router\Route;
1011
use Sunrise\Http\Router\RouteInterface;
1112
use Sunrise\Http\ServerRequest\ServerRequestFactory;
@@ -365,4 +366,49 @@ public function testRunWithBrokenMiddleware() : void
365366
$this->assertNull($route->getMiddlewares()[2]->getRequest());
366367
$this->assertNull($route->getRequestHandler()->getRequest());
367368
}
369+
370+
/**
371+
* @return void
372+
*/
373+
public function testGetClassHolder() : void
374+
{
375+
$class = new Fixtures\Controllers\BlankController();
376+
377+
$route = new Route('foo', '/foo', [], $class);
378+
$holder = $route->getHolder();
379+
380+
$this->assertInstanceOf(\ReflectionClass::class, $holder);
381+
$this->assertSame(\get_class($class), $holder->getName());
382+
}
383+
384+
/**
385+
* @return void
386+
*/
387+
public function testGetClosureHolder() : void
388+
{
389+
$callback = function () {
390+
};
391+
392+
$route = new Route('foo', '/foo', [], new CallableRequestHandler($callback));
393+
$holder = $route->getHolder();
394+
395+
$this->assertInstanceOf(\ReflectionFunction::class, $holder);
396+
$this->assertSame($callback, $holder->getClosure());
397+
}
398+
399+
/**
400+
* @return void
401+
*/
402+
public function testGetMethodHolder() : void
403+
{
404+
$class = new Fixtures\Controllers\BlankController();
405+
$method = '__invoke';
406+
407+
$route = new Route('foo', '/foo', [], new CallableRequestHandler([$class, $method]));
408+
$holder = $route->getHolder();
409+
410+
$this->assertInstanceOf(\ReflectionMethod::class, $holder);
411+
$this->assertSame(\get_class($class), $holder->getDeclaringClass()->getName());
412+
$this->assertSame($method, $holder->getName());
413+
}
368414
}

tests/RouterBuilderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Sunrise\Http\Router\Exception\RouteNotFoundException;
1010
use Sunrise\Http\Router\Router;
1111
use Sunrise\Http\Router\RouterBuilder;
12+
use Symfony\Component\EventDispatcher\EventDispatcher;
1213

1314
/**
1415
* RouterBuilderTest
@@ -25,6 +26,7 @@ class RouterBuilderTest extends TestCase
2526
*/
2627
public function testBuild() : void
2728
{
29+
$eventDispatcher = new EventDispatcher();
2830
$container = $this->getContainer();
2931
$cache = $this->getCache();
3032

@@ -43,6 +45,7 @@ public function testBuild() : void
4345
$hosts['baz'] = ['baz.net'];
4446

4547
$builder = (new RouterBuilder)
48+
->setEventDispatcher($eventDispatcher)
4649
->setContainer($container)
4750
->setCache($cache)
4851
->setCacheKey('foo')
@@ -66,6 +69,7 @@ public function testBuild() : void
6669
$this->assertSame($patterns, Router::$patterns);
6770
$this->assertSame($hosts, $router->getHosts());
6871
$this->assertSame($middlewares, $router->getMiddlewares());
72+
$this->assertSame($eventDispatcher, $router->getEventDispatcher());
6973

7074
$router->getRoutes('foo');
7175
$router->getRoutes('bar');

tests/RouterTest.php

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -788,38 +788,35 @@ public function testMatchWithHosts() : void
788788
new Route('qux', '/ping', ['GET'], $requestHandler),
789789
];
790790

791-
$routes[0]->setHost('foo.host');
792-
$routes[1]->setHost('bar.host');
793-
$routes[2]->setHost('baz.host');
791+
$routes[0]->setHost('foo');
792+
$routes[1]->setHost('bar');
793+
$routes[2]->setHost('baz');
794794

795795
$router = new Router();
796-
$router->addHost('baz.host', 'example.com');
796+
$router->addHost('foo', 'foo.net');
797+
$router->addHost('bar', 'bar.net');
798+
$router->addHost('baz', 'baz.net');
797799
$router->addRoute(...$routes);
798800

801+
// hosted route
799802
$foundRoute = $router->match((new ServerRequestFactory)
800-
->createServerRequest('GET', 'http://foo.host/ping'));
803+
->createServerRequest('GET', 'http://foo.net/ping'));
801804
$this->assertSame($routes[0]->getName(), $foundRoute->getName());
802805

806+
// hosted route
803807
$foundRoute = $router->match((new ServerRequestFactory)
804-
->createServerRequest('GET', 'http://bar.host/ping'));
808+
->createServerRequest('GET', 'http://bar.net/ping'));
805809
$this->assertSame($routes[1]->getName(), $foundRoute->getName());
806810

811+
// hosted route
807812
$foundRoute = $router->match((new ServerRequestFactory)
808-
->createServerRequest('GET', 'http://baz.host/ping'));
809-
$this->assertSame($routes[2]->getName(), $foundRoute->getName());
810-
811-
$foundRoute = $router->match((new ServerRequestFactory)
812-
->createServerRequest('GET', 'http://example.com/ping'));
813+
->createServerRequest('GET', 'http://baz.net/ping'));
813814
$this->assertSame($routes[2]->getName(), $foundRoute->getName());
814815

816+
// non-hosted route
815817
$foundRoute = $router->match((new ServerRequestFactory)
816818
->createServerRequest('GET', 'http://localhost/ping'));
817819
$this->assertSame($routes[3]->getName(), $foundRoute->getName());
818-
819-
$routes[3]->setHost('qux.host');
820-
$this->expectException(RouteNotFoundException::class);
821-
$router->match((new ServerRequestFactory)
822-
->createServerRequest('GET', 'http://localhost/ping'));
823820
}
824821

825822
/**
@@ -891,4 +888,81 @@ public function testRouteEventOverrideRequest() : void
891888
$router->setEventDispatcher($eventDispatcher);
892889
$router->handle($request);
893890
}
891+
892+
/**
893+
* @return void
894+
*/
895+
public function testResolveHost() : void
896+
{
897+
$router = new Router();
898+
$router->addHost('foo', 'www1.foo.com', 'www2.foo.com');
899+
$router->addHost('bar', 'www1.bar.com', 'www2.bar.com');
900+
901+
$this->assertSame('foo', $router->resolveHostname('www1.foo.com'));
902+
$this->assertSame('foo', $router->resolveHostname('www2.foo.com'));
903+
$this->assertSame('bar', $router->resolveHostname('www1.bar.com'));
904+
$this->assertSame('bar', $router->resolveHostname('www2.bar.com'));
905+
$this->assertNull($router->resolveHostname('example.com'));
906+
}
907+
908+
/**
909+
* @return void
910+
*/
911+
public function testGetRoutesByHostname() : void
912+
{
913+
$router = new Router();
914+
$router->addHost('foo', 'www1.foo.com', 'www2.foo.com');
915+
$router->addHost('bar', 'www1.bar.com', 'www2.bar.com');
916+
917+
$routes = [
918+
new Fixtures\Route(),
919+
new Fixtures\Route(),
920+
new Fixtures\Route(),
921+
new Fixtures\Route(),
922+
new Fixtures\Route(),
923+
new Fixtures\Route(),
924+
];
925+
926+
$routes[0]->setHost('foo');
927+
$routes[2]->setHost('bar');
928+
$routes[4]->setHost('bar');
929+
930+
$router->addRoute(...$routes);
931+
932+
$this->assertSame([
933+
$routes[0],
934+
$routes[1],
935+
$routes[3],
936+
$routes[5],
937+
], $router->getRoutesByHostname('www1.foo.com'));
938+
939+
$this->assertSame([
940+
$routes[0],
941+
$routes[1],
942+
$routes[3],
943+
$routes[5],
944+
], $router->getRoutesByHostname('www2.foo.com'));
945+
946+
$this->assertSame([
947+
$routes[1],
948+
$routes[2],
949+
$routes[3],
950+
$routes[4],
951+
$routes[5],
952+
], $router->getRoutesByHostname('www1.bar.com'));
953+
954+
$this->assertSame([
955+
$routes[1],
956+
$routes[2],
957+
$routes[3],
958+
$routes[4],
959+
$routes[5],
960+
], $router->getRoutesByHostname('www2.bar.com'));
961+
962+
$this->assertSame([
963+
$routes[1],
964+
$routes[3],
965+
$routes[5],
966+
], $router->getRoutesByHostname('localhost'));
967+
}
894968
}

0 commit comments

Comments
 (0)