Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
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.

92 lines
2.3 KiB

5 years ago
  1. <?php
  2. /**
  3. * Flight: An extensible micro-framework.
  4. *
  5. * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
  6. * @license MIT, http://flightphp.com/license
  7. */
  8. require_once 'vendor/autoload.php';
  9. require_once __DIR__.'/classes/Hello.php';
  10. class DispatcherTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @var \flight\core\Dispatcher
  14. */
  15. private $dispatcher;
  16. function setUp(){
  17. $this->dispatcher = new \flight\core\Dispatcher();
  18. }
  19. // Map a closure
  20. function testClosureMapping(){
  21. $this->dispatcher->set('map1', function(){
  22. return 'hello';
  23. });
  24. $result = $this->dispatcher->run('map1');
  25. $this->assertEquals('hello', $result);
  26. }
  27. // Map a function
  28. function testFunctionMapping(){
  29. $this->dispatcher->set('map2', function(){
  30. return 'hello';
  31. });
  32. $result = $this->dispatcher->run('map2');
  33. $this->assertEquals('hello', $result);
  34. }
  35. // Map a class method
  36. function testClassMethodMapping(){
  37. $h = new Hello();
  38. $this->dispatcher->set('map3', array($h, 'sayHi'));
  39. $result = $this->dispatcher->run('map3');
  40. $this->assertEquals('hello', $result);
  41. }
  42. // Map a static class method
  43. function testStaticClassMethodMapping(){
  44. $this->dispatcher->set('map4', array('Hello', 'sayBye'));
  45. $result = $this->dispatcher->run('map4');
  46. $this->assertEquals('goodbye', $result);
  47. }
  48. // Run before and after filters
  49. function testBeforeAndAfter() {
  50. $this->dispatcher->set('hello', function($name){
  51. return "Hello, $name!";
  52. });
  53. $this->dispatcher->hook('hello', 'before', function(&$params, &$output){
  54. // Manipulate the parameter
  55. $params[0] = 'Fred';
  56. });
  57. $this->dispatcher->hook('hello', 'after', function(&$params, &$output){
  58. // Manipulate the output
  59. $output .= " Have a nice day!";
  60. });
  61. $result = $this->dispatcher->run('hello', array('Bob'));
  62. $this->assertEquals('Hello, Fred! Have a nice day!', $result);
  63. }
  64. // Test an invalid callback
  65. function testInvalidCallback() {
  66. $this->setExpectedException('Exception', 'Invalid callback specified.');
  67. $this->dispatcher->execute(array('NonExistentClass', 'nonExistentMethod'));
  68. }
  69. }