ArrayInputTest.php 5.3 KB

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