InputArgumentTest.php 4.2 KB

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