test_router.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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($this->router->call_handler('foo'), true);
  23. $this->assertEquals($this->router->call_handler('bar'), 'bar');
  24. $this->assertEquals($this->router->call_handler('baz'), 'baz');
  25. $this->assertEquals($this->router->call_handler('bazbar'), 'barbaz');
  26. }
  27. function test_call_handler_failure() {
  28. $this->assertFalse($this->router->call_handler('barfoo'));
  29. }
  30. function test_add_route() {
  31. $this->router->add_route('(foobar)', 'test_handler_arg');
  32. $this->assertEquals($this->router->call_handler('foobar'), 'foobar');
  33. }
  34. }
  35. ?>