InputArgumentTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\Console\Input;
  10. use Symfony\Components\Console\Input\InputArgument;
  11. use Symfony\Components\Console\Exception;
  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. // mode argument
  19. $argument = new InputArgument('foo');
  20. $this->assertFalse($argument->isRequired(), '__construct() gives a "Argument::OPTIONAL" mode by default');
  21. $argument = new InputArgument('foo', null);
  22. $this->assertFalse($argument->isRequired(), '__construct() can take "Argument::OPTIONAL" as its mode');
  23. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  24. $this->assertFalse($argument->isRequired(), '__construct() can take "Argument::PARAMETER_OPTIONAL" as its mode');
  25. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  26. $this->assertTrue($argument->isRequired(), '__construct() can take "Argument::PARAMETER_REQUIRED" as its mode');
  27. try
  28. {
  29. $argument = new InputArgument('foo', 'ANOTHER_ONE');
  30. $this->fail('__construct() throws an Exception if the mode is not valid');
  31. }
  32. catch (\Exception $e)
  33. {
  34. $this->assertType('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
  35. $this->assertEquals('Argument mode "ANOTHER_ONE" is not valid.', $e->getMessage());
  36. }
  37. }
  38. public function testIsArray()
  39. {
  40. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  41. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  42. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  43. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  44. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  45. $this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
  46. }
  47. public function testGetDescription()
  48. {
  49. $argument = new InputArgument('foo', null, 'Some description');
  50. $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
  51. }
  52. public function testGetDefault()
  53. {
  54. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  55. $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
  56. }
  57. public function testSetDefault()
  58. {
  59. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  60. $argument->setDefault(null);
  61. $this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
  62. $argument->setDefault('another');
  63. $this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
  64. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  65. $argument->setDefault(array(1, 2));
  66. $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
  67. try
  68. {
  69. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  70. $argument->setDefault('default');
  71. $this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
  72. }
  73. catch (\Exception $e)
  74. {
  75. $this->assertType('\Exception', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
  76. $this->assertEquals('Cannot set a default value except for Parameter::OPTIONAL mode.', $e->getMessage());
  77. }
  78. try
  79. {
  80. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  81. $argument->setDefault('default');
  82. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  83. }
  84. catch (\Exception $e)
  85. {
  86. $this->assertType('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  87. $this->assertEquals('A default value for an array argument must be an array.', $e->getMessage());
  88. }
  89. }
  90. }