InputArgumentTest.php 4.4 KB

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