ArrayInputTest.php 7.4 KB

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\InputArgument;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputOption;
  16. class ArrayInputTest extends TestCase
  17. {
  18. public function testGetFirstArgument()
  19. {
  20. $input = new ArrayInput([]);
  21. $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null if no argument were passed');
  22. $input = new ArrayInput(['name' => 'Fabien']);
  23. $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
  24. $input = new ArrayInput(['--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(['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(['--foo']);
  33. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  34. $input = new ArrayInput(['--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(['name' => 'Fabien', '--foo' => 'bar']);
  41. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  42. $this->assertEquals('default', $input->getParameterOption('--bar', 'default'), '->getParameterOption() returns the default value if an option is not present in the passed parameters');
  43. $input = new ArrayInput(['Fabien', '--foo' => 'bar']);
  44. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  45. $input = new ArrayInput(['--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->assertEquals('default', $input->getParameterOption('--bar', 'default', true), '->getParameterOption() returns the default value 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(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
  52. $this->assertEquals(['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 [
  65. [
  66. ['--foo' => 'bar'],
  67. [new InputOption('foo')],
  68. ['foo' => 'bar'],
  69. '->parse() parses long options',
  70. ],
  71. [
  72. ['--foo' => 'bar'],
  73. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
  74. ['foo' => 'bar'],
  75. '->parse() parses long options with a default value',
  76. ],
  77. [
  78. [],
  79. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
  80. ['foo' => 'default'],
  81. '->parse() uses the default value for long options with value optional which are not passed',
  82. ],
  83. [
  84. ['--foo' => null],
  85. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
  86. ['foo' => null],
  87. '->parse() parses long options with a default value',
  88. ],
  89. [
  90. ['-f' => 'bar'],
  91. [new InputOption('foo', 'f')],
  92. ['foo' => 'bar'],
  93. '->parse() parses short options',
  94. ],
  95. [
  96. ['--' => null, '-f' => 'bar'],
  97. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')],
  98. ['foo' => 'default'],
  99. '->parse() does not parse opts after an end of options signal',
  100. ],
  101. [
  102. ['--' => null],
  103. [],
  104. [],
  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 [
  125. [
  126. ['foo' => 'foo'],
  127. new InputDefinition([new InputArgument('name')]),
  128. 'The "foo" argument does not exist.',
  129. ],
  130. [
  131. ['--foo' => null],
  132. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]),
  133. 'The "--foo" option requires a value.',
  134. ],
  135. [
  136. ['--foo' => 'foo'],
  137. new InputDefinition(),
  138. 'The "--foo" option does not exist.',
  139. ],
  140. [
  141. ['-o' => 'foo'],
  142. new InputDefinition(),
  143. 'The "-o" option does not exist.',
  144. ],
  145. ];
  146. }
  147. public function testToString()
  148. {
  149. $input = new ArrayInput(['-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(['-b' => ['bval_1', 'bval_2'], '--f' => ['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_arg' => ['val_1', 'val_2']]);
  154. $this->assertSame('val_1 val_2', (string) $input);
  155. }
  156. }