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.

92 lines
3.0 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__) . '/../../../app/router/Route.php';
  12. class RouteTest extends PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructNoParams()
  15. {
  16. $route = new Route('index', 'user');
  17. $this->assertSame('userAction', $route->getAction());
  18. $this->assertSame('Layout', $route->getLayout());
  19. $this->assertEmpty($route->getParams());
  20. $this->assertAttributeEquals('index', 'route', $route);
  21. }
  22. public function testConstructWithParams()
  23. {
  24. $route = new Route('index', 'user', array('name' => 'tony', 'role' => 'user'));
  25. $this->assertArrayHasKey('role', $route->getParams());
  26. $this->assertSame(array('name' => 'tony', 'role' => 'user'), $route->getParams());
  27. }
  28. public function testMatchDiffCount()
  29. {
  30. $route = new Route('user/account/id/142', 'user', array('name' => 'tony', 'role' => 'user'));
  31. $this->assertFalse($route->match(array('user', 'account')));
  32. }
  33. /**
  34. * @TODO: Route::match() - need to trim left-right slashes
  35. */
  36. public function testMatchDiffParts()
  37. {
  38. $route = new Route('user/account', 'user');
  39. $this->assertFalse($route->match(array('user', 'login')));
  40. }
  41. public function testMatchVariable()
  42. {
  43. $route = new Route('user/account/:id', 'user');
  44. $this->assertTrue($route->match(array('user', 'account', '142')));
  45. $this->assertSame(array('id' => '142'), $route->getParams());
  46. }
  47. public function testMatchDiffPregParts()
  48. {
  49. $route = new Route('user/account/(?<key>value)', 'user');
  50. $this->assertFalse($route->match(array('user', 'account', 'wrong')));
  51. $this->assertSame(0, count($route->getParams()));
  52. }
  53. public function testMatchPregPasses()
  54. {
  55. $route = new Route('user/account/(?<key>[a-z]+)', 'user');
  56. $this->assertTrue($route->match(array('user', 'account', 'value')));
  57. $this->assertSame(array('key' => 'value'), $route->getParams());
  58. $this->assertSame(1, count($route->getParams()));
  59. }
  60. /**
  61. * @TODO Need check empty parameters from constructor Route
  62. */
  63. public function testMatchEmptyRequest()
  64. {
  65. $route = new Route('', 'user', array('name' => 'tony', 'role' => 'user'));
  66. $this->setExpectedException('PHPUnit_Framework_Error');
  67. $route->match('');
  68. }
  69. public function testGetUri()
  70. {
  71. $route = 'myroute';
  72. $route_mock = $this->getMockBuilder('Route')
  73. ->disableOriginalConstructor()
  74. ->setMethods(array('__construct'))
  75. ->getMock();
  76. $reflection = new ReflectionProperty('Route', 'route');
  77. $reflection->setAccessible(true);
  78. $reflection->setValue($route_mock, $route);
  79. $this->assertEquals('/' . $route, $route_mock->getUri());
  80. }
  81. }