InputArgumentTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * @expectedException \InvalidArgumentException
  33. * @expectedExceptionMessage Argument mode "-1" is not valid.
  34. */
  35. public function testInvalidModes()
  36. {
  37. new InputArgument('foo', '-1');
  38. }
  39. public function testIsArray()
  40. {
  41. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  42. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  43. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  44. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  45. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  46. $this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
  47. }
  48. public function testGetDescription()
  49. {
  50. $argument = new InputArgument('foo', null, 'Some description');
  51. $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
  52. }
  53. public function testGetDefault()
  54. {
  55. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  56. $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
  57. }
  58. public function testSetDefault()
  59. {
  60. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  61. $argument->setDefault(null);
  62. $this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
  63. $argument->setDefault('another');
  64. $this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
  65. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  66. $argument->setDefault([1, 2]);
  67. $this->assertEquals([1, 2], $argument->getDefault(), '->setDefault() changes the default value');
  68. }
  69. /**
  70. * @expectedException \LogicException
  71. * @expectedExceptionMessage Cannot set a default value except for InputArgument::OPTIONAL mode.
  72. */
  73. public function testSetDefaultWithRequiredArgument()
  74. {
  75. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  76. $argument->setDefault('default');
  77. }
  78. /**
  79. * @expectedException \LogicException
  80. * @expectedExceptionMessage A default value for an array argument must be an array.
  81. */
  82. public function testSetDefaultWithArrayArgument()
  83. {
  84. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  85. $argument->setDefault('default');
  86. }
  87. }