[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/cli/tests/cases/ -> lmbCliRunnerTest.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/fs/src/lmbFs.class.php');
  10  lmb_require('limb/cli/src/lmbCliResponse.class.php');
  11  lmb_require('limb/cli/src/lmbCliInput.class.php');
  12  lmb_require('limb/cli/src/lmbCliRunner.class.php');
  13  
  14  class lmbCliRunnerTest extends UnitTestCase
  15  {
  16    var $tmp_dir;
  17  
  18    function setUp()
  19    {
  20      $this->tmp_dir = LIMB_VAR_DIR . '/tmp_cmd/';
  21      lmbFs :: mkdir($this->tmp_dir);
  22    }
  23  
  24    function tearDown()
  25    {
  26      lmbFs :: rm($this->tmp_dir);
  27    }
  28  
  29    function testExecuteFailureNoCommand()
  30    {
  31      $input = new lmbCliInput();
  32      $output = new lmbCliResponse();
  33  
  34      $runner = new lmbCliRunner($input, $output);
  35      $runner->returnOnExit();
  36      $runner->throwOnError();
  37  
  38      try
  39      {
  40        $runner->execute();
  41        $this->assertTrue(false);
  42      }
  43      catch(lmbException $e){}
  44    }
  45  
  46    function testCantMapToCmdObject()
  47    {
  48      $input = new lmbCliInput();
  49      $output = new lmbCliResponse();
  50  
  51      $input->read(array('foo.php', 'foo'));
  52  
  53      $runner = new lmbCliRunner($input, $output);
  54      $runner->returnOnExit();
  55      $runner->throwOnError();
  56  
  57      try
  58      {
  59        $runner->execute();
  60        $this->assertTrue(false);
  61      }
  62      catch(lmbException $e){}
  63    }
  64  
  65    function testCommandToClass()
  66    {
  67      $this->assertEqual(lmbCliRunner :: commandToClass('foo'), 'FooCliCmd');
  68      $this->assertEqual(lmbCliRunner :: commandToClass('foo_bar'), 'FooBarCliCmd');
  69      $this->assertEqual(lmbCliRunner :: commandToClass('foo-bar'), 'FooBarCliCmd');
  70    }
  71  
  72    function testDefaultAction()
  73    {
  74      $input = new lmbCliInput();
  75      $output = new lmbCliResponse();
  76  
  77      $input->read(array('foo.php', $cmd = $this->_randomName()));
  78  
  79      $runner = new lmbCliRunner($input, $output);
  80      $runner->setCommandSearchPath($this->tmp_dir);
  81      $runner->returnOnExit();
  82      $runner->throwOnError();
  83  
  84      $this->_createCommandClass($cmd);
  85  
  86      $this->assertEqual($runner->execute(), 0);
  87    }
  88  
  89    function testFallbackToDefaultAction()
  90    {
  91      $input = new lmbCliInput();
  92      $output = new lmbCliResponse();
  93  
  94      $input->read(array('foo.php', $cmd = $this->_randomName(), 'no-such-method'));
  95  
  96      $runner = new lmbCliRunner($input, $output);
  97      $runner->setCommandSearchPath($this->tmp_dir);
  98      $runner->returnOnExit();
  99      $runner->throwOnError();
 100  
 101      $this->_createCommandClass($cmd);
 102  
 103      $this->assertEqual($runner->execute(), 0);
 104    }
 105  
 106    function testConcreteAction()
 107    {
 108      $input = new lmbCliInput();
 109      $output = new lmbCliResponse();
 110  
 111      $input->read(array('foo.php', $cmd = $this->_randomName(), 'foo'));
 112  
 113      $runner = new lmbCliRunner($input, $output);
 114      $runner->setCommandSearchPath($this->tmp_dir);
 115      $runner->returnOnExit();
 116      $runner->throwOnError();
 117  
 118      $this->_createCommandClass($cmd, 'function foo(){return 1;}');
 119  
 120      $this->assertEqual($runner->execute(), 1);
 121    }
 122  
 123    function testSanitizeActionName()
 124    {
 125      $input = new lmbCliInput();
 126      $output = new lmbCliResponse();
 127  
 128      $input->read(array('foo.php', $cmd = $this->_randomName(), 'foo-bar'));
 129  
 130      $runner = new lmbCliRunner($input, $output);
 131      $runner->setCommandSearchPath($this->tmp_dir);
 132      $runner->returnOnExit();
 133      $runner->throwOnError();
 134  
 135      $this->_createCommandClass($cmd, 'function fooBar(){return 1;}');
 136  
 137      $this->assertEqual($runner->execute(), 1);
 138    }
 139  
 140    function testPassArgvToAction()
 141    {
 142      $input = new lmbCliInput();
 143      $output = new lmbCliResponse();
 144  
 145      $input->strictMode(false);
 146      $input->read(array('foo.php', $cmd = $this->_randomName(), 'foo', '--dry-run', '-c', 'bar'));
 147  
 148      $runner = new lmbCliRunner($input, $output);
 149      $runner->setCommandSearchPath($this->tmp_dir);
 150      $runner->returnOnExit();
 151      $runner->throwOnError();
 152  
 153      $this->_createCommandClass($cmd, 'function foo($argv){var_dump($argv);}');
 154  
 155      ob_start();
 156      $runner->execute();
 157      $str = ob_get_contents();
 158      ob_end_clean();
 159  
 160      $expected = <<<EOD
 161  array(3) {
 162    [0]=>
 163    string(9) "--dry-run"
 164    [1]=>
 165    string(2) "-c"
 166    [2]=>
 167    string(3) "bar"
 168  }
 169  
 170  EOD;
 171  
 172      $this->assertEqual($expected, $str);
 173    }
 174  
 175    function _createCommandClass($name, $body='')
 176    {
 177      $class = lmbCliRunner :: commandToClass($name);
 178  
 179      $php = <<<EOD
 180  <?php
 181  class $class extends lmbCliBaseCmd
 182  {
 183    $body
 184  }
 185  ?>
 186  EOD;
 187      file_put_contents(LIMB_VAR_DIR . '/tmp_cmd/' . $class . '.class.php', $php);
 188    }
 189  
 190    function _randomName()
 191    {
 192      return 'foo' . mt_rand();
 193    }
 194  }
 195  
 196  ?>


Generated: Fri Dec 5 04:05:07 2008 Cross-referenced by PHPXref 0.7