ArrayInputTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(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. }
  35. public function testGetParameterOption()
  36. {
  37. $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
  38. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  39. $input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
  40. $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
  41. }
  42. public function testParseArguments()
  43. {
  44. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  45. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
  46. }
  47. /**
  48. * @dataProvider provideOptions
  49. */
  50. public function testParseOptions($input, $options, $expectedOptions, $message)
  51. {
  52. $input = new ArrayInput($input, new InputDefinition($options));
  53. $this->assertEquals($expectedOptions, $input->getOptions(), $message);
  54. }
  55. public function provideOptions()
  56. {
  57. return array(
  58. array(
  59. array('--foo' => 'bar'),
  60. array(new InputOption('foo')),
  61. array('foo' => 'bar'),
  62. '->parse() parses long options',
  63. ),
  64. array(
  65. array('--foo' => 'bar'),
  66. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  67. array('foo' => 'bar'),
  68. '->parse() parses long options with a default value',
  69. ),
  70. array(
  71. array('--foo' => null),
  72. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
  73. array('foo' => 'default'),
  74. '->parse() parses long options with a default value',
  75. ),
  76. array(
  77. array('-f' => 'bar'),
  78. array(new InputOption('foo', 'f')),
  79. array('foo' => 'bar'),
  80. '->parse() parses short options',
  81. ),
  82. );
  83. }
  84. /**
  85. * @dataProvider provideInvalidInput
  86. */
  87. public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
  88. {
  89. if (method_exists($this, 'expectException')) {
  90. $this->expectException('InvalidArgumentException');
  91. $this->expectExceptionMessage($expectedExceptionMessage);
  92. } else {
  93. $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
  94. }
  95. new ArrayInput($parameters, $definition);
  96. }
  97. public function provideInvalidInput()
  98. {
  99. return array(
  100. array(
  101. array('foo' => 'foo'),
  102. new InputDefinition(array(new InputArgument('name'))),
  103. 'The "foo" argument does not exist.',
  104. ),
  105. array(
  106. array('--foo' => null),
  107. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
  108. 'The "--foo" option requires a value.',
  109. ),
  110. array(
  111. array('--foo' => 'foo'),
  112. new InputDefinition(),
  113. 'The "--foo" option does not exist.',
  114. ),
  115. array(
  116. array('-o' => 'foo'),
  117. new InputDefinition(),
  118. 'The "-o" option does not exist.',
  119. ),
  120. );
  121. }
  122. public function testToString()
  123. {
  124. $input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"));
  125. $this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input);
  126. $input = new ArrayInput(array('-b' => array('bval_1', 'bval_2'), '--f' => array('fval_1', 'fval_2')));
  127. $this->assertSame('-b=bval_1 -b=bval_2 --f=fval_1 --f=fval_2', (string) $input);
  128. $input = new ArrayInput(array('array_arg' => array('val_1', 'val_2')));
  129. $this->assertSame('val_1 val_2', (string) $input);
  130. }
  131. }