/lib/limb/cli/tests/cases/lmbCliInputTest.class.php

https://github.com/limb-php-framework/limb-app-buildman · PHP · 256 lines · 201 code · 45 blank · 10 comment · 0 complexity · b55fbd2bf4275a1c028513ad8145756a MD5 · raw file

  1. <?php
  2. /**
  3. * Limb Web Application Framework
  4. *
  5. * @link http://limb-project.com
  6. *
  7. * @copyright Copyright &copy; 2004-2007 BIT
  8. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  9. * @version $Id: lmbCliInputTest.class.php 4988 2007-02-08 15:35:19Z pachanga $
  10. * @package cli
  11. */
  12. lmb_require('limb/cli/src/lmbCliInput.class.php');
  13. lmb_require('limb/cli/src/lmbCliOption.class.php');
  14. class lmbCliInputTest extends UnitTestCase
  15. {
  16. function testReadEmpty()
  17. {
  18. $cli = new lmbCliInput();
  19. $this->assertEqual($cli->getOptions(), array());
  20. $this->assertEqual($cli->getArguments(), array());
  21. $this->assertNull($cli->getOption('f'));
  22. $this->assertNull($cli->getOptionValue('f'));
  23. $this->assertFalse($cli->isOptionPresent('f'));
  24. $this->assertEqual($cli->getOptionValue('f', 'wow'), 'wow');
  25. $this->assertNull($cli->getArgument(0));
  26. $this->assertEqual($cli->getArgument(0, 'wow'), 'wow');
  27. }
  28. function testReadSimpleOptionsWithArguments()
  29. {
  30. $argv = array('foo.php', '-f', 'wow', '--bar=1', 'foo', 'bar');
  31. $cli = new lmbCliInput(new lmbCliOption('f', lmbCliOption :: VALUE_REQ),
  32. new lmbCliOption('bar', lmbCliOption :: VALUE_REQ));
  33. $this->assertTrue($cli->read($argv));
  34. $this->assertEqual($cli->getOptionValue('f'), 'wow');
  35. $this->assertEqual($cli->getArguments(), array('foo', 'bar'));
  36. }
  37. function testReadOptionsHoldingSpaces()
  38. {
  39. $argv = array('foo.php', '--foo', 'wow hey test', '-f', 'spaces spaces', '--bar', 1, 'foo', 'bar');
  40. $cli = new lmbCliInput(new lmbCliOption('foo', lmbCliOption :: VALUE_REQ),
  41. new lmbCliOption('bar', lmbCliOption :: VALUE_REQ),
  42. new lmbCliOption('f', lmbCliOption :: VALUE_REQ));
  43. $this->assertTrue($cli->read($argv));
  44. $this->assertEqual($cli->getOptionValue('foo'), 'wow hey test');
  45. $this->assertEqual($cli->getOptionValue('f'), 'spaces spaces');
  46. $this->assertEqual($cli->getOptionValue('bar'), 1);
  47. $this->assertEqual($cli->getArguments(), array('foo', 'bar'));
  48. }
  49. function testStopReadingOnceEnoughArgumentsStarted()
  50. {
  51. $argv = array('foo.php', '--foo=1', '-z', 'stop_here', '-b 2');
  52. $cli = new lmbCliInput(new lmbCliOption('foo', lmbCliOption :: VALUE_REQ),
  53. new lmbCliOption('z'));
  54. $this->assertTrue($cli->read($argv));
  55. $this->assertEqual($cli->getArgument(0), 'stop_here');
  56. $this->assertEqual($cli->getOptionValue('foo'), 1);
  57. $this->assertNull($cli->getOptionValue('z'));
  58. $this->assertTrue($cli->isOptionPresent('z'));
  59. $this->assertNull($cli->getOptionValue('b'));
  60. $this->assertFalse($cli->isOptionPresent('b'));
  61. }
  62. function testForbiddenOptionValuesBecomeArguments()
  63. {
  64. $cli = new lmbCliInput(new lmbCliOption('f'));
  65. $this->assertTrue($cli->read(array('foo.php', '-f', 'foo', 'bar')));
  66. $this->assertEqual($cli->getArguments(), array('foo', 'bar'));
  67. }
  68. function testReadOptionValueRequiredError()
  69. {
  70. $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ));
  71. $cli->throwException();
  72. try
  73. {
  74. $cli->read(array('foo.php', '--foo'));
  75. $this->assertTrue(false);
  76. }
  77. catch(lmbCliException $e){}
  78. $cli->throwException(false);
  79. $this->assertFalse($cli->read(array('foo.php', '-f')));
  80. }
  81. function testReadOptionValueForbiddenError()
  82. {
  83. $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_NO));
  84. $cli->throwException();
  85. try
  86. {
  87. $cli->read(array('foo.php', '--foo=1'));
  88. $this->assertTrue(false);
  89. }
  90. catch(lmbCliException $e){}
  91. $cli->throwException(false);
  92. $this->assertFalse($cli->read(array('foo.php', '--foo', 'foo', 'bar')));
  93. }
  94. function testMinimumArgumentsError()
  95. {
  96. $cli = new lmbCliInput();
  97. $cli->setMinimumArguments(2);
  98. $this->assertFalse($cli->read(array('foo.php', 'wow')));
  99. }
  100. function testOfGetOptionValueDualism()
  101. {
  102. $argv = array('foo.php', '-f', 1, '--bar=4');
  103. $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
  104. new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ));
  105. $this->assertTrue($cli->read($argv));
  106. $this->assertEqual($cli->getOptionValue('f'), 1);
  107. $this->assertEqual($cli->getOptionValue('foo'), 1);
  108. $this->assertEqual($cli->getOptionValue('b'), 4);
  109. $this->assertEqual($cli->getOptionValue('bar'), 4);
  110. }
  111. function testReadWithEqualSignPresent()
  112. {
  113. $argv = array('foo.php', '--foo=1', '-b', 2);
  114. $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
  115. new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ));
  116. $this->assertTrue($cli->read($argv));
  117. $this->assertEqual($cli->getOptionValue('f'), 1);
  118. $this->assertEqual($cli->getOptionValue('b'), 2);
  119. }
  120. function testReadOptionsWithEqualSignMissing()
  121. {
  122. $argv = array('foo.php', '--foo', 1, '-b', 2);
  123. $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
  124. new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ));
  125. $this->assertTrue($cli->read($argv));
  126. $this->assertEqual($cli->getOptionValue('f'), 1);
  127. $this->assertEqual($cli->getOptionValue('b'), 2);
  128. }
  129. function testReadMixedOptions()
  130. {
  131. $argv = array('foo.php', '--foo=1', '-b', 2, '--zoo', 3);
  132. $cli = new lmbCliInput(new lmbCliOption('f', 'foo', lmbCliOption :: VALUE_REQ),
  133. new lmbCliOption('b', 'bar', lmbCliOption :: VALUE_REQ),
  134. new lmbCliOption('z', 'zoo', lmbCliOption :: VALUE_REQ));
  135. $this->assertTrue($cli->read($argv));
  136. $this->assertEqual($cli->getOptionValue('f'), 1);
  137. $this->assertEqual($cli->getOptionValue('b'), 2);
  138. $this->assertEqual($cli->getOptionValue('z'), 3);
  139. }
  140. function testShortOptionsGluing()
  141. {
  142. $argv = array('foo.php', '-ibk');
  143. $cli = new lmbCliInput(new lmbCliOption('i'),
  144. new lmbCliOption('b'),
  145. new lmbCliOption('k'));
  146. $this->assertTrue($cli->read($argv));
  147. $this->assertTrue($cli->isOptionPresent('i'));
  148. $this->assertTrue($cli->isOptionPresent('b'));
  149. $this->assertTrue($cli->isOptionPresent('k'));
  150. $this->assertFalse($cli->isOptionPresent('z'));
  151. }
  152. function testCancelOptionsGluingForNotSpecifiedOptions()
  153. {
  154. $cli = new lmbCliInput(new lmbCliOption('i', lmbCliOption :: VALUE_REQ));
  155. $this->assertTrue($cli->read(array('foo.php', '-ias')));
  156. $this->assertEqual($cli->getOptionValue('i'), 'as');
  157. $this->assertTrue($cli->read(array('foo.php', '-i100')));
  158. $this->assertEqual($cli->getOptionValue('i'), 100);
  159. }
  160. function testOptionsGluingWithLastValue()
  161. {
  162. $argv = array('foo.php', '-ibk', 2);
  163. $cli = new lmbCliInput(new lmbCliOption('i'),
  164. new lmbCliOption('b'),
  165. new lmbCliOption('k', lmbCliOption :: VALUE_REQ));
  166. $this->assertTrue($cli->read($argv));
  167. $this->assertNull($cli->getOptionValue('i'));
  168. $this->assertNull($cli->getOptionValue('b'));
  169. $this->assertEqual($cli->getOptionValue('k'), 2);
  170. }
  171. function testGetAllOptions()
  172. {
  173. $argv = array('foo.php',
  174. '--foo=1',
  175. '-b', 2,
  176. '--wow', 1,
  177. '--hey', 'value with space',
  178. '--zoo',
  179. '-i50',
  180. '-j good',
  181. '-e bad',
  182. '-ask',
  183. '-glu');
  184. $cli = new lmbCliInput(new lmbCliOption('foo', lmbCliOption :: VALUE_REQ),
  185. new lmbCliOption('b', lmbCliOption :: VALUE_REQ),
  186. new lmbCliOption('zoo'),
  187. new lmbCliOption('wow', lmbCliOption :: VALUE_REQ),
  188. new lmbCliOption('hey', lmbCliOption :: VALUE_REQ),
  189. new lmbCliOption('i', lmbCliOption :: VALUE_REQ),
  190. new lmbCliOption('j', lmbCliOption :: VALUE_REQ),
  191. new lmbCliOption('e', lmbCliOption :: VALUE_REQ),
  192. new lmbCliOption('a', lmbCliOption :: VALUE_REQ),
  193. new lmbCliOption('g'),
  194. new lmbCliOption('l'),
  195. new lmbCliOption('u'));
  196. $this->assertTrue($cli->read($argv));
  197. $this->assertEqual($cli->getOptionValue('foo'), 1);
  198. $this->assertEqual($cli->getOptionValue('b'), 2);
  199. $this->assertEqual($cli->getOptionValue('wow'), 1);
  200. $this->assertEqual($cli->getOptionValue('hey'), 'value with space');
  201. $this->assertEqual($cli->getOptionValue('zoo'), null);
  202. $this->assertEqual($cli->getOptionValue('i'), 50);
  203. $this->assertEqual($cli->getOptionValue('j'), 'good');
  204. $this->assertEqual($cli->getOptionValue('e'), 'bad');
  205. $this->assertEqual($cli->getOptionValue('a'), 'sk');
  206. $this->assertEqual($cli->getOptionValue('g'), null);
  207. $this->assertEqual($cli->getOptionValue('l'), null);
  208. $this->assertEqual($cli->getOptionValue('u'), null);
  209. }
  210. }
  211. ?>