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.
 
 
 

113 line
3.8 KiB

  1. <?php
  2. namespace Ulrichsg\Getopt;
  3. class OptionParserTest extends \PHPUnit_Framework_TestCase
  4. {
  5. /** @var OptionParser */
  6. private $parser;
  7. public function setUp()
  8. {
  9. $this->parser = new OptionParser(Getopt::REQUIRED_ARGUMENT);
  10. }
  11. public function testParseString()
  12. {
  13. $options = $this->parser->parseString('ab:c::3');
  14. $this->assertInternalType('array', $options);
  15. $this->assertCount(4, $options);
  16. foreach ($options as $option) {
  17. $this->assertInstanceOf('Ulrichsg\Getopt\Option', $option);
  18. $this->assertNull($option->long());
  19. switch ($option->short()) {
  20. case 'a':
  21. case '3':
  22. $this->assertEquals(Getopt::NO_ARGUMENT, $option->mode());
  23. break;
  24. case 'b':
  25. $this->assertEquals(Getopt::REQUIRED_ARGUMENT, $option->mode());
  26. break;
  27. case 'c':
  28. $this->assertEquals(Getopt::OPTIONAL_ARGUMENT, $option->mode());
  29. break;
  30. default:
  31. $this->fail('Unexpected option: '.$option->short());
  32. }
  33. }
  34. }
  35. public function testParseStringEmpty()
  36. {
  37. $this->setExpectedException('InvalidArgumentException');
  38. $this->parser->parseString('');
  39. }
  40. public function testParseStringInvalidCharacter()
  41. {
  42. $this->setExpectedException('InvalidArgumentException');
  43. $this->parser->parseString('ab:c::dä');
  44. }
  45. public function testParseStringStartsWithColon()
  46. {
  47. $this->setExpectedException('InvalidArgumentException');
  48. $this->parser->parseString(':ab:c::d');
  49. }
  50. public function testParseStringTripleColon()
  51. {
  52. $this->setExpectedException('InvalidArgumentException');
  53. $this->parser->parseString('ab:c:::d');
  54. }
  55. public function testParseArray()
  56. {
  57. $options = $this->parser->parseArray(
  58. array(
  59. array('a', 'alpha', Getopt::OPTIONAL_ARGUMENT, 'Description', 42),
  60. new Option('b', 'beta'),
  61. array('c')
  62. )
  63. );
  64. $this->assertCount(3, $options);
  65. foreach ($options as $option) {
  66. $this->assertInstanceOf('Ulrichsg\Getopt\Option', $option);
  67. switch ($option->short()) {
  68. case 'a':
  69. $this->assertEquals('alpha', $option->long());
  70. $this->assertEquals(Getopt::OPTIONAL_ARGUMENT, $option->mode());
  71. $this->assertEquals('Description', $option->getDescription());
  72. $this->assertEquals(42, $option->getArgument()->getDefaultValue());
  73. break;
  74. case 'b':
  75. $this->assertEquals('beta', $option->long());
  76. $this->assertEquals(Getopt::NO_ARGUMENT, $option->mode());
  77. $this->assertEquals('', $option->getDescription());
  78. break;
  79. case 'c':
  80. $this->assertNull($option->long());
  81. $this->assertEquals(Getopt::REQUIRED_ARGUMENT, $option->mode());
  82. $this->assertEquals('', $option->getDescription());
  83. $this->assertFalse($option->getArgument()->hasDefaultValue());
  84. break;
  85. default:
  86. $this->fail('Unexpected option: '.$option->short());
  87. }
  88. }
  89. }
  90. public function testParseArrayEmpty()
  91. {
  92. $this->setExpectedException('InvalidArgumentException');
  93. $this->parser->parseArray(array());
  94. }
  95. public function testParseArrayInvalid()
  96. {
  97. $this->setExpectedException('InvalidArgumentException');
  98. $this->parser->parseArray(array('a', 'b'));
  99. }
  100. }