DispatcherTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\console;
  9. use lithium\console\Dispatcher;
  10. use lithium\console\Request;
  11. class DispatcherTest extends \lithium\test\Unit {
  12. protected $_backup = array();
  13. public function setUp() {
  14. $this->_backup['_SERVER'] = $_SERVER;
  15. $_SERVER['argv'] = array();
  16. }
  17. public function tearDown() {
  18. $_SERVER = $this->_backup['_SERVER'];
  19. }
  20. public function testEmptyConfigReturnRules() {
  21. $result = Dispatcher::config();
  22. $expected = array('rules' => array(
  23. 'command' => array(array('lithium\util\Inflector', 'camelize')),
  24. 'action' => array(array('lithium\util\Inflector', 'camelize', array(false)))
  25. ));
  26. $this->assertEqual($expected, $result);
  27. }
  28. public function testConfigWithClasses() {
  29. Dispatcher::config(array(
  30. 'classes' => array('request' => 'lithium\tests\mocks\console\MockDispatcherRequest')
  31. ));
  32. $expected = 'run';
  33. $result = Dispatcher::run()->testAction;
  34. $this->assertEqual($expected, $result);
  35. }
  36. public function testRunWithCommand() {
  37. $response = Dispatcher::run(new Request(array(
  38. 'args' => array('lithium\tests\mocks\console\MockDispatcherCommand')
  39. )));
  40. $expected = 'run';
  41. $result = $response->testAction;
  42. $this->assertEqual($expected, $result);
  43. }
  44. public function testRunWithPassed() {
  45. $response = Dispatcher::run(new Request(array(
  46. 'args' => array('lithium\tests\mocks\console\MockDispatcherCommand', 'with param')
  47. )));
  48. $expected = 'run';
  49. $result = $response->testAction;
  50. $this->assertEqual($expected, $result);
  51. $expected = 'with param';
  52. $result = $response->testParam;
  53. $this->assertEqual($expected, $result);
  54. }
  55. public function testRunWithAction() {
  56. $response = Dispatcher::run(new Request(array(
  57. 'args' => array('lithium\tests\mocks\console\MockDispatcherCommand', 'testAction')
  58. )));
  59. $expected = 'testAction';
  60. $result = $response->testAction;
  61. $this->assertEqual($expected, $result);
  62. }
  63. public function testInvalidCommand() {
  64. $expected = (object) array('status' => "Command `\\this\\command\\is\\fake` not found.\n");
  65. $result = Dispatcher::run(new Request(array(
  66. 'args' => array(
  67. '\this\command\is\fake',
  68. 'testAction'
  69. )
  70. )));
  71. $this->assertEqual($expected, $result);
  72. }
  73. public function testRunWithCamelizingCommand() {
  74. $expected = (object) array('status' => "Command `FooBar` not found.\n");
  75. $result = Dispatcher::run(new Request(array(
  76. 'args' => array(
  77. 'foo-bar'
  78. )
  79. )));
  80. $this->assertEqual($expected, $result);
  81. $expected = (object) array('status' => "Command `FooBar` not found.\n");
  82. $result = Dispatcher::run(new Request(array(
  83. 'args' => array('foo_bar')
  84. )));
  85. $this->assertEqual($expected, $result);
  86. }
  87. public function testRunWithCamelizingAction() {
  88. $result = Dispatcher::run(new Request(array(
  89. 'args' => array(
  90. 'lithium\tests\mocks\console\command\MockCommandHelp',
  91. 'sample-task-with-optional-args'
  92. )
  93. )));
  94. $this->assertTrue($result);
  95. $result = Dispatcher::run(new Request(array(
  96. 'args' => array(
  97. 'lithium\tests\mocks\console\command\MockCommandHelp',
  98. 'sample_task_with_optional_args'
  99. )
  100. )));
  101. $this->assertTrue($result);
  102. }
  103. public function testEnvironmentIsSet() {
  104. $expected = 'production';
  105. $response = Dispatcher::run(new Request(array(
  106. 'args' => array(
  107. 'lithium\tests\mocks\console\MockDispatcherCommand',
  108. 'testEnv', '--env=production'
  109. )
  110. )));
  111. $result = $response->environment;
  112. $this->assertEqual($expected, $result);
  113. }
  114. }
  115. ?>