[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/cli/tests/cases/ -> lmbCliInputTest.class.php (source)

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


Generated: Tue Oct 14 04:47:40 2008 Cross-referenced by PHPXref 0.7