InputArgumentTest.php 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Component\Console\Input;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\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 "InputArgument::OPTIONAL" mode by default');
  21. $argument = new InputArgument('foo', null);
  22. $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
  23. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  24. $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
  25. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  26. $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
  27. try {
  28. $argument = new InputArgument('foo', 'ANOTHER_ONE');
  29. $this->fail('__construct() throws an Exception if the mode is not valid');
  30. } catch (\Exception $e) {
  31. $this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
  32. $this->assertEquals('Argument mode "ANOTHER_ONE" is not valid.', $e->getMessage());
  33. }
  34. }
  35. public function testIsArray()
  36. {
  37. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  38. $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array');
  39. $argument = new InputArgument('foo', InputArgument::OPTIONAL | 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);
  42. $this->assertFalse($argument->isArray(), '->isArray() returns false if the argument can not be an array');
  43. }
  44. public function testGetDescription()
  45. {
  46. $argument = new InputArgument('foo', null, 'Some description');
  47. $this->assertEquals('Some description', $argument->getDescription(), '->getDescription() return the message description');
  48. }
  49. public function testGetDefault()
  50. {
  51. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  52. $this->assertEquals('default', $argument->getDefault(), '->getDefault() return the default value');
  53. }
  54. public function testSetDefault()
  55. {
  56. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  57. $argument->setDefault(null);
  58. $this->assertNull($argument->getDefault(), '->setDefault() can reset the default value by passing null');
  59. $argument->setDefault('another');
  60. $this->assertEquals('another', $argument->getDefault(), '->setDefault() changes the default value');
  61. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  62. $argument->setDefault(array(1, 2));
  63. $this->assertEquals(array(1, 2), $argument->getDefault(), '->setDefault() changes the default value');
  64. try {
  65. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  66. $argument->setDefault('default');
  67. $this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
  68. } catch (\Exception $e) {
  69. $this->assertInstanceOf('\Exception', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
  70. $this->assertEquals('Cannot set a default value except for Parameter::OPTIONAL mode.', $e->getMessage());
  71. }
  72. try {
  73. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  74. $argument->setDefault('default');
  75. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  76. } catch (\Exception $e) {
  77. $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  78. $this->assertEquals('A default value for an array argument must be an array.', $e->getMessage());
  79. }
  80. }
  81. }