InputArgumentTest.php 4.4 KB

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