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.

127 lines
4.2 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 testGetRoute()
  57. {
  58. $route = 'route object.';
  59. $router = new Router();
  60. $reflection = new ReflectionProperty('Router', 'route');
  61. $reflection->setAccessible(true);
  62. $reflection->setValue($router, $route);
  63. $this->assertEquals($route, $router->getRoute());
  64. }
  65. public function testGetUriWithNameIsNull()
  66. {
  67. $name = null;
  68. $uri = 'uri from route.';
  69. $route_mock = $this->getMockBuilder('Route')
  70. ->disableOriginalConstructor()
  71. ->setMethods(array('getUri'))
  72. ->getMock();
  73. $route_mock->expects($this->once())
  74. ->method('getUri')
  75. ->with()
  76. ->will($this->returnValue($uri));
  77. $router = new Router();
  78. $reflection = new ReflectionProperty('Router', 'route');
  79. $reflection->setAccessible(true);
  80. $reflection->setValue($router, $route_mock);
  81. $this->assertEquals($uri, $router->getUri($name));
  82. }
  83. public function testGetUriWithNamed()
  84. {
  85. $name = 'nameofroute';
  86. $uri = 'uri from route.';
  87. $route_mock = $this->getMockBuilder('Route')
  88. ->disableOriginalConstructor()
  89. ->setMethods(array('getUri'))
  90. ->getMock();
  91. $route_mock->expects($this->once())
  92. ->method('getUri')
  93. ->with()
  94. ->will($this->returnValue($uri));
  95. $router = new Router();
  96. $reflection = new ReflectionProperty('Router', 'routes');
  97. $reflection->setAccessible(true);
  98. $reflection->setValue($router, array($name => $route_mock));
  99. $this->assertEquals($uri, $router->getUri($name));
  100. }
  101. public function testGetUriWithNamedWithError()
  102. {
  103. $name = 'name of route';
  104. $router_mock = $this->getMockBuilder('Router')
  105. ->disableOriginalConstructor()
  106. ->setMethods(array('getRoute', 'getRouteByName'))
  107. ->getMock();
  108. $router_mock->expects($this->never())
  109. ->method('getRoute');
  110. $router_mock->expects($this->never())
  111. ->method('getRouteByName');
  112. $this->setExpectedException('ErrorException');
  113. $router_mock->getUri($name);
  114. }
  115. }