RouterTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Router;
  10. use lithium\console\Request;
  11. class RouterTest extends \lithium\test\Unit {
  12. protected $_backup;
  13. public function setUp() {
  14. $this->_backup = $_SERVER;
  15. $_SERVER['argv'] = array();
  16. }
  17. public function tearDown() {
  18. $_SERVER = $this->_backup;
  19. }
  20. public function testParseNoArgumentsNoOptions() {
  21. $expected = array(
  22. 'command' => null, 'action' => 'run', 'args' => array()
  23. );
  24. $result = Router::parse();
  25. $this->assertEqual($expected, $result);
  26. }
  27. public function testParseArguments() {
  28. $expected = array(
  29. 'command' => 'test', 'action' => 'action',
  30. 'args' => array('param')
  31. );
  32. $result = Router::parse(new Request(array(
  33. 'args' => array('test', 'action', 'param')
  34. )));
  35. $this->assertEqual($expected, $result);
  36. }
  37. public function testParseGnuStyleLongOptions() {
  38. $expected = array(
  39. 'command' => 'test', 'action' => 'run', 'args' => array(),
  40. 'case' => 'lithium.tests.cases.console.RouterTest'
  41. );
  42. $result = Router::parse(new Request(array(
  43. 'args' => array(
  44. 'test', 'run',
  45. '--case=lithium.tests.cases.console.RouterTest'
  46. )
  47. )));
  48. $this->assertEqual($expected, $result);
  49. $expected = array(
  50. 'command' => 'test', 'action' => 'run', 'args' => array(),
  51. 'case' => 'lithium.tests.cases.console.RouterTest',
  52. 'phase' => 'drowning'
  53. );
  54. $result = Router::parse(new Request(array(
  55. 'args' => array(
  56. 'test',
  57. '--case=lithium.tests.cases.console.RouterTest',
  58. '--phase=drowning'
  59. )
  60. )));
  61. $this->assertEqual($expected, $result);
  62. }
  63. public function testParseGnuStyleLongOptionsContainingDash() {
  64. $expected = array(
  65. 'command' => 'test', 'action' => 'run', 'args' => array(),
  66. 'foo-bar' => 'something'
  67. );
  68. $result = Router::parse(new Request(array(
  69. 'args' => array(
  70. 'test', 'run',
  71. '--foo-bar=something'
  72. )
  73. )));
  74. $this->assertEqual($expected, $result);
  75. }
  76. public function testParseShortOption() {
  77. $expected = array(
  78. 'command' => 'test', 'action' => 'action', 'args' => array(),
  79. 'i' => true
  80. );
  81. $result = Router::parse(new Request(array(
  82. 'args' => array('test', 'action', '-i')
  83. )));
  84. $this->assertEqual($expected, $result);
  85. $expected = array(
  86. 'command' => 'test', 'action' => 'action', 'args' => array('something'),
  87. 'i' => true
  88. );
  89. $result = Router::parse(new Request(array(
  90. 'args' => array('test', 'action', '-i', 'something')
  91. )));
  92. $this->assertEqual($expected, $result);
  93. }
  94. public function testParseShortOptionAsFirst() {
  95. $expected = array(
  96. 'command' => 'test', 'action' => 'action', 'args' => array(),
  97. 'i' => true
  98. );
  99. $result = Router::parse(new Request(array(
  100. 'args' => array('-i', 'test', 'action')
  101. )));
  102. $this->assertEqual($expected, $result);
  103. $expected = array(
  104. 'command' => 'test', 'action' => 'action', 'args' => array('something'),
  105. 'i' => true
  106. );
  107. $result = Router::parse(new Request(array(
  108. 'args' => array('-i', 'test', 'action', 'something')
  109. )));
  110. $this->assertEqual($expected, $result);
  111. }
  112. public function testParseGnuStyleLongOptionAsFirst() {
  113. $expected = array(
  114. 'command' => 'test', 'action' => 'action', 'long' => 'something', 'i' => true,
  115. 'args' => array()
  116. );
  117. $result = Router::parse(new Request(array(
  118. 'args' => array('--long=something', 'test', 'action', '-i')
  119. )));
  120. $this->assertEqual($expected, $result);
  121. }
  122. }
  123. ?>