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

/tests/Console/ConsoleInputTest.php

http://github.com/phpundercontrol/phpUnderControl
PHP | 326 lines | 159 code | 40 blank | 127 comment | 1 complexity | f9e627be96ed6181a4c6641b919ce86f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of phpUnderControl.
  4. *
  5. * PHP Version 5.2.0
  6. *
  7. * Copyright (c) 2007-2011, Manuel Pichler <mapi@manuel-pichler.de>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package Console
  41. * @author Manuel Pichler <mapi@manuel-pichler.de>
  42. * @copyright 2007-2011 Manuel Pichler. All rights reserved.
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @version SVN: $Id$
  45. * @link http://www.phpundercontrol.org/
  46. */
  47. require_once dirname( __FILE__ ) . '/../AbstractTest.php';
  48. /**
  49. * Test case for the console input class.
  50. *
  51. * @category QualityAssurance
  52. * @package Console
  53. * @author Manuel Pichler <mapi@manuel-pichler.de>
  54. * @copyright 2007-2011 Manuel Pichler. All rights reserved.
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: @package_version@
  57. * @link http://www.phpundercontrol.org/
  58. */
  59. class phpucConsoleInputTest extends phpucAbstractTest
  60. {
  61. /**
  62. * Tests that {@link phpucConsoleInput::parse()} fails with an exception if
  63. * a mandatory option without a default default is not defined on the cli.
  64. *
  65. * @return void
  66. */
  67. public function testConsoleInputWithoutMandatoryOptionAndNoDefaultValueFail()
  68. {
  69. $definition = new phpucConsoleInputDefinition();
  70. $definition->addCommand( 'foo', 'The foo command.' );
  71. $definition->addOption(
  72. 'foo',
  73. 'b',
  74. 'bar',
  75. 'The bar option',
  76. true,
  77. null,
  78. true
  79. );
  80. $this->prepareArgv( array( 'foo' ) );
  81. $this->setExpectedException(
  82. 'phpucConsoleException',
  83. "The option '--bar' is marked as mandatory and not set."
  84. );
  85. $input = new phpucConsoleInput( $definition );
  86. $input->parse();
  87. }
  88. /**
  89. * Tests that {@link phpucConsoleInput::parse()} fails with an exception if
  90. * a mandatory option without a default default is not defined on the cli.
  91. *
  92. * @return void
  93. */
  94. public function testConsoleInputWithoutMandatoryOptionAndNoDefaultValueButFollowingOptionFail()
  95. {
  96. $definition = new phpucConsoleInputDefinition();
  97. $definition->addCommand( 'foo', 'The foo command.' );
  98. $definition->addOption(
  99. 'foo',
  100. 'b',
  101. 'bar',
  102. 'The bar option',
  103. true,
  104. null,
  105. true
  106. );
  107. $this->prepareArgv( array( 'foo', '-b', '-a' ) );
  108. $this->setExpectedException(
  109. 'phpucConsoleException',
  110. "The option '-b' requires an additional value."
  111. );
  112. $input = new phpucConsoleInput( $definition );
  113. $input->parse();
  114. }
  115. /**
  116. * Tests that {@link phpucConsoleInput::parse()} fails with an exception if
  117. * a whitelisted option is not in the whitelist.
  118. *
  119. * @return void
  120. */
  121. public function testConsoleInputWithInvalidWhitelistOptionValueFail()
  122. {
  123. $definition = new phpucConsoleInputDefinition();
  124. $definition->addCommand( 'foo', 'The foo command.' );
  125. $definition->addOption(
  126. 'foo', 'b', 'bar', 'The bar option', array( 'a', 'b' ), null, true
  127. );
  128. $this->prepareArgv( array( 'foo', '--bar', 'c' ) );
  129. $this->setExpectedException(
  130. 'phpucConsoleException',
  131. 'The value for option --bar must match one of these values "a", "b".'
  132. );
  133. $input = new phpucConsoleInput( $definition );
  134. $input->parse();
  135. }
  136. /**
  137. * Tests that {@link phpucConsoleInput::parse()} fails with an exception if
  138. * an option value doesn't match against the defined regexp.
  139. *
  140. * @return void
  141. */
  142. public function testConsoleInputWithInvalidFormatedOptionValueFail()
  143. {
  144. $definition = new phpucConsoleInputDefinition();
  145. $definition->addCommand( 'foo', 'The foo command.' );
  146. $definition->addOption(
  147. 'foo',
  148. 'b',
  149. 'bar',
  150. 'The bar option',
  151. '/^[0-9a-f]{4}\-[0-9a-f]{2}$/D',
  152. null,
  153. true
  154. );
  155. $this->prepareArgv( array( 'foo', '--bar', '071a-0' ) );
  156. $this->setExpectedException(
  157. 'phpucConsoleException',
  158. "The value for option '--bar' has an invalid format."
  159. );
  160. $input = new phpucConsoleInput( $definition );
  161. $input->parse();
  162. }
  163. /**
  164. * Tests that the magic __get() method fails with an exception for an unknown
  165. * property.
  166. *
  167. * @return void
  168. */
  169. public function testGetterUnknownPropertyFail()
  170. {
  171. $this->setExpectedException(
  172. 'OutOfRangeException',
  173. 'Unknown or writonly property $phpuc.'
  174. );
  175. $definition = new phpucConsoleInputDefinition();
  176. $this->prepareArgv( array( '--version' ) );
  177. $input = new phpucConsoleInput( $definition );
  178. echo $input->phpuc;
  179. }
  180. /**
  181. * Tests that the console arg ctor throws an exception if no $argv variable
  182. * exists.
  183. *
  184. * @return void
  185. */
  186. public function testConsoleWithoutArgv()
  187. {
  188. $this->prepareArgv();
  189. $this->setExpectedException( 'phpucConsoleException' );
  190. new phpucConsoleInput();
  191. }
  192. /**
  193. * Tests the install command without the cc-install-dir argument which must
  194. * result in an {@link phpucConsoleException}.
  195. *
  196. * @return void
  197. */
  198. public function testConsoleInstallCommandButWithoutArguments()
  199. {
  200. $this->prepareArgv( array( 'install' ) );
  201. $this->setExpectedException( 'phpucConsoleException' );
  202. $input = new phpucConsoleInput();
  203. $input->parse();
  204. }
  205. /**
  206. * Tests that the input returns the help for -h and --help.
  207. *
  208. * @return void
  209. */
  210. public function testConsoleInputPrintHelp()
  211. {
  212. $out1 = $this->fetchConsoleInputOutput( '-h' );
  213. $out2 = $this->fetchConsoleInputOutput( '--help' );
  214. $this->assertEquals( $out1, $out2 );
  215. $this->assertRegExp(
  216. '/Command line options and arguments for "\w+"/', $out1
  217. );
  218. }
  219. /**
  220. * Tests the returned version information.
  221. *
  222. * @return void
  223. */
  224. public function testConsoleInputPrintVersion()
  225. {
  226. $out1 = $this->fetchConsoleInputOutput( '-v' );
  227. $out2 = $this->fetchConsoleInputOutput( '--version' );
  228. $this->assertEquals( $out1, $out2 );
  229. $this->assertEquals(
  230. 'phpUnderControl @package_version@ by Manuel Pichler.', $out1
  231. );
  232. }
  233. /**
  234. * Tests the returned usage information.
  235. *
  236. * @return void
  237. */
  238. public function testConsoleInputPrintUsage()
  239. {
  240. $out1 = $this->fetchConsoleInputOutput( '-u' );
  241. $out2 = $this->fetchConsoleInputOutput( '--usage' );
  242. $this->assertEquals( $out1, $out2 );
  243. $this->assertRegExp(
  244. '/^Usage: phpuc.php <command> <options> <arguments>/', $out1
  245. );
  246. }
  247. /**
  248. * Tests that the parse method throws an {@link phpucConsoleException} for
  249. * invalid command identifiers.
  250. *
  251. * @return void
  252. */
  253. public function testConsoleWithInvalidCommandIdentifier()
  254. {
  255. $this->prepareArgv( array( 'phpUnderControl' ) );
  256. $input = new phpucConsoleInput();
  257. ob_start();
  258. try
  259. {
  260. $input->parse();
  261. $this->fail( 'phpucConsoleException expected.' );
  262. }
  263. catch ( phpucConsoleException $e )
  264. {
  265. }
  266. ob_end_clean();
  267. }
  268. /**
  269. * Returns the console output of {@link phpucConsoleInput} for the given
  270. * <b>$option</b>
  271. *
  272. * @param string $option The option to use.
  273. *
  274. * @return string
  275. */
  276. protected function fetchConsoleInputOutput( $option )
  277. {
  278. $this->prepareArgv( array( $option ) );
  279. $input = new phpucConsoleInput();
  280. ob_start();
  281. $input->parse();
  282. $content = ob_get_contents();
  283. ob_end_clean();
  284. return trim( $content );
  285. }
  286. }