ArrayInputTest.php 4.8 KB

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