ArrayInputTest.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Input\InputDefinition;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputOption;
  16. class ArrayInputTest extends TestCase
  17. {
  18. public function testGetFirstArgument()
  19. {
  20. $input = new ArrayInput(array());
  21. $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null if no argument were passed');
  22. $input = new ArrayInput(array('name' => 'Fabien'));
  23. $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
  24. $input = new ArrayInput(array('--foo' => 'bar', 'name' => 'Fabien'));
  25. $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
  26. }
  27. public function testHasParameterOption()
  28. {
  29. $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
  30. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  31. $this->assertFalse($input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters');
  32. $input = new ArrayInput(array('--foo'));
  33. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  34. $input = new ArrayInput(array('--foo', '--', '--bar'));
  35. $this->assertTrue($input->hasParameterOption('--bar'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  36. $this->assertFalse($input->hasParameterOption('--bar', true), '->hasParameterOption() returns false if an option is present in the passed parameters after an end of options signal');
  37. }
  38. public function testGetParameterOption()
  39. {
  40. $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
  41. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  42. $this->assertFalse($input->getParameterOption('--bar'), '->getParameterOption() returns the default if an option is not present in the passed parameters');
  43. $input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
  44. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  45. $input = new ArrayInput(array('--foo', '--', '--bar' => 'woop'));
  46. $this->assertEquals('woop', $input->getParameterOption('--bar'), '->getParameterOption() returns the correct value if an option is present in the passed parameters');
  47. $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');
  48. }
  49. public function testParseArguments()
  50. {
  51. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  52. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
  53. }
  54. /**
  55. * @dataProvider provideOptions
  56. */
  57. public function testParseOptions($input, $options, $expectedOptions, $message)
  58. {
  59. $input = new ArrayInput($input, new InputDefinition($options));
  60. $this->assertEquals($expectedOptions, $input->getOptions(), $message);
  61. }
  62. public function provideOptions()
  63. {
  64. return array(
  65. array(
  66. array('--foo' => 'bar'),
  67. array(new InputOption('foo')),
  68. array('foo' => 'bar'),
  69. '->parse() parses long options',
  70. ),
  71. array(
  72. array('--foo' => 'bar'),
  73. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  74. array('foo' => 'bar'),
  75. '->parse() parses long options with a default value',
  76. ),
  77. array(
  78. array(),
  79. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  80. array('foo' => 'default'),
  81. '->parse() uses the default value for long options with value optional which are not passed',
  82. ),
  83. array(
  84. array('--foo' => null),
  85. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  86. array('foo' => null),
  87. '->parse() parses long options with a default value',
  88. ),
  89. array(
  90. array('-f' => 'bar'),
  91. array(new InputOption('foo', 'f')),
  92. array('foo' => 'bar'),
  93. '->parse() parses short options',
  94. ),
  95. array(
  96. array('--' => null, '-f' => 'bar'),
  97. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  98. array('foo' => 'default'),
  99. '->parse() does not parse opts after an end of options signal',
  100. ),
  101. array(
  102. array('--' => null),
  103. array(),
  104. array(),
  105. '->parse() does not choke on end of options signal',
  106. ),
  107. );
  108. }
  109. /**
  110. * @dataProvider provideInvalidInput
  111. */
  112. public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
  113. {
  114. if (method_exists($this, 'expectException')) {
  115. $this->expectException('InvalidArgumentException');
  116. $this->expectExceptionMessage($expectedExceptionMessage);
  117. } else {
  118. $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
  119. }
  120. new ArrayInput($parameters, $definition);
  121. }
  122. public function provideInvalidInput()
  123. {
  124. return array(
  125. array(
  126. array('foo' => 'foo'),
  127. new InputDefinition(array(new InputArgument('name'))),
  128. 'The "foo" argument does not exist.',
  129. ),
  130. array(
  131. array('--foo' => null),
  132. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
  133. 'The "--foo" option requires a value.',
  134. ),
  135. array(
  136. array('--foo' => 'foo'),
  137. new InputDefinition(),
  138. 'The "--foo" option does not exist.',
  139. ),
  140. array(
  141. array('-o' => 'foo'),
  142. new InputDefinition(),
  143. 'The "-o" option does not exist.',
  144. ),
  145. );
  146. }
  147. public function testToString()
  148. {
  149. $input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"));
  150. $this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input);
  151. $input = new ArrayInput(array('-b' => array('bval_1', 'bval_2'), '--f' => array('fval_1', 'fval_2')));
  152. $this->assertSame('-b=bval_1 -b=bval_2 --f=fval_1 --f=fval_2', (string) $input);
  153. $input = new ArrayInput(array('array_arg' => array('val_1', 'val_2')));
  154. $this->assertSame('val_1 val_2', (string) $input);
  155. }
  156. }