InputArgumentTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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->assertEquals(false, $argument->isRequired(), '__construct() gives a "Argument::OPTIONAL" mode by default');
  21. $argument = new InputArgument('foo', null);
  22. $this->assertEquals(false, $argument->isRequired(), '__construct() can take "Argument::OPTIONAL" as its mode');
  23. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  24. $this->assertEquals(false, $argument->isRequired(), '__construct() can take "Argument::PARAMETER_OPTIONAL" as its mode');
  25. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  26. $this->assertEquals(true, $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. }
  35. }
  36. public function testIsArray()
  37. {
  38. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  39. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  40. $argument = new InputArgument('foo', InputArgument::OPTIONAL | 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);
  43. $this->assertTrue(!$argument->isArray(), '->isArray() returns false if the argument can not be an array');
  44. }
  45. public function testGetDescription()
  46. {
  47. $argument = new InputArgument('foo', null, 'Some description');
  48. $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
  49. }
  50. public function testGetDefault()
  51. {
  52. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  53. $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
  54. }
  55. public function testSetDefault()
  56. {
  57. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  58. $argument->setDefault(null);
  59. $this->assertTrue(is_null($argument->getDefault()), '->setDefault() can reset the default value by passing null');
  60. $argument->setDefault('another');
  61. $this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
  62. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  63. $argument->setDefault(array(1, 2));
  64. $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
  65. try
  66. {
  67. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  68. $argument->setDefault('default');
  69. $this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
  70. }
  71. catch (\Exception $e)
  72. {
  73. }
  74. try
  75. {
  76. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  77. $argument->setDefault('default');
  78. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  79. }
  80. catch (\Exception $e)
  81. {
  82. }
  83. }
  84. }