/tests/Getargs_getValuesTest.php

https://github.com/CloCkWeRX/Console_Getargs · PHP · 297 lines · 182 code · 40 blank · 75 comment · 27 complexity · 32ada775f0d4d8bee604bbe08298d61b MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * getValues Test case for Console_Getargs
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Console
  15. * @package Console_Getargs
  16. * @subpackage Tests
  17. * @author Stephan Schmidt <schst@php.net>
  18. * @copyright 1997-2005 The PHP Group
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  20. * @version CVS: $Id$
  21. * @link http://pear.php.net/package/Console_Getargs
  22. */
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Getargs_getValues_testCase::main');
  25. }
  26. require_once 'Console/Getargs.php';
  27. require_once 'PHPUnit/Framework/TestCase.php';
  28. /**
  29. * getValues Test case for Console_Getargs
  30. *
  31. * @category Console
  32. * @package Console_Getargs
  33. * @subpackage Tests
  34. * @author Stephan Schmidt <schst@php.net>
  35. * @copyright 1997-2005 The PHP Group
  36. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  37. * @version Release: @package_version@
  38. * @link http://pear.php.net/package/Console_Getargs
  39. */
  40. class Getargs_getValuesTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @access public
  46. * @static
  47. */
  48. public static function main() {
  49. require_once 'PHPUnit/TextUI/TestRunner.php';
  50. $suite = new PHPUnit_Framework_TestSuite('Getargs_getValues_testCase');
  51. PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Test getValues('long') with one argument
  55. *
  56. * @access public
  57. * @return void
  58. */
  59. function testLong1()
  60. {
  61. $config = array(
  62. 'name' => array(
  63. 'short' => 'n',
  64. 'min' => 1,
  65. 'max' => 1,
  66. 'desc' => 'An argument.')
  67. );
  68. $args = array('-n', 'arg1');
  69. $message = implode(' ', $args);
  70. $obj =& Console_Getargs::factory($config, $args);
  71. if (PEAR::isError($obj)) {
  72. $this->fail("'$message' " . $obj->getMessage());
  73. } else {
  74. $this->assertEquals(array('name' => 'arg1'), $obj->getValues('long'), "'$message' Incorrect value returned");
  75. }
  76. $args = array('--name', 'arg1');
  77. $message = implode(' ', $args);
  78. $obj =& Console_Getargs::factory($config, $args);
  79. if (PEAR::isError($obj)) {
  80. $this->fail("'$message' " . $obj->getMessage());
  81. } else {
  82. $this->assertEquals(array('name' => 'arg1'), $obj->getValues('long'), "'$message' Incorrect value returned");
  83. }
  84. }
  85. /**
  86. * Test getValues('long') with one argument and two values
  87. *
  88. * @access public
  89. * @return void
  90. */
  91. function testLong2()
  92. {
  93. $config = array(
  94. 'name' => array(
  95. 'short' => 'n',
  96. 'min' => 2,
  97. 'max' => 2,
  98. 'desc' => 'Two arguments.')
  99. );
  100. $args = array('-n', 'arg1', 'arg2');
  101. $message = implode(' ', $args);
  102. $obj =& Console_Getargs::factory($config, $args);
  103. if (PEAR::isError($obj)) {
  104. $this->fail("'$message' " . $obj->getMessage());
  105. } else {
  106. $this->assertEquals(array('name' => array('arg1', 'arg2')), $obj->getValues('long'), "'$message' Incorrect value returned");
  107. }
  108. $args = array('--name', 'arg1', 'arg2');
  109. $message = implode(' ', $args);
  110. $obj =& Console_Getargs::factory($config, $args);
  111. if (PEAR::isError($obj)) {
  112. $this->fail("'$message' " . $obj->getMessage());
  113. } else {
  114. $this->assertEquals(array('name' => array('arg1', 'arg2')), $obj->getValues('long'), "'$message' Incorrect value returned");
  115. }
  116. }
  117. /**
  118. * Test getValues('short') with one argument
  119. *
  120. * @access public
  121. * @return void
  122. */
  123. function testShort()
  124. {
  125. $config = array(
  126. 'name' => array(
  127. 'short' => 'n',
  128. 'min' => 1,
  129. 'max' => 1,
  130. 'desc' => 'An argument.')
  131. );
  132. $args = array('-n', 'arg1');
  133. $message = implode(' ', $args);
  134. $obj =& Console_Getargs::factory($config, $args);
  135. if (PEAR::isError($obj)) {
  136. $this->fail("'$message' " . $obj->getMessage());
  137. } else {
  138. $this->assertEquals(array('n' => 'arg1'), $obj->getValues('short'), "'$message' Incorrect value returned");
  139. }
  140. $args = array('--name', 'arg1');
  141. $message = implode(' ', $args);
  142. $obj =& Console_Getargs::factory($config, $args);
  143. if (PEAR::isError($obj)) {
  144. $this->fail("'$message' " . $obj->getMessage());
  145. } else {
  146. $this->assertEquals(array('n' => 'arg1'), $obj->getValues('short'), "'$message' Incorrect value returned");
  147. }
  148. }
  149. /**
  150. * Test getValues('short') with one argument and two values
  151. *
  152. * @access public
  153. * @return void
  154. */
  155. function testShort2()
  156. {
  157. $config = array(
  158. 'name' => array(
  159. 'short' => 'n',
  160. 'min' => 2,
  161. 'max' => 2,
  162. 'desc' => 'Two arguments.')
  163. );
  164. $args = array('-n', 'arg1', 'arg2');
  165. $message = implode(' ', $args);
  166. $obj =& Console_Getargs::factory($config, $args);
  167. if (PEAR::isError($obj)) {
  168. $this->fail("'$message' " . $obj->getMessage());
  169. } else {
  170. $this->assertEquals(array('n' => array('arg1', 'arg2')), $obj->getValues('short'), "'$message' Incorrect value returned");
  171. }
  172. $args = array('--name', 'arg1', 'arg2');
  173. $message = implode(' ', $args);
  174. $obj =& Console_Getargs::factory($config, $args);
  175. if (PEAR::isError($obj)) {
  176. $this->fail("'$message' " . $obj->getMessage());
  177. } else {
  178. $this->assertEquals(array('n' => array('arg1', 'arg2')), $obj->getValues('short'), "'$message' Incorrect value returned");
  179. }
  180. }
  181. /**
  182. * Test getValues('short') and getValues('long') with a switch
  183. *
  184. * @access public
  185. * @return void
  186. */
  187. function testSwitch()
  188. {
  189. $config = array(
  190. 'switch' => array(
  191. 'short' => 's',
  192. 'max' => 0,
  193. 'desc' => 'A switch.')
  194. );
  195. $args = array('-s');
  196. $message = implode(' ', $args);
  197. $obj =& Console_Getargs::factory($config, $args);
  198. if (PEAR::isError($obj)) {
  199. $this->fail("'$message' " . $obj->getMessage());
  200. } else {
  201. $this->assertTrue($obj->isDefined('s'), "'$message' Switch not defined");
  202. $this->assertTrue($obj->isDefined('switch'), "'$message' Switch not defined");
  203. }
  204. $args = array('--switch');
  205. $message = implode(' ', $args);
  206. $obj =& Console_Getargs::factory($config, $args);
  207. if (PEAR::isError($obj)) {
  208. $this->fail("'$message' " . $obj->getMessage());
  209. } else {
  210. $this->assertEquals(array('switch' => true), $obj->getValues('long'), "'$message' Switch not defined");
  211. $this->assertEquals(array('s' => true), $obj->getValues('short'), "'$message' Switch not defined");
  212. }
  213. }
  214. /**
  215. * Test getValues('long') and getValues('short') with two arguments
  216. *
  217. * @access public
  218. * @return void
  219. */
  220. function testMultiple()
  221. {
  222. $config = array(
  223. 'name' => array(
  224. 'short' => 'n',
  225. 'min' => 1,
  226. 'max' => 1,
  227. 'desc' => 'One argument.'),
  228. 'email' => array(
  229. 'short' => 'e',
  230. 'min' => 2,
  231. 'max' => 2,
  232. 'desc' => 'Two arguments.'),
  233. );
  234. $args = array('-n', 'arg1', '-e', 'schst@php.net', 'wenz@php.net');
  235. $message = implode(' ', $args);
  236. $obj =& Console_Getargs::factory($config, $args);
  237. if (PEAR::isError($obj)) {
  238. $this->fail("'$message' " . $obj->getMessage());
  239. } else {
  240. $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'$message' Incorrect value returned");
  241. $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'$message' Incorrect value returned");
  242. }
  243. $args = array('--name', 'arg1', '--email', 'schst@php.net', 'wenz@php.net');
  244. $message = implode(' ', $args);
  245. $obj =& Console_Getargs::factory($config, $args);
  246. if (PEAR::isError($obj)) {
  247. $this->fail("'$message' " . $obj->getMessage());
  248. } else {
  249. $this->assertEquals(array('name' => $args[1], 'email' => array($args[3], $args[4])), $obj->getValues('long'), "'$message' Incorrect value returned");
  250. $this->assertEquals(array('n' => $args[1], 'e' => array($args[3], $args[4])), $obj->getValues('short'), "'$message' Incorrect value returned");
  251. }
  252. }
  253. }
  254. if (PHPUnit_MAIN_METHOD == 'Getargs_getValues_testCase::main') {
  255. Getargs_getValues_testCase::main();
  256. }
  257. ?>