* @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id$ * @link http://pear.php.net/package/Console_Getargs */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'Getargs_getValues_testCase::main'); } require_once 'Console/Getargs.php'; require_once 'PHPUnit/Framework.php'; /** * getValues Test case for Console_Getargs * * @category Console * @package Console_Getargs * @subpackage Tests * @author Stephan Schmidt * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/Console_Getargs */ class Getargs_getValues_testCase extends PHPUnit_Framework_TestCase { /** * Runs the test methods of this class. * * @access public * @static */ public static function main() { require_once 'PHPUnit/TextUI/TestRunner.php'; $suite = new PHPUnit_Framework_TestSuite('Getargs_getValues_testCase'); PHPUnit_TextUI_TestRunner::run($suite); } /** * Test getValues('long') with one argument * * @access public * @return void */ function testLong1() { $config = array( 'name' => array( 'short' => 'n', 'min' => 1, 'max' => 1, 'desc' => 'An argument.') ); $args = array('-n', 'arg1'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => 'arg1'), $obj->getValues('long'), "'$message' Incorrect value returned"); } $args = array('--name', 'arg1'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => 'arg1'), $obj->getValues('long'), "'$message' Incorrect value returned"); } } /** * Test getValues('long') with one argument and two values * * @access public * @return void */ function testLong2() { $config = array( 'name' => array( 'short' => 'n', 'min' => 2, 'max' => 2, 'desc' => 'Two arguments.') ); $args = array('-n', 'arg1', 'arg2'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => array('arg1', 'arg2')), $obj->getValues('long'), "'$message' Incorrect value returned"); } $args = array('--name', 'arg1', 'arg2'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => array('arg1', 'arg2')), $obj->getValues('long'), "'$message' Incorrect value returned"); } } /** * Test getValues('short') with one argument * * @access public * @return void */ function testShort() { $config = array( 'name' => array( 'short' => 'n', 'min' => 1, 'max' => 1, 'desc' => 'An argument.') ); $args = array('-n', 'arg1'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('n' => 'arg1'), $obj->getValues('short'), "'$message' Incorrect value returned"); } $args = array('--name', 'arg1'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('n' => 'arg1'), $obj->getValues('short'), "'$message' Incorrect value returned"); } } /** * Test getValues('short') with one argument and two values * * @access public * @return void */ function testShort2() { $config = array( 'name' => array( 'short' => 'n', 'min' => 2, 'max' => 2, 'desc' => 'Two arguments.') ); $args = array('-n', 'arg1', 'arg2'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('n' => array('arg1', 'arg2')), $obj->getValues('short'), "'$message' Incorrect value returned"); } $args = array('--name', 'arg1', 'arg2'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('n' => array('arg1', 'arg2')), $obj->getValues('short'), "'$message' Incorrect value returned"); } } /** * Test getValues('short') and getValues('long') with a switch * * @access public * @return void */ function testSwitch() { $config = array( 'switch' => array( 'short' => 's', 'max' => 0, 'desc' => 'A switch.') ); $args = array('-s'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertTrue($obj->isDefined('s'), "'$message' Switch not defined"); $this->assertTrue($obj->isDefined('switch'), "'$message' Switch not defined"); } $args = array('--switch'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('switch' => true), $obj->getValues('long'), "'$message' Switch not defined"); $this->assertEquals(array('s' => true), $obj->getValues('short'), "'$message' Switch not defined"); } } /** * Test getValues('long') and getValues('short') with two arguments * * @access public * @return void */ function testMultiple() { $config = array( 'name' => array( 'short' => 'n', 'min' => 1, 'max' => 1, 'desc' => 'One argument.'), 'email' => array( 'short' => 'e', 'min' => 2, 'max' => 2, 'desc' => 'Two arguments.'), ); $args = array('-n', 'arg1', '-e', 'schst@php.net', 'wenz@php.net'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'$message' Incorrect value returned"); $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'$message' Incorrect value returned"); } $args = array('--name', 'arg1', '--email', 'schst@php.net', 'wenz@php.net'); $message = implode(' ', $args); $obj =& Console_Getargs::factory($config, $args); if (PEAR::isError($obj)) { $this->fail("'$message' " . $obj->getMessage()); } else { $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'$message' Incorrect value returned"); $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'$message' Incorrect value returned"); } } } if (PHPUnit_MAIN_METHOD == 'Getargs_getValues_testCase::main') { Getargs_getValues_testCase::main(); } ?>