PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/YapepBase/Batch/CliArgumentParserTest.php

https://bitbucket.org/szeber/yapep_base
PHP | 339 lines | 236 code | 57 blank | 46 comment | 0 complexity | 99b8ee6cf2a6024a515a22f91e5f9d23 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of YAPEPBase.
  4. *
  5. * @package YapepBase
  6. * @subpackage Batch
  7. * @copyright 2011 The YAPEP Project All rights reserved.
  8. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  9. */
  10. namespace YapepBase\Batch;
  11. use YapepBase\Exception\Exception;
  12. use YapepBase\Exception\ParameterException;
  13. /**
  14. * Test for the CliArgumentParser class.
  15. *
  16. * @package YapepBase
  17. * @subpackage Batch
  18. */
  19. class CliArgumentParserTest extends \PHPUnit_Framework_TestCase {
  20. /**
  21. * Tests the error handling during the setup phase.
  22. *
  23. * @return void
  24. */
  25. public function testSetupErrorHandling() {
  26. $parser = new CliArgumentParser();
  27. try {
  28. $parser->addSwitch('', '');
  29. $this->fail(
  30. 'A ParameterException should be thrown when trying to add a switch without both a short and a long name');
  31. } catch (ParameterException $e) {
  32. // Do nothing
  33. }
  34. }
  35. /**
  36. * Tests the error handling during parsing.
  37. *
  38. * @return void
  39. */
  40. public function testParseErrorHandling() {
  41. $parser = new CliArgumentParser();
  42. $parser->addSwitch('a', '', true);
  43. $parser->addSwitch('b', '');
  44. try {
  45. $parser->parse(array('-a', '-b'));
  46. $this->fail('Parsing should fail when a switch that requires a value does not have one');
  47. }
  48. catch (Exception $e) {
  49. // Do nothing
  50. }
  51. try {
  52. $parser->parse(array('-a'));
  53. $this->fail('Parsing should fail when a switch that requires a value does not have one');
  54. }
  55. catch (Exception $e) {
  56. // Do nothing
  57. }
  58. try {
  59. $parser->parse(array('-v'));
  60. $this->fail('Parsing should fail when an unknown switch is passed');
  61. }
  62. catch (Exception $e) {
  63. // Do nothing
  64. }
  65. }
  66. /**
  67. * Tests the parsing functions.
  68. *
  69. * @return void
  70. */
  71. public function testParsing() {
  72. $parser = new CliArgumentParser();
  73. $parser->addSwitch('a', 'switch-a');
  74. $parser->addSwitch('b', null);
  75. $parser->addSwitch(null, 'switch-c');
  76. $parser->addSwitch('v', 'value', true);
  77. $parser->addSwitch('o', 'optional', true, true);
  78. // Test parsing for no arguments
  79. $parser->parse(array());
  80. $this->assertSame(array(), $parser->getParsedSwitches(), 'Invalid switches parsed for empty arg list');
  81. $this->assertSame(array(), $parser->getParsedOperands(), 'Invalid operands parsed for empty arg list');
  82. // Test parsing for simple switches ('-a -b --switch-c')
  83. $expectedSwitches = array(
  84. 'a' => 1,
  85. 'switch-a' => 1,
  86. 'b' => 1,
  87. 'switch-c' => 1
  88. );
  89. ksort($expectedSwitches);
  90. $expectedOperands = array();
  91. $parser->parse(array(
  92. '-a',
  93. '-b',
  94. '--switch-c',
  95. ));
  96. $switches = $parser->getParsedSwitches();
  97. ksort($switches);
  98. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  99. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  100. // Test parsing for value switches without a separator ('-vvalue1 -ovalue2')
  101. $expectedSwitches = array(
  102. 'v' => 'value1',
  103. 'value' => 'value1',
  104. 'o' => 'value2',
  105. 'optional' => 'value2'
  106. );
  107. ksort($expectedSwitches);
  108. $expectedOperands = array();
  109. $parser->parse(array(
  110. '-vvalue1',
  111. '-ovalue2',
  112. ));
  113. $switches = $parser->getParsedSwitches();
  114. ksort($switches);
  115. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  116. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  117. // Test parsing for value switches with = as separator for short versions ('-v=value1 -o=value2')
  118. $expectedSwitches = array(
  119. 'v' => 'value1',
  120. 'value' => 'value1',
  121. 'o' => 'value2',
  122. 'optional' => 'value2'
  123. );
  124. ksort($expectedSwitches);
  125. $expectedOperands = array();
  126. $parser->parse(array(
  127. '-v=value1',
  128. '-o=value2',
  129. ));
  130. $switches = $parser->getParsedSwitches();
  131. ksort($switches);
  132. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  133. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  134. // Test parsing for value switches with space separator for short versions ('-v value1')
  135. $expectedSwitches = array(
  136. 'v' => 'value1',
  137. 'value' => 'value1',
  138. );
  139. ksort($expectedSwitches);
  140. $expectedOperands = array();
  141. $parser->parse(array(
  142. '-v',
  143. 'value1',
  144. ));
  145. $switches = $parser->getParsedSwitches();
  146. ksort($switches);
  147. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  148. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  149. // Test parsing for value switches with = as separator for short versions ('--value=value1 --optional=value2')
  150. $expectedSwitches = array(
  151. 'v' => 'value1',
  152. 'value' => 'value1',
  153. 'o' => 'value2',
  154. 'optional' => 'value2'
  155. );
  156. ksort($expectedSwitches);
  157. $expectedOperands = array();
  158. $parser->parse(array(
  159. '--value=value1',
  160. '--optional=value2',
  161. ));
  162. $switches = $parser->getParsedSwitches();
  163. ksort($switches);
  164. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  165. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  166. // Test parsing for value switches with space separator for short versions ('--value value1')
  167. $expectedSwitches = array(
  168. 'v' => 'value1',
  169. 'value' => 'value1',
  170. );
  171. ksort($expectedSwitches);
  172. $expectedOperands = array();
  173. $parser->parse(array(
  174. '--value',
  175. 'value1',
  176. ));
  177. $switches = $parser->getParsedSwitches();
  178. ksort($switches);
  179. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  180. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  181. // Test parsing for collapsed syntax, with one value switch ('-abvvalue1')
  182. $expectedSwitches = array(
  183. 'a' => 1,
  184. 'switch-a' => 1,
  185. 'b' => 1,
  186. 'v' => 'value1',
  187. 'value' => 'value1',
  188. );
  189. ksort($expectedSwitches);
  190. $expectedOperands = array();
  191. $parser->parse(array(
  192. '-abvvalue1',
  193. ));
  194. $switches = $parser->getParsedSwitches();
  195. ksort($switches);
  196. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  197. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  198. // Test parsing for collapsed syntax for non-value switches with multiple invocations ('-aaba')
  199. $expectedSwitches = array(
  200. 'a' => 3,
  201. 'switch-a' => 3,
  202. 'b' => 1,
  203. );
  204. ksort($expectedSwitches);
  205. $expectedOperands = array();
  206. $parser->parse(array(
  207. '-aaba',
  208. ));
  209. $switches = $parser->getParsedSwitches();
  210. ksort($switches);
  211. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  212. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  213. // Test parsing for operands ('-b test1, test2')
  214. $expectedSwitches = array(
  215. 'b' => 1,
  216. );
  217. ksort($expectedSwitches);
  218. $expectedOperands = array(
  219. 'test1',
  220. 'test2',
  221. );
  222. $parser->parse(array(
  223. '-b',
  224. 'test1',
  225. 'test2',
  226. ));
  227. $switches = $parser->getParsedSwitches();
  228. ksort($switches);
  229. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  230. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  231. // Test parsing for finish operator ('-a -- -b')
  232. $expectedSwitches = array(
  233. 'a' => 1,
  234. 'switch-a' => 1,
  235. );
  236. ksort($expectedSwitches);
  237. $expectedOperands = array(
  238. '-b'
  239. );
  240. $parser->parse(array(
  241. '-a',
  242. '--',
  243. '-b',
  244. ));
  245. $switches = $parser->getParsedSwitches();
  246. ksort($switches);
  247. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  248. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  249. // Test parsing for optional value and operand ('-o test')
  250. $expectedSwitches = array(
  251. 'o' => false,
  252. 'optional' => false
  253. );
  254. ksort($expectedSwitches);
  255. $expectedOperands = array(
  256. 'test'
  257. );
  258. $parser->parse(array(
  259. '-o',
  260. 'test'
  261. ));
  262. $switches = $parser->getParsedSwitches();
  263. ksort($switches);
  264. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  265. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  266. // Test parsing when the same value switch is added multiple times ('-vvalue1 -vvalue2')
  267. $expectedSwitches = array(
  268. 'v' => 'value2',
  269. 'value' => 'value2',
  270. );
  271. ksort($expectedSwitches);
  272. $expectedOperands = array();
  273. $parser->parse(array(
  274. '-vvalue1',
  275. '-vvalue2',
  276. ));
  277. $switches = $parser->getParsedSwitches();
  278. ksort($switches);
  279. $this->assertSame($expectedSwitches, $switches, 'Invalid switches were parsed');
  280. $this->assertSame($expectedOperands, $parser->getParsedOperands(), 'Invalid operands were parsed');
  281. }
  282. }