cli-barcode PHP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

200 line
6.2 KiB

  1. <?php
  2. namespace Ulrichsg\Getopt;
  3. class GetoptTest extends \PHPUnit_Framework_TestCase
  4. {
  5. public function testAddOptions()
  6. {
  7. $getopt = new Getopt();
  8. $getopt->addOptions('a:');
  9. $getopt->addOptions(
  10. array(
  11. array('s', null, Getopt::OPTIONAL_ARGUMENT),
  12. array(null, 'long', Getopt::OPTIONAL_ARGUMENT),
  13. array('n', 'name', Getopt::OPTIONAL_ARGUMENT)
  14. )
  15. );
  16. $getopt->parse('-a aparam -s sparam --long longparam');
  17. $this->assertEquals('aparam', $getopt->getOption('a'));
  18. $this->assertEquals('longparam', $getopt->getOption('long'));
  19. $this->assertEquals('sparam', $getopt->getOption('s'));
  20. }
  21. public function testAddOptionsChooseShortOrLongAutomatically()
  22. {
  23. $getopt = new Getopt();
  24. $getopt->addOptions(
  25. array(
  26. array('s'),
  27. array('long', Getopt::OPTIONAL_ARGUMENT)
  28. )
  29. );
  30. $getopt->parse('-s --long longparam');
  31. $this->assertEquals('longparam', $getopt->getOption('long'));
  32. $this->assertEquals('1', $getopt->getOption('s'));
  33. }
  34. public function testAddOptionsUseDefaultArgumentType()
  35. {
  36. $getopt = new Getopt(null, Getopt::REQUIRED_ARGUMENT);
  37. $getopt->addOptions(
  38. array(
  39. array('l', 'long')
  40. )
  41. );
  42. $getopt->parse('-l something');
  43. $this->assertEquals('something', $getopt->getOption('l'));
  44. $getopt->parse('--long someOtherThing');
  45. $this->assertEquals('someOtherThing', $getopt->getOption('long'));
  46. }
  47. public function testAddOptionsFailsOnInvalidArgument()
  48. {
  49. $this->setExpectedException('\InvalidArgumentException');
  50. $getopt = new Getopt(null);
  51. $getopt->addOptions(new Option('a', 'alpha'));
  52. }
  53. public function testAddOptionsOverwritesExistingOptions()
  54. {
  55. $getopt = new Getopt(array(
  56. array('a', null, Getopt::REQUIRED_ARGUMENT)
  57. ));
  58. $getopt->addOptions(array(
  59. array('a', null, Getopt::NO_ARGUMENT)
  60. ));
  61. $getopt->parse('-a foo');
  62. $this->assertEquals(1, $getopt->getOption('a'));
  63. $this->assertEquals('foo', $getopt->getOperand(0));
  64. }
  65. public function testAddOptionsFailsOnConflict()
  66. {
  67. $this->setExpectedException('\InvalidArgumentException');
  68. $getopt = new Getopt(array(
  69. array('v', 'version')
  70. ));
  71. $getopt->addOptions(array(
  72. array('v', 'verbose')
  73. ));
  74. }
  75. public function testParseUsesGlobalArgvWhenNoneGiven()
  76. {
  77. global $argv;
  78. $argv = array('foo.php', '-a');
  79. $getopt = new Getopt('a');
  80. $getopt->parse();
  81. $this->assertEquals(1, $getopt->getOption('a'));
  82. }
  83. public function testAccessMethods()
  84. {
  85. $getopt = new Getopt('a');
  86. $getopt->parse('-a foo');
  87. $options = $getopt->getOptions();
  88. $this->assertCount(1, $options);
  89. $this->assertEquals(1, $options['a']);
  90. $this->assertEquals(1, $getopt->getOption('a'));
  91. $operands = $getopt->getOperands();
  92. $this->assertCount(1, $operands);
  93. $this->assertEquals('foo', $operands[0]);
  94. $this->assertEquals('foo', $getopt->getOperand(0));
  95. }
  96. public function testCountable()
  97. {
  98. $getopt = new Getopt('abc');
  99. $getopt->parse('-abc');
  100. $this->assertEquals(3, count($getopt));
  101. }
  102. public function testArrayAccess()
  103. {
  104. $getopt = new Getopt('q');
  105. $getopt->parse('-q');
  106. $this->assertEquals(1, $getopt['q']);
  107. }
  108. public function testIterable()
  109. {
  110. $getopt = new Getopt(array(
  111. array(null, 'alpha', Getopt::NO_ARGUMENT),
  112. array('b', 'beta', Getopt::REQUIRED_ARGUMENT)
  113. ));
  114. $getopt->parse('--alpha -b foo');
  115. $expected = array('alpha' => 1, 'b' => 'foo'); // 'beta' should not occur
  116. foreach ($getopt as $option => $value) {
  117. $this->assertEquals($expected[$option], $value);
  118. }
  119. }
  120. public function testHelpText()
  121. {
  122. $getopt = new Getopt(array(
  123. array('a', 'alpha', Getopt::NO_ARGUMENT, 'Short and long options with no argument'),
  124. array(null, 'beta', Getopt::OPTIONAL_ARGUMENT, 'Long option only with an optional argument'),
  125. array('c', null, Getopt::REQUIRED_ARGUMENT, 'Short option only with a mandatory argument')
  126. ));
  127. $getopt->parse('');
  128. $script = $_SERVER['PHP_SELF'];
  129. $expected = "Usage: $script [options] [operands]\n";
  130. $expected .= "Options:\n";
  131. $expected .= " -a, --alpha Short and long options with no argument\n";
  132. $expected .= " --beta [<arg>] Long option only with an optional argument\n";
  133. $expected .= " -c <arg> Short option only with a mandatory argument\n";
  134. $this->assertEquals($expected, $getopt->getHelpText());
  135. }
  136. public function testHelpTextWithoutDescriptions()
  137. {
  138. $getopt = new Getopt(array(
  139. array('a', 'alpha', Getopt::NO_ARGUMENT),
  140. array(null, 'beta', Getopt::OPTIONAL_ARGUMENT),
  141. array('c', null, Getopt::REQUIRED_ARGUMENT)
  142. ));
  143. $getopt->parse('');
  144. $script = $_SERVER['PHP_SELF'];
  145. $expected = "Usage: $script [options] [operands]\n";
  146. $expected .= "Options:\n";
  147. $expected .= " -a, --alpha \n";
  148. $expected .= " --beta [<arg>] \n";
  149. $expected .= " -c <arg> \n";
  150. $this->assertEquals($expected, $getopt->getHelpText());
  151. }
  152. public function testHelpTextNoParse()
  153. {
  154. $getopt = new Getopt();
  155. $expected = "Usage: [options] [operands]\nOptions:\n";
  156. $this->assertSame($expected, $getopt->getHelpText());
  157. }
  158. public function testHelpTextWithCustomBanner()
  159. {
  160. $script = $_SERVER['PHP_SELF'];
  161. $getopt = new Getopt();
  162. $getopt->setBanner("My custom Banner %s\n");
  163. $this->assertSame("My custom Banner \nOptions:\n", $getopt->getHelpText());
  164. $getopt->parse('');
  165. $this->assertSame("My custom Banner $script\nOptions:\n", $getopt->getHelpText());
  166. }
  167. }