InputArgumentTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. require_once __DIR__.'/../../../bootstrap.php';
  11. use Symfony\Components\Console\Input\InputArgument;
  12. use Symfony\Components\Console\Exception;
  13. class InputArgumentTest extends \PHPUnit_Framework_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. // mode argument
  20. $argument = new InputArgument('foo');
  21. $this->assertEquals(false, $argument->isRequired(), '__construct() gives a "Argument::OPTIONAL" mode by default');
  22. $argument = new InputArgument('foo', null);
  23. $this->assertEquals(false, $argument->isRequired(), '__construct() can take "Argument::OPTIONAL" as its mode');
  24. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  25. $this->assertEquals(false, $argument->isRequired(), '__construct() can take "Argument::PARAMETER_OPTIONAL" as its mode');
  26. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  27. $this->assertEquals(true, $argument->isRequired(), '__construct() can take "Argument::PARAMETER_REQUIRED" as its mode');
  28. try
  29. {
  30. $argument = new InputArgument('foo', 'ANOTHER_ONE');
  31. $this->fail('__construct() throws an Exception if the mode is not valid');
  32. }
  33. catch (\Exception $e)
  34. {
  35. }
  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->assertTrue(!$argument->isArray(), '->isArray() returns false if the argument can not 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->assertTrue(is_null($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(array(1, 2));
  65. $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
  66. try
  67. {
  68. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  69. $argument->setDefault('default');
  70. $this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
  71. }
  72. catch (\Exception $e)
  73. {
  74. }
  75. try
  76. {
  77. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  78. $argument->setDefault('default');
  79. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  80. }
  81. catch (\Exception $e)
  82. {
  83. }
  84. }
  85. }