ArrayInputTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Input;
  11. use Symfony\Component\Console\Input\ArrayInput;
  12. use Symfony\Component\Console\Input\InputDefinition;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputOption;
  15. class ArrayInputTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testGetFirstArgument()
  18. {
  19. $input = new ArrayInput(array());
  20. $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null if no argument were passed');
  21. $input = new ArrayInput(array('name' => 'Fabien'));
  22. $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
  23. $input = new ArrayInput(array('--foo' => 'bar', 'name' => 'Fabien'));
  24. $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
  25. }
  26. public function testHasParameterOption()
  27. {
  28. $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
  29. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  30. $this->assertFalse($input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters');
  31. $input = new ArrayInput(array('--foo'));
  32. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  33. $input = new ArrayInput(array('--foo', '--', '--bar'));
  34. $this->assertTrue($input->hasParameterOption('--bar'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  35. $this->assertFalse($input->hasParameterOption('--bar', true), '->hasParameterOption() returns false if an option is present in the passed parameters after an end of options signal');
  36. }
  37. public function testGetParameterOption()
  38. {
  39. $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
  40. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  41. $this->assertFalse($input->getParameterOption('--bar'), '->getParameterOption() returns the default if an option is not present in the passed parameters');
  42. $input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
  43. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  44. $input = new ArrayInput(array('--foo', '--', '--bar' => 'woop'));
  45. $this->assertEquals('woop', $input->getParameterOption('--bar'), '->getParameterOption() returns the correct value if an option is present in the passed parameters');
  46. $this->assertFalse($input->getParameterOption('--bar', false, true), '->getParameterOption() returns false if an option is present in the passed parameters after an end of options signal');
  47. }
  48. public function testParseArguments()
  49. {
  50. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  51. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
  52. }
  53. /**
  54. * @dataProvider provideOptions
  55. */
  56. public function testParseOptions($input, $options, $expectedOptions, $message)
  57. {
  58. $input = new ArrayInput($input, new InputDefinition($options));
  59. $this->assertEquals($expectedOptions, $input->getOptions(), $message);
  60. }
  61. public function provideOptions()
  62. {
  63. return array(
  64. array(
  65. array('--foo' => 'bar'),
  66. array(new InputOption('foo')),
  67. array('foo' => 'bar'),
  68. '->parse() parses long options',
  69. ),
  70. array(
  71. array('--foo' => 'bar'),
  72. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  73. array('foo' => 'bar'),
  74. '->parse() parses long options with a default value',
  75. ),
  76. array(
  77. array('--foo' => null),
  78. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  79. array('foo' => 'default'),
  80. '->parse() parses long options with a default value',
  81. ),
  82. array(
  83. array('-f' => 'bar'),
  84. array(new InputOption('foo', 'f')),
  85. array('foo' => 'bar'),
  86. '->parse() parses short options',
  87. ),
  88. array(
  89. array('--' => null, '-f' => 'bar'),
  90. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  91. array('foo' => 'default'),
  92. '->parse() does not parse opts after an end of options signal',
  93. ),
  94. array(
  95. array('--' => null),
  96. array(),
  97. array(),
  98. '->parse() does not choke on end of options signal',
  99. ),
  100. );
  101. }
  102. /**
  103. * @dataProvider provideInvalidInput
  104. */
  105. public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
  106. {
  107. $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
  108. new ArrayInput($parameters, $definition);
  109. }
  110. public function provideInvalidInput()
  111. {
  112. return array(
  113. array(
  114. array('foo' => 'foo'),
  115. new InputDefinition(array(new InputArgument('name'))),
  116. 'The "foo" argument does not exist.',
  117. ),
  118. array(
  119. array('--foo' => null),
  120. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
  121. 'The "--foo" option requires a value.',
  122. ),
  123. array(
  124. array('--foo' => 'foo'),
  125. new InputDefinition(),
  126. 'The "--foo" option does not exist.',
  127. ),
  128. array(
  129. array('-o' => 'foo'),
  130. new InputDefinition(),
  131. 'The "-o" option does not exist.',
  132. ),
  133. );
  134. }
  135. public function testToString()
  136. {
  137. $input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"));
  138. $this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input);
  139. }
  140. }