test_router.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. require_once 'router.php';
  3. use WebBasics\Router;
  4. function test_handler_no_args() {
  5. return true;
  6. }
  7. function test_handler_arg($arg) {
  8. return $arg;
  9. }
  10. function test_handler_args($arg0, $arg1) {
  11. return $arg1 . $arg0;
  12. }
  13. class RouterTest extends PHPUnit_Framework_TestCase {
  14. function setUp() {
  15. $this->router = new Router(array(
  16. 'foo' => 'test_handler_no_args',
  17. '(ba[rz])' => 'test_handler_arg',
  18. '(ba[rz])(ba[rz])' => 'test_handler_args',
  19. ));
  20. }
  21. function test_call_handler_success() {
  22. $this->assertEquals(true, $this->router->call_handler('foo'));
  23. $this->assertEquals('bar', $this->router->call_handler('bar'));
  24. $this->assertEquals('baz', $this->router->call_handler('baz'));
  25. $this->assertEquals('barbaz', $this->router->call_handler('bazbar'));
  26. }
  27. function test_call_handler_failure() {
  28. $this->assertFalse($this->router->call_handler('barfoo'));
  29. }
  30. function test_call_handler_skip() {
  31. $foo = 'foo';
  32. $bar = function() use (&$foo) { $foo = 'bar'; return false; };
  33. $baz = function() { return; };
  34. $router = new Router(array('.*' => $bar, 'baz' => $baz));
  35. $router->call_handler('baz');
  36. $this->assertEquals('bar', $foo);
  37. }
  38. function test_add_route() {
  39. $this->router->add_route('(foobar)', 'test_handler_arg');
  40. $this->assertEquals('foobar', $this->router->call_handler('foobar'));
  41. }
  42. }
  43. ?>