InputArgumentTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\InputArgument;
  13. class InputArgumentTest extends TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $argument = new InputArgument('foo');
  18. $this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
  19. }
  20. public function testModes()
  21. {
  22. $argument = new InputArgument('foo');
  23. $this->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
  24. $argument = new InputArgument('foo', null);
  25. $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
  26. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  27. $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
  28. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  29. $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
  30. }
  31. /**
  32. * @dataProvider provideInvalidModes
  33. */
  34. public function testInvalidModes($mode)
  35. {
  36. $this->expectException('InvalidArgumentException');
  37. $this->expectExceptionMessage(sprintf('Argument mode "%s" is not valid.', $mode));
  38. new InputArgument('foo', $mode);
  39. }
  40. public function provideInvalidModes()
  41. {
  42. return [
  43. ['ANOTHER_ONE'],
  44. [-1],
  45. ];
  46. }
  47. public function testIsArray()
  48. {
  49. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  50. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  51. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  52. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  53. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  54. $this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
  55. }
  56. public function testGetDescription()
  57. {
  58. $argument = new InputArgument('foo', null, 'Some description');
  59. $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
  60. }
  61. public function testGetDefault()
  62. {
  63. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  64. $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
  65. }
  66. public function testSetDefault()
  67. {
  68. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  69. $argument->setDefault(null);
  70. $this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
  71. $argument->setDefault('another');
  72. $this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
  73. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  74. $argument->setDefault([1, 2]);
  75. $this->assertEquals([1, 2], $argument->getDefault(), '->setDefault() changes the default value');
  76. }
  77. public function testSetDefaultWithRequiredArgument()
  78. {
  79. $this->expectException('LogicException');
  80. $this->expectExceptionMessage('Cannot set a default value except for InputArgument::OPTIONAL mode.');
  81. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  82. $argument->setDefault('default');
  83. }
  84. public function testSetDefaultWithArrayArgument()
  85. {
  86. $this->expectException('LogicException');
  87. $this->expectExceptionMessage('A default value for an array argument must be an array.');
  88. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  89. $argument->setDefault('default');
  90. }
  91. }