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.

78 lines
2.4 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-11
  8. *
  9. * Unit tests for Route 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->assertEquals('userAction', $route->getAction());
  18. $this->assertEquals('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->assertEquals(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->assertEquals(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->assertEquals(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->assertEquals(array('key' => 'value'), $route->getParams());
  58. $this->assertEquals(1, count($route->getParams()));
  59. }
  60. /**
  61. * @expectedException Exception
  62. */
  63. public function testMatchEmptyRequest()
  64. {
  65. $route = new Route('', 'user', array('name' => 'tony', 'role' => 'user'));
  66. $route->match('');
  67. }
  68. }