RouteTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Route test class
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2012 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Test\Router
  9. */
  10. class GitPHP_RouteTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testPath()
  13. {
  14. $route = new GitPHP_Route('test/:param1/route/:param2');
  15. $this->assertEquals('test/:param1/route/:param2', $route->GetPath());
  16. }
  17. public function testConstraints()
  18. {
  19. $route = new GitPHP_Route('test/:param1/route/:param2', array('param1' => 'testvalue', 'param2' => 'testvalue2|testvalue3'));
  20. $params = array(
  21. 'param1' => 'testvalue'
  22. );
  23. $this->assertFalse($route->Valid($params));
  24. $params['param2'] = 'badvalue';
  25. $this->assertFalse($route->Valid($params));
  26. $params['param2'] = 'testvalue2';
  27. $this->assertTrue($route->Valid($params));
  28. $params['param2'] = 'testvalue3';
  29. $this->assertTrue($route->Valid($params));
  30. }
  31. public function testUsedParams()
  32. {
  33. $route = new GitPHP_Route('test/:param1/route/:param2', array(), array('otherparam' => 'othervalue', 'otherparam2' => 'othervalue2'));
  34. $params = $route->GetUsedParameters();
  35. $this->assertCount(4, $params);
  36. $this->assertContains('param1', $params);
  37. $this->assertContains('param2', $params);
  38. $this->assertContains('otherparam', $params);
  39. $this->assertContains('otherparam2', $params);
  40. }
  41. public function testMatch()
  42. {
  43. $route = new GitPHP_Route('test/:param1/route/:param2', array('param1' => 'validvalue'), array('extraparam' => 'extravalue'));
  44. $path = 'test/invalid';
  45. $this->assertFalse($route->Match($path));
  46. $path = 'test/invalidvalue/route/otherparam';
  47. $this->assertFalse($route->Match($path));
  48. $path = 'test/validvalue/route/otherparam';
  49. $params = $route->Match($path);
  50. $this->assertCount(3, $params);
  51. $this->assertEquals('validvalue', $params['param1']);
  52. $this->assertEquals('otherparam', $params['param2']);
  53. $this->assertEquals('extravalue', $params['extraparam']);
  54. }
  55. public function testBuild()
  56. {
  57. $route = new GitPHP_Route('test/:param1/route/:param2');
  58. $params = array(
  59. 'param1' => 'testvalue',
  60. 'param2' => 'testvalue2'
  61. );
  62. $this->assertEquals('test/testvalue/route/testvalue2', $route->Build($params));
  63. }
  64. public function testParent()
  65. {
  66. $parentroute = new GitPHP_Route('parent/:parent', array('parent' => 'parentvalue'), array('parentparam' => 'parentvalue'));
  67. $childroute = new GitPHP_Route('child/:child', array('child' => 'childvalue'), array('childparam' => 'childvalue'), $parentroute);
  68. $this->assertEquals('parent/:parent/child/:child', $childroute->GetPath());
  69. $params = array(
  70. 'child' => 'childvalue'
  71. );
  72. $this->assertFalse($childroute->Valid($params));
  73. $params['parent'] = 'parentvalue';
  74. $this->assertTrue($childroute->Valid($params));
  75. $usedparams = $childroute->GetUsedParameters();
  76. $this->assertCount(4, $usedparams);
  77. $this->assertContains('parent', $usedparams);
  78. $this->assertContains('child', $usedparams);
  79. $this->assertContains('parentparam', $usedparams);
  80. $this->assertContains('childparam', $usedparams);
  81. $routeparams = $childroute->Match('parent/parentvalue/child/childvalue');
  82. $this->assertCount(4, $routeparams);
  83. $this->assertEquals('parentvalue', $routeparams['parent']);
  84. $this->assertEquals('childvalue', $routeparams['child']);
  85. $this->assertEquals('parentvalue', $routeparams['parentparam']);
  86. $this->assertEquals('childvalue', $routeparams['childparam']);
  87. $this->assertEquals('parent/parentvalue/child/childvalue', $childroute->Build($params));
  88. }
  89. }