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.

145 lines
4.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 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_mock = $this->getMockBuilder('Router')
  78. ->disableOriginalConstructor()
  79. ->setMethods(array('getRoute'))
  80. ->getMock();
  81. $router_mock->expects($this->once())
  82. ->method('getRoute')
  83. ->with()
  84. ->will($this->returnValue($route_mock));
  85. $this->assertEquals($uri, $router_mock->getUri($name));
  86. }
  87. public function testGetUriWithNamed()
  88. {
  89. $name = 'name of route';
  90. $uri = 'uri from route.';
  91. $route_mock = $this->getMockBuilder('Route')
  92. ->disableOriginalConstructor()
  93. ->setMethods(array('getUri'))
  94. ->getMock();
  95. $route_mock->expects($this->once())
  96. ->method('getUri')
  97. ->with()
  98. ->will($this->returnValue($uri));
  99. $router_mock = $this->getMockBuilder('Router')
  100. ->disableOriginalConstructor()
  101. ->setMethods(array('getRoute', 'routeIsExists', 'getRouteByName'))
  102. ->getMock();
  103. $router_mock->expects($this->never())
  104. ->method('getRoute');
  105. $router_mock->expects($this->at(0))
  106. ->method('routeIsExists')
  107. ->with($this->equalTo($name))
  108. ->will($this->returnValue(true));
  109. $router_mock->expects($this->at(1))
  110. ->method('getRouteByName')
  111. ->with($this->equalTo($name))
  112. ->will($this->returnValue($route_mock));
  113. $this->assertEquals($uri, $router_mock->getUri($name));
  114. }
  115. public function testGetUriWithNamedWithError()
  116. {
  117. $name = 'name of route';
  118. $router_mock = $this->getMockBuilder('Router')
  119. ->disableOriginalConstructor()
  120. ->setMethods(array('getRoute', 'routeIsExists', 'getRouteByName'))
  121. ->getMock();
  122. $router_mock->expects($this->never())
  123. ->method('getRoute');
  124. $router_mock->expects($this->once())
  125. ->method('routeIsExists')
  126. ->with($this->equalTo($name))
  127. ->will($this->returnValue(false));
  128. $router_mock->expects($this->never())
  129. ->method('getRouteByName');
  130. $this->setExpectedException('ErrorException');
  131. $router_mock->getUri($name);
  132. }
  133. }