2010-03-18 07:48:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-25 16:06:54 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-03-18 07:48:03 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Console\Tests\Input;
|
2010-03-18 07:48:03 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
use Symfony\Component\Console\Input\InputDefinition;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2010-03-18 07:48:03 +00:00
|
|
|
|
|
|
|
class InputTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testConstructor()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
|
|
|
|
$this->assertEquals('foo', $input->getArgument('name'), '->__construct() takes a InputDefinition as an argument');
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testOptions()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'))));
|
|
|
|
$this->assertEquals('foo', $input->getOption('name'), '->getOption() returns the value for the given option');
|
|
|
|
|
|
|
|
$input->setOption('name', 'bar');
|
|
|
|
$this->assertEquals('bar', $input->getOption('name'), '->setOption() sets the value for a given option');
|
|
|
|
$this->assertEquals(array('name' => 'bar'), $input->getOptions(), '->getOptions() returns all option values');
|
|
|
|
|
2010-11-27 16:56:55 +00:00
|
|
|
$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
|
|
|
|
$this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones');
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$input->setOption('foo', 'bar');
|
|
|
|
$this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
|
|
|
|
$this->assertEquals('The "foo" option does not exist.', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$input->getOption('foo');
|
|
|
|
$this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
|
|
|
|
$this->assertEquals('The "foo" option does not exist.', $e->getMessage());
|
|
|
|
}
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testArguments()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
|
|
|
|
$this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
|
|
|
|
|
|
|
|
$input->setArgument('name', 'bar');
|
|
|
|
$this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
|
|
|
|
$this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values');
|
|
|
|
|
|
|
|
$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
|
|
|
|
$this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
|
|
|
|
$this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$input->setArgument('foo', 'bar');
|
|
|
|
$this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
|
|
|
|
$this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$input->getArgument('foo');
|
|
|
|
$this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
|
|
|
|
$this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
|
|
|
|
}
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testValidate()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$input = new ArrayInput(array());
|
|
|
|
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$input->validate();
|
|
|
|
$this->fail('->validate() throws a \RuntimeException if not enough arguments are given');
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\Exception $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertInstanceOf('\RuntimeException', $e, '->validate() throws a \RuntimeException if not enough arguments are given');
|
|
|
|
$this->assertEquals('Not enough arguments.', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$input = new ArrayInput(array('name' => 'foo'));
|
|
|
|
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
try {
|
2010-05-06 12:25:53 +01:00
|
|
|
$input->validate();
|
2010-05-07 15:09:11 +01:00
|
|
|
} catch (\RuntimeException $e) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
|
|
|
|
}
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function testSetFetInteractive()
|
2010-03-18 07:48:03 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$input = new ArrayInput(array());
|
|
|
|
$this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
|
|
|
|
$input->setInteractive(false);
|
|
|
|
$this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
|
2010-03-18 07:48:03 +00:00
|
|
|
}
|
|
|
|
}
|