You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.9 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-31
  8. *
  9. * Unit tests for Router class
  10. */
  11. require_once dirname(__FILE__) . '/../../../classes/Env.class.php';
  12. require_once dirname(__FILE__) . '/../../../app/router/Route.php';
  13. require_once dirname(__FILE__) . '/../../../app/router/Router.php';
  14. class RouterTest extends PHPUnit_Framework_TestCase
  15. {
  16. public function testGetRouteFirst()
  17. {
  18. $router = new Router();
  19. $this->assertNull($router->getRoute());
  20. $this->assertNull($router->getRouteName());
  21. }
  22. public function testRouterCycle()
  23. {
  24. $router = new Router();
  25. $router->add('user', 'user/account/:id', 'user');
  26. $route = $router->route('user/account/213');
  27. $this->assertSame(1, count($route->getParams()));
  28. $this->assertSame(array('id' => '213'), $route->getParams());
  29. $this->assertSame('user', $router->getRouteName());
  30. }
  31. public function testRouterMultipleRoutes()
  32. {
  33. $router = new Router();
  34. $router->add('user', 'user/account/:id', 'user');
  35. $router->add('sale', 'sale/order/:id', 'user');
  36. $route = $router->route('user/account/213');
  37. $this->assertSame('user', $router->getRouteName());
  38. $this->assertSame(1, count($route->getParams()));
  39. $this->assertSame(array('id' => '213'), $route->getParams());
  40. $route = $router->route('sale/order/22');
  41. $this->assertSame('sale', $router->getRouteName());
  42. $this->assertSame(array('id' => '22'), $route->getParams());
  43. }
  44. public function testRouteNotmatch()
  45. {
  46. $router = new Router();
  47. $router->add('user', 'user/account/:id', 'user');
  48. $this->assertFalse($router->route('user/info/213'));
  49. }
  50. public function testSetDefaultLayout()
  51. {
  52. $router = new Router();
  53. $router->setDefaultLayout('userLayout');
  54. $this->assertAttributeEquals('userLayout', 'default_layout', $router);
  55. }
  56. public function testGetRouteWithNameIsNull()
  57. {
  58. $name = null;
  59. $route = 'route object.';
  60. $router = new Router();
  61. $reflection = new ReflectionProperty('Router', 'route');
  62. $reflection->setAccessible(true);
  63. $reflection->setValue($router, $route);
  64. $this->assertEquals($route, $router->getRoute($name));
  65. }
  66. public function testGetRouteWithNamed()
  67. {
  68. $name = 'nameofroute';
  69. $uri = 'uri from route.';
  70. $route = 'route object.';
  71. $router = new Router();
  72. $reflection = new ReflectionProperty('Router', 'routes');
  73. $reflection->setAccessible(true);
  74. $reflection->setValue($router, array($name => $route));
  75. $this->assertEquals($route, $router->getRoute($name));
  76. }
  77. public function testGetRouteWithNamedWithError()
  78. {
  79. $name = 'name of route';
  80. $router = new Router();
  81. $this->setExpectedException('ErrorException');
  82. $router->getRoute($name);
  83. }
  84. public function testRouteIsExists()
  85. {
  86. $route = 'route object.';
  87. $name = 'nameofroute';
  88. $name_is_not_exists = 'nameofroutenotexists';
  89. $routes = array($name => $route);
  90. $router = new Router();
  91. $reflection = new ReflectionProperty('Router', 'routes');
  92. $reflection->setAccessible(true);
  93. $reflection->setValue($router, $routes);
  94. $this->assertTrue($router->routeIsExists($name));
  95. $this->assertFalse($router->routeIsExists($name_is_not_exists));
  96. }
  97. public function testGetDefaultErrorLayout()
  98. {
  99. $router = new Router();
  100. $this->assertSame('ErrorLayout', $router->getErrorLayout());
  101. }
  102. public function testSetErrorLayout()
  103. {
  104. $router = new Router();
  105. $router->setErrorLayout('CustomError');
  106. $this->assertSame('CustomErrorLayout', $router->getErrorLayout());
  107. }
  108. }