RouterTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. class RoutingTest extends PHPUnit_Framework_TestCase {
  3. public static function setUpBeforeClass()
  4. {
  5. $routes = array();
  6. $routes['GET /'] = function() {return 'root';};
  7. $routes['GET /home'] = array('name' => 'home', 'do' => function() {});
  8. $routes['POST /home'] = array('name' => 'post-home', 'do' => function() {});
  9. $routes['GET /user/(:num)'] = array('name' => 'user', 'do' => function() {});
  10. $routes['GET /user/(:any)/(:num)/edit'] = array('name' => 'edit', 'do' => function() {});
  11. System\Router::$routes = $routes;
  12. }
  13. public function testRouterReturnsNullWhenNotFound()
  14. {
  15. $this->assertNull(System\Router::route('GET', 'not-found'));
  16. }
  17. public function testRouterRoutesToProperRouteWhenSegmentsArePresent()
  18. {
  19. $this->assertEquals(System\Router::route('GET', 'home')->callback['name'], 'home');
  20. $this->assertEquals(System\Router::route('POST', 'home')->callback['name'], 'post-home');
  21. $this->assertEquals(System\Router::route('GET', 'user/1')->callback['name'], 'user');
  22. $this->assertEquals(System\Router::route('GET', 'user/taylor/25/edit')->callback['name'], 'edit');
  23. }
  24. public function testRouterReturnsNullWhenRouteNotFound()
  25. {
  26. $this->assertNull(System\Router::route('POST', 'user/taylor/25/edit'));
  27. $this->assertNull(System\Router::route('GET', 'user/taylor/taylor/edit'));
  28. $this->assertNull(System\Router::route('GET', 'user/taylor'));
  29. $this->assertNull(System\Router::route('GET', 'user/12-3'));
  30. }
  31. public static function tearDownAfterClass()
  32. {
  33. System\Router::$routes = null;
  34. }
  35. }