2010-10-07 03:18:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ConsoleOptionParserTest file
|
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-10-07 03:18:31 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-07 03:18:31 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Console
|
2010-10-07 03:18:31 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-10-07 03:18:31 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-08 19:15:23 +00:00
|
|
|
App::uses('ConsoleOptionParser', 'Console');
|
2010-10-07 03:18:31 +00:00
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* ConsoleOptionParserTest
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Console
|
|
|
|
*/
|
2010-10-07 03:18:31 +00:00
|
|
|
class ConsoleOptionParserTest extends CakeTestCase {
|
2010-10-08 01:18:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test setting the console description
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDescription() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 01:18:22 +00:00
|
|
|
$result = $parser->description('A test');
|
|
|
|
|
|
|
|
$this->assertEquals($parser, $result, 'Setting description is not chainable');
|
|
|
|
$this->assertEquals('A test', $parser->description(), 'getting value is wrong.');
|
2010-10-15 03:21:56 +00:00
|
|
|
|
2015-09-25 15:22:00 +00:00
|
|
|
$parser->description(array('A test', 'something'));
|
2010-10-10 21:18:51 +00:00
|
|
|
$this->assertEquals("A test\nsomething", $parser->description(), 'getting value is wrong.');
|
2010-10-08 01:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test setting the console epilog
|
2010-10-08 01:18:22 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testEpilog() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 01:18:22 +00:00
|
|
|
$result = $parser->epilog('A test');
|
|
|
|
|
|
|
|
$this->assertEquals($parser, $result, 'Setting epilog is not chainable');
|
|
|
|
$this->assertEquals('A test', $parser->epilog(), 'getting value is wrong.');
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2015-09-25 15:22:00 +00:00
|
|
|
$parser->epilog(array('A test', 'something'));
|
2010-10-10 21:18:51 +00:00
|
|
|
$this->assertEquals("A test\nsomething", $parser->epilog(), 'getting value is wrong.');
|
2010-10-08 01:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-08 01:46:19 +00:00
|
|
|
* test adding an option returns self.
|
2010-10-08 01:18:22 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionReturnSelf() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 01:18:22 +00:00
|
|
|
$result = $parser->addOption('test');
|
2010-10-08 01:46:19 +00:00
|
|
|
$this->assertEquals($parser, $result, 'Did not return $this from addOption');
|
|
|
|
}
|
|
|
|
|
2010-10-08 02:44:02 +00:00
|
|
|
/**
|
|
|
|
* test adding an option and using the long value for parsing.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionLong() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 02:44:02 +00:00
|
|
|
$parser->addOption('test', array(
|
2010-10-09 04:16:50 +00:00
|
|
|
'short' => 't'
|
2010-10-08 02:44:02 +00:00
|
|
|
));
|
|
|
|
$result = $parser->parse(array('--test', 'value'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
|
2010-10-08 02:44:02 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 03:10:00 +00:00
|
|
|
/**
|
|
|
|
* test adding an option with a zero value
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAddOptionZero() {
|
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addOption('count', array());
|
|
|
|
$result = $parser->parse(array('--count', '0'));
|
|
|
|
$this->assertEquals(array('count' => '0', 'help' => false), $result[0], 'Zero parameter did not parse out');
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:52:33 +00:00
|
|
|
/**
|
|
|
|
* test addOption with an object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionObject() {
|
2010-11-13 04:52:33 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addOption(new ConsoleInputOption('test', 't'));
|
|
|
|
$result = $parser->parse(array('--test=value'));
|
|
|
|
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
|
|
|
|
}
|
|
|
|
|
2010-10-08 03:08:32 +00:00
|
|
|
/**
|
|
|
|
* test adding an option and using the long value for parsing.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionLongEquals() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 03:08:32 +00:00
|
|
|
$parser->addOption('test', array(
|
2010-10-09 04:16:50 +00:00
|
|
|
'short' => 't'
|
2010-10-08 03:08:32 +00:00
|
|
|
));
|
|
|
|
$result = $parser->parse(array('--test=value'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
|
2010-10-08 03:08:32 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 02:44:02 +00:00
|
|
|
/**
|
|
|
|
* test adding an option and using the default.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionDefault() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 02:44:02 +00:00
|
|
|
$parser->addOption('test', array(
|
|
|
|
'default' => 'default value',
|
|
|
|
));
|
|
|
|
$result = $parser->parse(array('--test'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-12 03:12:38 +00:00
|
|
|
$parser->addOption('test', array(
|
|
|
|
'default' => 'default value',
|
|
|
|
));
|
|
|
|
$result = $parser->parse(array());
|
2010-10-12 03:43:13 +00:00
|
|
|
$this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
|
2010-10-08 02:44:02 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 01:46:19 +00:00
|
|
|
/**
|
2010-10-09 04:16:50 +00:00
|
|
|
* test adding an option and using the short value for parsing.
|
2010-10-08 01:46:19 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionShort() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 01:46:19 +00:00
|
|
|
$parser->addOption('test', array(
|
2010-10-09 04:16:50 +00:00
|
|
|
'short' => 't'
|
2010-10-08 01:46:19 +00:00
|
|
|
));
|
|
|
|
$result = $parser->parse(array('-t', 'value'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Short parameter did not parse out');
|
|
|
|
}
|
|
|
|
|
2011-08-06 19:21:42 +00:00
|
|
|
/**
|
|
|
|
* Test that adding an option using a two letter short value causes an exception.
|
|
|
|
* As they will not parse correctly.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAddOptionShortOneLetter() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(ConsoleException::class);
|
2011-08-06 19:21:42 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addOption('test', array('short' => 'te'));
|
|
|
|
}
|
|
|
|
|
2010-10-12 03:43:13 +00:00
|
|
|
/**
|
|
|
|
* test adding and using boolean options.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionBoolean() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addOption('test', array(
|
|
|
|
'boolean' => true,
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = $parser->parse(array('--test', 'value'));
|
|
|
|
$expected = array(array('test' => true, 'help' => false), array('value'));
|
|
|
|
$this->assertEquals($expected, $result);
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-10-12 03:43:13 +00:00
|
|
|
$result = $parser->parse(array('value'));
|
|
|
|
$expected = array(array('test' => false, 'help' => false), array('value'));
|
|
|
|
$this->assertEquals($expected, $result);
|
2010-10-08 03:13:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-09 04:16:50 +00:00
|
|
|
/**
|
|
|
|
* test adding an multiple shorts.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptionMultipleShort() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-16 04:39:22 +00:00
|
|
|
$parser->addOption('test', array('short' => 't', 'boolean' => true))
|
|
|
|
->addOption('file', array('short' => 'f', 'boolean' => true))
|
|
|
|
->addOption('output', array('short' => 'o', 'boolean' => true));
|
2010-10-09 04:16:50 +00:00
|
|
|
|
|
|
|
$result = $parser->parse(array('-o', '-t', '-f'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$expected = array('file' => true, 'test' => true, 'output' => true, 'help' => false);
|
2010-10-09 04:16:50 +00:00
|
|
|
$this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
|
2010-10-09 04:57:09 +00:00
|
|
|
|
2010-10-09 04:16:50 +00:00
|
|
|
$result = $parser->parse(array('-otf'));
|
|
|
|
$this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
|
|
|
|
}
|
|
|
|
|
2010-10-08 03:13:56 +00:00
|
|
|
/**
|
|
|
|
* test multiple options at once.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMultipleOptions() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-08 03:13:56 +00:00
|
|
|
$parser->addOption('test')
|
|
|
|
->addOption('connection')
|
2010-10-16 04:39:22 +00:00
|
|
|
->addOption('table', array('short' => 't', 'default' => true));
|
2010-10-08 03:13:56 +00:00
|
|
|
|
|
|
|
$result = $parser->parse(array('--test', 'value', '-t', '--connection', 'postgres'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false);
|
2010-10-08 03:13:56 +00:00
|
|
|
$this->assertEquals($expected, $result[0], 'multiple options did not parse');
|
2010-10-08 01:18:22 +00:00
|
|
|
}
|
2010-10-09 04:57:09 +00:00
|
|
|
|
2010-10-10 15:28:27 +00:00
|
|
|
/**
|
|
|
|
* Test adding multiple options.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddOptions() {
|
2010-10-10 15:28:27 +00:00
|
|
|
$parser = new ConsoleOptionParser('something', false);
|
|
|
|
$result = $parser->addOptions(array(
|
|
|
|
'name' => array('help' => 'The name'),
|
|
|
|
'other' => array('help' => 'The other arg')
|
|
|
|
));
|
|
|
|
$this->assertEquals($parser, $result, 'addOptions is not chainable.');
|
|
|
|
|
|
|
|
$result = $parser->options();
|
|
|
|
$this->assertEquals(3, count($result), 'Not enough options');
|
|
|
|
}
|
|
|
|
|
2010-10-09 14:33:54 +00:00
|
|
|
/**
|
|
|
|
* test that boolean options work
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOptionWithBooleanParam() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-09 14:33:54 +00:00
|
|
|
$parser->addOption('no-commit', array('boolean' => true))
|
|
|
|
->addOption('table', array('short' => 't'));
|
2010-10-10 00:13:27 +00:00
|
|
|
|
2010-10-09 14:33:54 +00:00
|
|
|
$result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$expected = array(array('table' => 'posts', 'no-commit' => true, 'help' => false), array('arg1', 'arg2'));
|
2010-10-09 14:33:54 +00:00
|
|
|
$this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
|
2010-10-10 00:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test parsing options that do not exist.
|
|
|
|
*
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2010-10-10 00:13:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOptionThatDoesNotExist() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(ConsoleException::class);
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-10 00:13:27 +00:00
|
|
|
$parser->addOption('no-commit', array('boolean' => true));
|
|
|
|
|
2013-01-23 12:45:50 +00:00
|
|
|
$parser->parse(array('--fail', 'other'));
|
2010-10-09 14:33:54 +00:00
|
|
|
}
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2011-09-01 19:43:38 +00:00
|
|
|
/**
|
|
|
|
* test parsing short options that do not exist.
|
|
|
|
*
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2011-09-01 19:43:38 +00:00
|
|
|
*/
|
|
|
|
public function testShortOptionThatDoesNotExist() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(ConsoleException::class);
|
2011-09-01 19:43:38 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addOption('no-commit', array('boolean' => true));
|
|
|
|
|
2013-01-23 12:45:50 +00:00
|
|
|
$parser->parse(array('-f'));
|
2011-09-01 19:43:38 +00:00
|
|
|
}
|
2010-10-09 14:33:54 +00:00
|
|
|
|
2010-10-10 19:04:16 +00:00
|
|
|
/**
|
|
|
|
* test that options with choices enforce them.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOptionWithChoices() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(ConsoleException::class);
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-10 19:04:16 +00:00
|
|
|
$parser->addOption('name', array('choices' => array('mark', 'jose')));
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-10-10 19:04:16 +00:00
|
|
|
$result = $parser->parse(array('--name', 'mark'));
|
2010-10-12 03:43:13 +00:00
|
|
|
$expected = array('name' => 'mark', 'help' => false);
|
2010-10-10 19:04:16 +00:00
|
|
|
$this->assertEquals($expected, $result[0], 'Got the correct value.');
|
|
|
|
|
2015-09-25 15:22:00 +00:00
|
|
|
$parser->parse(array('--name', 'jimmy'));
|
2010-10-10 19:04:16 +00:00
|
|
|
}
|
|
|
|
|
2011-08-06 19:43:41 +00:00
|
|
|
/**
|
|
|
|
* Ensure that option values can start with -
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testOptionWithValueStartingWithMinus() {
|
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addOption('name')
|
|
|
|
->addOption('age');
|
|
|
|
|
|
|
|
$result = $parser->parse(array('--name', '-foo', '--age', 'old'));
|
|
|
|
$expected = array('name' => '-foo', 'age' => 'old', 'help' => false);
|
|
|
|
$this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
|
|
|
|
}
|
|
|
|
|
2010-10-09 04:57:09 +00:00
|
|
|
/**
|
|
|
|
* test positional argument parsing.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPositionalArgument() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-09 04:57:09 +00:00
|
|
|
$result = $parser->addArgument('name', array('help' => 'An argument'));
|
2012-02-23 23:29:53 +00:00
|
|
|
$this->assertEquals($parser, $result, 'Should return this');
|
2010-10-09 04:57:09 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:52:33 +00:00
|
|
|
/**
|
|
|
|
* test addOption with an object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddArgumentObject() {
|
2010-11-13 04:52:33 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addArgument(new ConsoleInputArgument('test'));
|
|
|
|
$result = $parser->arguments();
|
2012-11-21 03:42:44 +00:00
|
|
|
$this->assertCount(1, $result);
|
2010-11-13 04:57:55 +00:00
|
|
|
$this->assertEquals('test', $result[0]->name());
|
2010-11-13 04:52:33 +00:00
|
|
|
}
|
|
|
|
|
2012-11-21 03:42:44 +00:00
|
|
|
/**
|
|
|
|
* Test adding arguments out of order.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAddArgumentOutOfOrder() {
|
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addArgument('name', array('index' => 1, 'help' => 'first argument'))
|
|
|
|
->addArgument('bag', array('index' => 2, 'help' => 'second argument'))
|
|
|
|
->addArgument('other', array('index' => 0, 'help' => 'Zeroth argument'));
|
|
|
|
|
|
|
|
$result = $parser->arguments();
|
|
|
|
$this->assertCount(3, $result);
|
|
|
|
$this->assertEquals('other', $result[0]->name());
|
|
|
|
$this->assertEquals('name', $result[1]->name());
|
|
|
|
$this->assertEquals('bag', $result[2]->name());
|
|
|
|
$this->assertSame(array(0, 1, 2), array_keys($result));
|
|
|
|
}
|
|
|
|
|
2010-10-09 04:57:09 +00:00
|
|
|
/**
|
|
|
|
* test overwriting positional arguments.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPositionalArgOverwrite() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-09 04:57:09 +00:00
|
|
|
$parser->addArgument('name', array('help' => 'An argument'))
|
|
|
|
->addArgument('other', array('index' => 0));
|
|
|
|
|
|
|
|
$result = $parser->arguments();
|
|
|
|
$this->assertEquals(1, count($result), 'Overwrite did not occur');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test parsing arguments.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParseArgumentTooMany() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(ConsoleException::class);
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-09 04:57:09 +00:00
|
|
|
$parser->addArgument('name', array('help' => 'An argument'))
|
|
|
|
->addArgument('other');
|
|
|
|
|
|
|
|
$expected = array('one', 'two');
|
|
|
|
$result = $parser->parse($expected);
|
|
|
|
$this->assertEquals($expected, $result[1], 'Arguments are not as expected');
|
2010-10-09 14:33:54 +00:00
|
|
|
|
2015-09-25 15:22:00 +00:00
|
|
|
$parser->parse(array('one', 'two', 'three'));
|
2010-10-09 04:57:09 +00:00
|
|
|
}
|
2010-10-09 14:41:11 +00:00
|
|
|
|
2012-01-17 19:01:16 +00:00
|
|
|
/**
|
|
|
|
* test parsing arguments with 0 value.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testParseArgumentZero() {
|
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
|
|
|
|
$expected = array('one', 'two', 0, 'after', 'zero');
|
|
|
|
$result = $parser->parse($expected);
|
|
|
|
$this->assertEquals($expected, $result[1], 'Arguments are not as expected');
|
|
|
|
}
|
|
|
|
|
2010-10-09 14:41:11 +00:00
|
|
|
/**
|
|
|
|
* test that when there are not enough arguments an exception is raised
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPositionalArgNotEnough() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(ConsoleException::class);
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-09 14:41:11 +00:00
|
|
|
$parser->addArgument('name', array('required' => true))
|
|
|
|
->addArgument('other', array('required' => true));
|
|
|
|
|
|
|
|
$parser->parse(array('one'));
|
|
|
|
}
|
2010-10-09 16:42:34 +00:00
|
|
|
|
2010-10-10 19:47:22 +00:00
|
|
|
/**
|
|
|
|
* test that arguments with choices enforce them.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPositionalArgWithChoices() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(ConsoleException::class);
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-11 05:51:00 +00:00
|
|
|
$parser->addArgument('name', array('choices' => array('mark', 'jose')))
|
|
|
|
->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
|
|
|
|
->addArgument('weapon', array('choices' => array('gun', 'sword')));
|
2010-10-10 19:47:22 +00:00
|
|
|
|
2010-10-11 05:51:00 +00:00
|
|
|
$result = $parser->parse(array('mark', 'samurai', 'sword'));
|
|
|
|
$expected = array('mark', 'samurai', 'sword');
|
2010-10-10 19:47:22 +00:00
|
|
|
$this->assertEquals($expected, $result[1], 'Got the correct value.');
|
|
|
|
|
2015-09-25 15:22:00 +00:00
|
|
|
$parser->parse(array('jose', 'coder'));
|
2010-10-10 19:47:22 +00:00
|
|
|
}
|
|
|
|
|
2010-10-10 15:28:27 +00:00
|
|
|
/**
|
|
|
|
* Test adding multiple arguments.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddArguments() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-10 15:28:27 +00:00
|
|
|
$result = $parser->addArguments(array(
|
|
|
|
'name' => array('help' => 'The name'),
|
|
|
|
'other' => array('help' => 'The other arg')
|
|
|
|
));
|
|
|
|
$this->assertEquals($parser, $result, 'addArguments is not chainable.');
|
|
|
|
|
|
|
|
$result = $parser->arguments();
|
|
|
|
$this->assertEquals(2, count($result), 'Not enough arguments');
|
|
|
|
}
|
|
|
|
|
2010-10-10 05:23:20 +00:00
|
|
|
/**
|
|
|
|
* test setting a subcommand up.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSubcommand() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-10 05:23:20 +00:00
|
|
|
$result = $parser->addSubcommand('initdb', array(
|
|
|
|
'help' => 'Initialize the database'
|
|
|
|
));
|
|
|
|
$this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:52:33 +00:00
|
|
|
/**
|
|
|
|
* test addSubcommand with an object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddSubcommandObject() {
|
2010-11-13 04:52:33 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addSubcommand(new ConsoleInputSubcommand('test'));
|
|
|
|
$result = $parser->subcommands();
|
|
|
|
$this->assertEquals(1, count($result));
|
2010-11-13 04:57:55 +00:00
|
|
|
$this->assertEquals('test', $result['test']->name());
|
2010-11-13 04:52:33 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 22:02:10 +00:00
|
|
|
/**
|
|
|
|
* test removeSubcommand with an object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-07-27 07:24:15 +00:00
|
|
|
public function testRemoveSubcommand() {
|
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
|
|
|
$parser->addSubcommand(new ConsoleInputSubcommand('test'));
|
|
|
|
$result = $parser->subcommands();
|
|
|
|
$this->assertEquals(1, count($result));
|
|
|
|
$parser->removeSubcommand('test');
|
|
|
|
$result = $parser->subcommands();
|
|
|
|
$this->assertEquals(0, count($result), 'Remove a subcommand does not work');
|
|
|
|
}
|
2014-07-20 22:02:10 +00:00
|
|
|
|
2010-10-11 17:12:23 +00:00
|
|
|
/**
|
|
|
|
* test adding multiple subcommands
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddSubcommands() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-11 17:12:23 +00:00
|
|
|
$result = $parser->addSubcommands(array(
|
|
|
|
'initdb' => array('help' => 'Initialize the database'),
|
|
|
|
'create' => array('help' => 'Create something')
|
|
|
|
));
|
|
|
|
$this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
|
|
|
|
$result = $parser->subcommands();
|
|
|
|
$this->assertEquals(2, count($result), 'Not enough subcommands');
|
|
|
|
}
|
|
|
|
|
2010-10-12 02:53:23 +00:00
|
|
|
/**
|
|
|
|
* test that no exception is triggered when help is being generated
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHelpNoExceptionWhenGettingHelp() {
|
2010-10-12 02:53:23 +00:00
|
|
|
$parser = new ConsoleOptionParser('mycommand', false);
|
|
|
|
$parser->addOption('test', array('help' => 'A test option.'))
|
|
|
|
->addArgument('model', array('help' => 'The model to make.', 'required' => true));
|
|
|
|
|
|
|
|
$result = $parser->parse(array('--help'));
|
|
|
|
$this->assertTrue($result[0]['help']);
|
|
|
|
}
|
|
|
|
|
2010-10-10 05:54:51 +00:00
|
|
|
/**
|
|
|
|
* test that help() with a command param shows the help for a subcommand
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHelpSubcommandHelp() {
|
2010-10-10 05:54:51 +00:00
|
|
|
$subParser = new ConsoleOptionParser('method', false);
|
|
|
|
$subParser->addOption('connection', array('help' => 'Db connection.'));
|
|
|
|
|
|
|
|
$parser = new ConsoleOptionParser('mycommand', false);
|
|
|
|
$parser->addSubcommand('method', array(
|
|
|
|
'help' => 'This is another command',
|
|
|
|
'parser' => $subParser
|
|
|
|
))
|
|
|
|
->addOption('test', array('help' => 'A test option.'));
|
|
|
|
|
|
|
|
$result = $parser->help('method');
|
|
|
|
$expected = <<<TEXT
|
|
|
|
<info>Usage:</info>
|
|
|
|
cake mycommand method [-h] [--connection]
|
|
|
|
|
|
|
|
<info>Options:</info>
|
|
|
|
|
|
|
|
--help, -h Display this help.
|
|
|
|
--connection Db connection.
|
|
|
|
|
|
|
|
TEXT;
|
2012-04-20 14:38:50 +00:00
|
|
|
$this->assertTextEquals($expected, $result, 'Help is not correct.');
|
2010-10-10 05:23:20 +00:00
|
|
|
}
|
2010-10-10 20:44:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test building a parser from an array.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBuildFromArray() {
|
2010-10-10 20:44:22 +00:00
|
|
|
$spec = array(
|
|
|
|
'command' => 'test',
|
|
|
|
'arguments' => array(
|
|
|
|
'name' => array('help' => 'The name'),
|
|
|
|
'other' => array('help' => 'The other arg')
|
|
|
|
),
|
|
|
|
'options' => array(
|
|
|
|
'name' => array('help' => 'The name'),
|
|
|
|
'other' => array('help' => 'The other arg')
|
|
|
|
),
|
2010-10-11 17:12:23 +00:00
|
|
|
'subcommands' => array(
|
|
|
|
'initdb' => array('help' => 'make database')
|
|
|
|
),
|
2010-10-10 20:44:22 +00:00
|
|
|
'description' => 'description text',
|
|
|
|
'epilog' => 'epilog text'
|
|
|
|
);
|
|
|
|
$parser = ConsoleOptionParser::buildFromArray($spec);
|
|
|
|
|
|
|
|
$this->assertEquals($spec['description'], $parser->description());
|
|
|
|
$this->assertEquals($spec['epilog'], $parser->epilog());
|
|
|
|
|
|
|
|
$options = $parser->options();
|
|
|
|
$this->assertTrue(isset($options['name']));
|
|
|
|
$this->assertTrue(isset($options['other']));
|
2010-10-10 21:15:44 +00:00
|
|
|
|
2010-10-10 20:44:22 +00:00
|
|
|
$args = $parser->arguments();
|
|
|
|
$this->assertEquals(2, count($args));
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-10-11 17:12:23 +00:00
|
|
|
$commands = $parser->subcommands();
|
|
|
|
$this->assertEquals(1, count($commands));
|
2010-10-10 20:44:22 +00:00
|
|
|
}
|
2010-10-11 05:32:09 +00:00
|
|
|
|
2010-10-18 03:35:45 +00:00
|
|
|
/**
|
|
|
|
* test that create() returns instances
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreateFactory() {
|
2010-10-18 03:35:45 +00:00
|
|
|
$parser = ConsoleOptionParser::create('factory', false);
|
|
|
|
$this->assertInstanceOf('ConsoleOptionParser', $parser);
|
|
|
|
$this->assertEquals('factory', $parser->command());
|
|
|
|
}
|
|
|
|
|
2011-08-06 13:52:07 +00:00
|
|
|
/**
|
|
|
|
* test that command() inflects the command name.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCommandInflection() {
|
|
|
|
$parser = new ConsoleOptionParser('CommandLine');
|
|
|
|
$this->assertEquals('command_line', $parser->command());
|
|
|
|
}
|
|
|
|
|
2010-10-11 05:32:09 +00:00
|
|
|
/**
|
|
|
|
* test that parse() takes a subcommand argument, and that the subcommand parser
|
|
|
|
* is used.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testParsingWithSubParser() {
|
2010-10-12 03:43:13 +00:00
|
|
|
$parser = new ConsoleOptionParser('test', false);
|
2010-10-11 05:32:09 +00:00
|
|
|
$parser->addOption('primary')
|
|
|
|
->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
|
|
|
|
->addArgument('two', array('required' => true))
|
|
|
|
->addSubcommand('sub', array(
|
|
|
|
'parser' => array(
|
|
|
|
'options' => array(
|
|
|
|
'secondary' => array('boolean' => true),
|
|
|
|
'fourth' => array('help' => 'fourth option')
|
|
|
|
),
|
|
|
|
'arguments' => array(
|
|
|
|
'sub_arg' => array('choices' => array('c', 'd'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
));
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-10-11 05:32:09 +00:00
|
|
|
$result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
|
2010-10-12 03:43:13 +00:00
|
|
|
$expected = array(array(
|
|
|
|
'secondary' => true,
|
|
|
|
'fourth' => '4',
|
|
|
|
'help' => false,
|
|
|
|
'verbose' => false,
|
|
|
|
'quiet' => false), array('c'));
|
2010-10-11 05:32:09 +00:00
|
|
|
$this->assertEquals($expected, $result, 'Sub parser did not parse request.');
|
|
|
|
}
|
2010-10-14 01:09:33 +00:00
|
|
|
|
2011-08-06 13:52:07 +00:00
|
|
|
}
|