InputArgumentTest.php 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\Console\Input;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\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->assertFalse($argument->isRequired(), '__construct() gives a "InputArgument::OPTIONAL" mode by default');
  22. $argument = new InputArgument('foo', null);
  23. $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
  24. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  25. $this->assertFalse($argument->isRequired(), '__construct() can take "InputArgument::OPTIONAL" as its mode');
  26. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  27. $this->assertTrue($argument->isRequired(), '__construct() can take "InputArgument::REQUIRED" as its mode');
  28. try {
  29. $argument = new InputArgument('foo', 'ANOTHER_ONE');
  30. $this->fail('__construct() throws an Exception if the mode is not valid');
  31. } catch (\Exception $e) {
  32. $this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
  33. $this->assertEquals('Argument mode "ANOTHER_ONE" is not valid.', $e->getMessage());
  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->assertFalse($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->assertNull($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. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  67. $argument->setDefault('default');
  68. $this->fail('->setDefault() throws an Exception if you give a default value for a required argument');
  69. } catch (\Exception $e) {
  70. $this->assertInstanceOf('\Exception', $e, '->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
  71. $this->assertEquals('Cannot set a default value except for Parameter::OPTIONAL mode.', $e->getMessage());
  72. }
  73. try {
  74. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  75. $argument->setDefault('default');
  76. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  77. } catch (\Exception $e) {
  78. $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  79. $this->assertEquals('A default value for an array argument must be an array.', $e->getMessage());
  80. }
  81. }
  82. }